inline function in namespace generate duplicate symbols during link on gcc
Asked Answered
S

2

10

I have a namespace with inline function that will be used if several source files. When trying to link my application, the inline function are reported as duplicate symbols. It seems as if my code would simply not inline the functions and I was wondering if this is the expected behavior and how to best deal with it.

I use the following gcc options: -g -Wextra -pedantic -Wmissing-field-initializers -Wredundant-decls -Wfloat-equal -Wno-reorder -Wno-long-long The same code style seems to compile and link properly when build in a VC7 environment.

The following code example shows the structure of the code:

/* header.h */
namespace myNamespace {
inline bool myFunction() {return true;}
}

/* use_1.cpp */
#include "header.h"
...
bool OK = myNamespace::myFunction();
...

/* use_2.cpp */
#include "header.h"
...
bool OK = myNamespace::myFunction();
...
Schwa answered 21/9, 2010 at 19:29 Comment(3)
Is OK a global variable in each cpp file? That would cause a duplicate symbol error. Assuming that it's not global, your example is legal C++. Try compiling with -Winline. What version of gcc are you using?Middlebrooks
Please post the actual compiler message. Does the error occur with minimal source files (just the mentioned lines without the stuff implied by ...)?Unglue
Once I changed one of the names of OK to OK1, added an empty main function and removed the ...; compiling both files with the options that you supplied worked correctly for me. You need to post more details as the cause of your error is not here.Irradiance
S
0

Delete the build directory for the dll/exe and recompile.

There is possibly some problem with the build. It could be pre-compiled headers as alluded to by the OP in a comment above. I had exactly the same problem and deleting the build directory and recompiling solved it.

Sharynshashlik answered 4/3, 2020 at 7:33 Comment(0)
L
-1

The inline keyword is taken only as a hint by the compiler. If the compiler decides that the function would be better performing without inline, it will not inline it. There are vendor-specific keywords that make the compiler inline a function - it is __attribute__((always_inline)) for GCC and __forceinline for Visual C++.

If you really want to make sure your function won't be causing linker errors in all cases on all standard compilers, you might want to make it templated, as templated functions are guaranteed not to cause linker errors even if defined in headers. This is, however, quite unnecessary for really simple functions.

Laudian answered 21/9, 2010 at 19:40 Comment(3)
Your code doesn't cause linker errors if you write it properly. Writing templates where they are not needed is nonsense.Unglue
Whether a C++ compiler inlines a function or not is not important here. The compiler handles this transparently under the as-if rule. Nevertheless inline affects the program's semantics. It is appropriate here regardless of whether the compiler actually inlines the function. Please research the "one definition rule".Obsecrate
SOLVED: I finally had the time to track down the problem to it's roots and (as so often) it was a simple consistency problem with the use of precompiled headers. I learned that the use of inline is in fact transparent and does not require to take care if the compiler actually does inline the function or not. And most important: always use the -Winvalid-pch flag when using precompiled headers.Schwa

© 2022 - 2024 — McMap. All rights reserved.