[Bug c++/45594] New: g++ incorrectly treats inline function redefinition
If two separate .cpp files contain two versions of inline function with the same signature, that are used by functions in the corresponding files, one of them is dropped and all functions use only one of them. Consider example: main.cpp - #include "test1.h" #include "test2.h" int main() { print_out_1(); print_out_2(); return 0; } - test1.h - #ifndef TEST1_H #define TEST1_H void print_out_1(); #endif // TEST1_H - test2.h - #ifndef TEST2_H #define TEST2_H void print_out_2(); #endif // TEST2_H - test1.cpp - #include "test1.h" #include inline int print_stuff(int n) { printf("number 255\n"); return n; } void print_out_1() { print_stuff(10); } - test2.cpp - #include "test2.h" #include inline int print_stuff(int n) { printf("letter A\n"); return n; } void print_out_2() { print_stuff(10); } - The main() function simply calls print_out_1() function from test1.h and print_out_2() function from test2.h, which in turn call print_stuff() inline functions. The expected output would be: - number 255 letter A - The actual output is: - number 255 number 255 - This error appears for both debug and release (-O2) builds. -- Summary: g++ incorrectly treats inline function redefinition Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: justchecking8964 at gmail dot com GCC build triplet: i486-linux-gnu GCC host triplet: i486-linux-gnu GCC target triplet: i486-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45594
[Bug c++/45594] g++ incorrectly treats inline function redefinition
--- Comment #2 from justchecking8964 at gmail dot com 2010-09-08 10:30 --- (In reply to comment #1) > You are violating the ODR. > ODR rule relates only to the non-inline functions, which is not the case here, see http://en.wikipedia.org/wiki/One_Definition_Rule : #2: In the entire program, an object or non-inline function cannot have more than one definition; if an object or function is used, it must have exactly one definition. You can declare an object or function that is never used, in which case you don't have to provide a definition. In no event can there be more than one definition. The case that apply here is discussed in point 3: #3: Some things, like types, templates, and extern inline functions, can be defined in more than one translation unit. For a given entity, each definition must be the same. Non-extern objects and functions in different translation units are different entities, even if their names and types are the same. The case above concerns non-extern inline functions, that "are different entities, even if their names and types are the same". -- justchecking8964 at gmail dot com changed: What|Removed |Added Status|RESOLVED|UNCONFIRMED Resolution|INVALID | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45594
[Bug c++/45594] g++ incorrectly treats inline function redefinition
--- Comment #4 from justchecking8964 at gmail dot com 2010-09-08 10:39 --- (In reply to comment #3) > (In reply to comment #2) > > The case that apply here is discussed in point 3: > > > > #3: ... For a given entity, each definition > > must be the same. ... > But this applies only to "[...] types, templates, and extern inline functions". My case is about non-extern inline functions, which "in different translation units are different entities, even if their names and types are the same." -- justchecking8964 at gmail dot com changed: What|Removed |Added Status|RESOLVED|UNCONFIRMED Resolution|INVALID | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45594
[Bug c++/45594] g++ incorrectly treats inline function redefinition
--- Comment #6 from justchecking8964 at gmail dot com 2010-09-08 11:00 --- > Your inline functions are extern, not static. Make them static and it > will work. > I see, thanks, I missed the part of the new standard that defines inline as being implicitly extern. There's something new to learn every day. Sorry for the bother. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45594