I have a C++ program that I compile with mingw (gcc for Windows). Using the TDM release of mingw which includes gcc 4.4.1. The executable links to two static library (.a) files: On of them is a third-party library written in C; the other is a C++ library, written by me, that uses the C library provides my own C++ API on top.
An (in my view, excessive) portion of the C library's functionality is implemented in inline functions. You can't avoid including the inline functions when you use the C library's API, but when I try to link it all together, I'm getting link errors saying there is a multiple definition of all of the inline functions - both ones I have called in my C++ wrapper library and ones which I have not, basically anything defined inline in the headers has gotten a function created for it in both the C library and the C++ library.
It doesn't cause multiple definition errors when the include files are used multiple times in different .c or .cpp files in the same project; the problem is just that it generates one definition per library.
How/why is the compiler generating functions and symbols for these inline functions in both libraries? How can I force it to stop generating them in my code? Is there a tool I can run to strip the duplicate functions from the .a file, or a way to make the linker ignore multiple definitions?
(FYI, the third party library does include #ifdef __cplusplus and extern "C" guards in all its headers; anyway if that were the problem, it would not cause a multiple definition of symbol, it would cause the opposite problem because the symbol would be undefined or at least different.)
Notably, the link errors do NOT occur if I link to the third party C library's DLL; however then I get strange runtime failures that seem to have to do with my code having its own version of functions it should be calling from the DLL. (As if the compiler is creating local versions of functions I didn't ask for.)
Similar versions of this question have been asked before, however, I didn't find the answer to my situation in any of these:
The answer to this question was that the poster was multiply defining variables, my problem is multiple definition of inline functions: http://stackoverflow.com/questions/223771/repeated-multiple-definition-errors-from-including-same-header-in-multiple-cpps
This was an MSVC program, but I'm using mingw; also, the poster's problem in this question was the definition of a C++ class constructor outside of the class body in a header, while my problem is with C functions that are inline: http://stackoverflow.com/questions/717622/static-lib-multiple-definition-problem
This fool renamed all his C code as C++ files and his C code wasn't C++-safe:http://stackoverflow.com/questions/2208834/multiple-definition-of-lots-of-std-functions-when-linking
This one was just wanting to know why violating the one definition rule was not an error:http://stackoverflow.com/questions/2025380/unpredictable-behavior-of-inline-functions-with-different-definitions
extern
, it creates an external definition - which is not an inline definition anymore. These external definitions cannot appear multiple times in the program. In C++ an explicitextern
on such a function has no effect. You are best off with doingstatic inline
in C. – ᐅ Johannes Schaub - litb ᐊ Feb 7 '10 at 17:57static inline
anymore. You could use-fgnu89-inline
and useextern inline
even in C mode, though (see gcc.gnu.org/onlinedocs/gcc/Inline.html . If you compile without-std=c99
, it may even be the mode in effect anyway already, i think). – ᐅ Johannes Schaub - litb ᐊ Feb 7 '10 at 20:19