Welcome! Log In Create A New Profile

Advanced

l;inking with an inline function

Posted by g_man 
l;inking with an inline function
September 19, 2010 06:31AM
I'm having problems linking to a function that I made and have declared inline. I have it declared in fileA.c and I have it in fileA.h, but when I try to use it in fileB.c which has fileA.h included it doesn't find the function.

Here is an example:
FileA.c:
float inline myInlineFunc(int foo,int bar){
return foo/bar;
}
FileA.h:
float inline myInlineFunc(int foo,int bar);
FileB.c:
#include FileB.h

myInlineFunc(5,3); //This line gives me a linking error.



I know everything is set up right, because when it isn't declared inline then it works fine. Is there any way to fix this. My function is called a few times and is only one line long, so it would be nice to be inlined, but it isn't crucial.

I believe that this is caused by gcc deleting the code for the function after it has been compiled. If so, is there any way to stop this from happening?

Thanks
Re: l;inking with an inline function
September 19, 2010 10:30PM
Inline happens at compile time not at link time, so that's why it won't link (as the function is already inlined) the only way to get inline functions to work is to put the whole implementation in the header file. (for best compiler compatibility make the function "static inline")
Re: l;inking with an inline function
September 20, 2010 12:01AM
that makes sense, but isn't it bad practice to put c code in a header file. Would it be a better idea to just copy the code and put it in fileB.c.
Re: l;inking with an inline function
September 20, 2010 05:21AM
Putting a function's code in a header file is basically the same as declaring it inline, except you avoid multiple definition errors by using inline.



Edited 1 time(s). Last edit at 09/20/2010 05:22AM by tueidj.
Re: l;inking with an inline function
September 20, 2010 05:37AM
Quote
tueidj
Putting a function's code in a header file is basically the same as declaring it inline, except you avoid multiple definition errors by using inline.
are you sure? but declaring it static inline would also solve the same problem.
Re: l;inking with an inline function
September 20, 2010 01:20PM
Quote
g_man
Quote
tueidj
Putting a function's code in a header file is basically the same as declaring it inline, except you avoid multiple definition errors by using inline.
are you sure? but declaring it static inline would also solve the same problem.
Indeed, static would solve the multiple definition errors. The "inline" keyword is just a hint for the compiler, the compiler is free to ignore it if it wants to. Also, in gcc "inline" implies "static".

More on gcc and inline can be found here: [gcc.gnu.org]
Re: l;inking with an inline function
September 21, 2010 03:32PM
Ok that would explane my errors. So that means it has to be declared with the code in every c file I plane to use it in?
Sorry, only registered users may post in this forum.

Click here to login