Hello -- I have a project that uses inline functions that need to be usable in code compiled both as GNU C89 and from C++. To get the inline functions to work in GNU C89-based code, the project does the "double-link" trick -- inlcuding the inlined function once as "extern inline" in the .h file, and then again without any specifiers in the .c file for the backup definition. When the .h file is included in C++ code, the inlined function is specified as "inline" in the C++ semantics. When optimization is turned on, and thus inline functions are actually inlined, everything works fine. However, on Cygwin, when functions are not inlined, I get link-time errors: the weak symbol emitted for the C++ code conflicts with the backup definition. (This problem does not occur on ELF-based systems, tested on GNU/Linux (RHEL 4) and Solaris 8.) Is this a gcc bug? Does anyone have any advice about a way to define my inline functions such that this problem will not arise? Simplified test case: $ gcc -Wall -g -O2 -c inline_conflict_c.c $ g++ -Wall -g -O2 -c inline_conflict_cpp.cpp $ g++ -Wall -g -O2 -o inline_conflict inline_conflict_c.o inline_conflict_cpp.o $ ./inline_conflict; echo $? 47 $ gcc -Wall -g -O0 -c inline_conflict_c.c $ g++ -Wall -g -O0 -c inline_conflict_cpp.cpp $ g++ -Wall -g -O0 -o inline_conflict inline_conflict_c.o inline_conflict_cpp.o inline_conflict_cpp.o: In function `foo': /home/Jonathan/Inline-Conflict/inline_conflict.h:23: multiple definition of `_foo' inline_conflict_c.o:/home/Jonathan/Inline-Conflict/inline_conflict.h:23: first defined here collect2: ld returned 1 exit status $ gcc -v Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs Configured with: /gcc/gcc-3.4.4/gcc-3.4.4-1/configure --verbose --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,d,f77,java,objc --enable-nls --without-included-gettext --enable-version-specific-runtime-libs --without-x --enable-libgcj --disable-java-awt --with-system-zlib --enable-interpreter --disable-libgcj-debug --enable-threads=posix --enable-java-gc=boehm --disable-win32-registry --enable-sjlj-exceptions --enable-hash-synchronization --enable-libstdcxx-debug : (reconfigured) Thread model: posix gcc version 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
#include "inline_conflict.h" #define IN_SOURCE #include "inline_conflict.h"
#include "inline_conflict.h" int main() { return foo(47); }
#if __cplusplus #define INLINE inline extern "C" { #elif(__GNUC__) #ifndef IN_SOURCE #define INLINE extern inline #else #undef INLINE #define INLINE /* nothing */ #endif #endif INLINE int foo(int x) { return x; } #if __cplusplus } /* extern "C" */ #endif
-- Jonathan Lennox lennox@xxxxxxxxxxxxxxx