views:

1669

answers:

1

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

+2  A: 
Johannes Schaub - litb
This is a very good answer. Unfortunately, I still haven't been able to apply the knowledge to successfully compile my program - attempts to change the extern inlines to static inlines make it fail with the error that it is mixing static and nonstatic definitions of the same function. Perhaps I can use macros in my own code to change the names of the functions where I include the headers, but there are approximately 100 of these things in the headers...
Dennis
For further information, this discussion indicates the compiler optimization level can affect the issue: http://lkml.indiana.edu/hypermail/linux/kernel/0408.0/1787.html
Dennis