This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: c++/8749: undefined reference error when declaring a function as inline and using -O3 switch


Synopsis: undefined reference error when declaring a function as inline and using -O3 switch

State-Changed-From-To: open->closed
State-Changed-By: reichelt
State-Changed-When: Fri Nov 29 13:37:15 2002
State-Changed-Why:
    Not a bug.
    
    Paragraph 7.1.2.4 of the ISO-Standard states:
    
    An inline function shall be defined in every translation unit in which it
    is used and shall have exactly the same definition in every case. [...]
    
    As I understand it, you have a situation similar to the following:
    
    File1.cc:
    ----------------snip here----------------
    inline int foo (int x) { return x; }
    
    int bar() { return foo(2); }
    ----------------snip here----------------
    
    File2.cc:
    ----------------snip here----------------
    inline int foo (int);
    
    int main() { return foo(1); }
    ----------------snip here----------------
    
    If I compile this using "g++ File1.cc File2.cc" I get a working program
    that returns 1. If I compile it with "-O" enabled, I get a linker error:
    
      Unresolved text symbol "foo(int)" -- 1st referenced by /var/tmp//cckhU7pa.o.
    
    Without optimization the function "foo" in the first file isn't inlined.
    But because it's used by "bar" it is put in the object file. With optimization
    the function is inlined and doesn't appear explicitly in the object file of
    the first file. Therefore, the linker error.
    
    In essence: The compiler does not need to put inline functions in the object
    file. The compiler can just optimize them away. And because of that we have
    paragraph 7.1.2.4.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8749


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]