When compiling with optimization turned off (-O0), inline functions are no
actually compiled as inline, but
when multiple source files contain inline functions of the same name, every
call to such function from any file
will actually transfer control to the function defined in the file first
specified in linker list. No errors
or warnings are actually being generated leading to hard-to-detect problems
while compiling big projects.
Example:
1.cpp
#include
inline void test(){ printf("1"); }
void test1(){ test(); }
2.cpp
#include
inline void test(){ printf("2"); }
void test2(){ test(); }
m.cpp
void test1();void test2();
int main(){test1(); test2(); return 0;}
-
When optimizations are turned on (-O2), the program outputs "12" as it should).
If you turn optimization to 0, (g++ -O0 1.cpp 2.cpp m.cpp), the program outputs
"11".
--
Summary: Non-static inline functions cause bugs when defined more
than once in different files
Product: gcc
Version: 4.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: Ivan dot Scherbakov at acronis dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30583