What can cause multiple determination errors after declaring an inline function?

I had this header file containing many built-in functions, after compilation it says: multiple function definition ***, a function that looks like this:

inline int testab(int a, int b) { return a>b; } 

after adding statics before the built-in, the error disappeared. Is it correct? Or am I missing something? I thought I could configure the built-in functions in a header like this.

+4
source share
2 answers

inline or not, once the header is copied in at least two files, you can no longer link the files.

The only way to safely use the function in the header is to use static . Thus, each copy of the function will be invisible to others.

Please note that there are no restrictions on their use, so you can safely write:

 static inline bool testab(int a, int b) { return a>b; } 

Edit: Details

inline tells the compiler that you think the function is small enough to be inline. That is, you tell the compiler that you do not think that the additional space for imposing the function matters, but not the (insignificant) (possible) performance enhancement for it. Most compilers, however, are smart enough to decide that on their own and with your keyword, they will just be a little more oriented to the built-in ones and it’s not necessary to always listen to you. Of course, it depends on the compiler. Some compilers may completely ignore the keyword.

static , on the other hand, means that no matter what area is defined by a static variable, it will be invisible outside of it. If the function has a static variable, it will be invisible outside of it. If you have a static variable in the file (that is, a static global variable), it will be invisible outside of it, which means that after compilation for the linker, the symbol does not exist and is confused. That is why if you wrote a library that has global variables or functions that should not be visible outside the library, you must declare them static .

Edit 2: Correction

Apparently, according to this , inline functions should not have their identifiers exported for the linker. However, you can attach static to it anyway to make it more understandable. Apparently, some compilers still export the identifier, so static is really required in such cases.

+7
source

You need to specify static if you define a function in the header file, otherwise for the function (one in each .c file) several descriptions of the table table are created and will be encountered when trying to link the corresponding object files, To define the built-in function in the header, I I believe that you need:

 static inline int foo(int x) __attribute__((always_inline)) { return x+1; } 

not sure if this is true; see http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Inline.html#Inline .

0
source

Source: https://habr.com/ru/post/1386270/


All Articles