This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
Hi,
I would like to define inline function in 1.c file.
And would like to call this inline function in 2.c file.
I'm using C28x compiler v6.4.2 LTS.
I tried to declare the inline function in a .h file by adding extern, but I'm still not able to call the inline function in 2.c file.
Hello!
Inline functions are static, i.e. visible only within translation unit where they are defined. One solution is to define, not declare, but define inline function in header file. When one includes that header in src1.c, src2.c, inline function definition is pulled in both and does the job.
Hope this helps.
Howard Zou said:I would like to define inline function in 1.c file.
And would like to call this inline function in 2.c file.
It doesn't work that way. For a function call to be inlined, the full implementation of the function must be visible. The best solution is to supply the full implementation of the function in a header file. Be sure to to use the keyword inline. For example ...
inline int function_that_is_inlined(int argument) { /* all of the code for the function is here */ }
Include this header file in every source file which calls the function.
Thanks and regards,
-George
George,
my customer would like to build their own lib which has the inline function.
And then they can call this function in different project which only need to include the lib.
They need it to be inline function because they are calling this function in time critical interrupt service routine and inline function runs faster.
And when they build their own lib.
No matter they put inline function to .h file or .c file, when they call this function in a project which include the .lib, it report error.
Is it not possible to add an inline function to a .lib?
How to achieve the need that different project will need to call the same inline function?
Another question is:
If
a. we add a function(not inline function) to a lib and call the function in a project by including the lib,
b. we write this function directly in a .c file in a project.
Will the project code size be bigger for a than for b and occupy more flash space?
Hello!
Inline function is a special beast. Unlike regular function, there is no call sequence for inlined one, instead it gets dissolved in the code of caller. So if inline function was used somewhere inside the library, it is not visible outside it.
For the clarity, imagine a situation where you have custom_lib_src1.c, custom_lib_src2.c, and inline function is defined in custom_hdr.h. I mean defined rather than declared, just like George had illustrated. Then to be able access that inline function in both library sources, one has to include custom_hdr.h in both custom_lib_src1.c, custom_lib_src2.c. For clarity, assume there was foo() function in custom_hdr.h, and bar() function defined in custom_lib_src1.c. In library project, when you included custom_hdr.h, foo() is seen in both sources. Now when you compiled you library project, the output is custom_lib.lib. If you look at modules listed in there, you'll find bar() function to be available, however, no foo(). It already get dissolved in respective callers. So if you want same inline function to be seen in other part of project, you have to include that custom_hdr.h with full definition in your project files.
As to the size question, to my knowledge, at linking stage only used functions code is pulled into final executable. For instance, you may refer standard library for sin(), and its code gets pulled in, but unused cos(), tan(), atan() are not linked into final image. From this point of view, there is no benefit in code size in either method.Having library may reduce project build time, as library source requires no translation, linking only. On the other hand having function included in project source along with other functions could be beneficial, when file-wide optimization is on. However, I'd better step aside at this point as I had no practice with C28x.
Hi,
for the inline function question:
if we put it in .h file, it's dissolved in respective caller.
Then how about we put it( foo() in your example) in .c file? I guess when I look at modules listed in there, I still can't find foo() right? Because it's inline function.
Then are there any good way to share a specific code between different project(I mean could be called by different project, we don't have to write it in every project), but still take the advantage of execution speed-up like inline function?
Hi,
It's a nature on inline function to dissolve in caller. When we tell to put inline function body in header, it effectively means we want it be written one time, and then copied to multiple destinations with #include directive. You may copy its body every C-file, you may pull it in with include - important thing is that inline function body should be defined in the file where it gets used. And where it it gets dissolved and no longer visible outside.
The only good way to have inline function shared - distribute its body through header file. Then you code it once and reuse. There is no way to distribute inline function in non-source way. One can have same header file included in multiple projects. We are doing just this way for div 3, div 6, mod 3, mod 6 functions.
rrlagic said:The only good way to have inline function shared - distribute its body through header file.
I confirm that this is correct.
Thanks and regards,
-George
If we write
#define EINT __asm(" clrc INTM")
in a .h file in a library.
Then we include that library in a new project.
Then I write "EINT" in a .c file in this new project, will it be replaced by __asm(" clrc INTM")?
If it's okay, then we may use this way to replace INLINE function to share functions between projects and keep the advantage of execution speed up.
That's a reasonable technique.
With regard to interrupts, you ought to considering using these intrinsics ...
unsigned int status; status = __disable_interrupts(); /* interrupts disabled here */ __restore_interrupts(status);
Please search the C28x compiler manual for those intrinsics.
Thanks and regards,
-George