Thread: Linking problems

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    Undefined reference. Using extern

    Hello again.

    I have 3 files:
    main.cpp:
    Code:
    #include <iostream>
    #include "def.h"
    using namespace std;
    
    int main()
    {
        cout << server_name << endl;
    }
    def.h:
    Code:
    #ifndef _def_h_
    #define _def_h_
    #include <iostream>
    extern const std::string server_name;
    
    #endif
    def.cpp:
    Code:
    #ifndef _def_cpp_
    #define _def_cpp_
    #include <iostream>
    const std::string server_name = "www.google.com";
    
    #endif

    I tried to compile them like this:
    g++ main.cpp -c
    g++ def.cpp -c
    g++ main.o def.o

    And I get this error:
    main.o(.text+0x12e):main.cpp: undefined reference to `server_name'
    collect2: ld returned 1 exit status
    Last edited by hardi; 12-13-2006 at 12:42 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    def.cpp should include def.h
    def.cpp doesn't need include guards.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also def.cpp
    does not need <iostream>
    instead <string> should be included
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    guards = the #ifndef?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hardi
    guards = the #ifndef?
    yes
    guards are used only in files that are included in other files to prevent multiple includes

    cpp file in most cases are not included in other file - so there is no need in the guards
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    But why doesn't def.cpp work without including the def.h

    btw: I included def.h and it worked.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    def.h is used to notify the compiler that the const std::string server_name is defined somethere else

    when compiling def.cpp compiler already knows that this variable is defined and how, so including def.h is not needed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by vart
    def.h is used to notify the compiler that the const std::string server_name is defined somethere else

    when compiling def.cpp compiler already knows that this variable is defined and how, so including def.h is not needed
    What you are saying contradicts with Salem's sayings:

    Quote Originally Posted by Salem
    def.cpp should include def.h
    def.cpp doesn't need include guards.
    He tells me to include def.h - which strangely helped me to compile the program, but without including the def.h file, I got the error I posted above. (You are telling me, that def.cpp doesn't need def.h so it should not be included - yes, g++ def.cpp -c gives me no errors, but if I type g++ def.o main.o I get the linker errors. but when I include the def.h file in def.cpp, the compiler shuts up and makes an executable. So: Why does it work? What's wrong with the initial system?)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Imho def.h helps main.cpp understand, that server_name is defined elsewhere and def.cpp just defines the variables. so if I link them together, all should be happy, but unfortunately that's not the case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ OpenGL linking problems
    By KoshiB in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 05:25 PM
  2. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 01:47 AM
  3. Linking problems trying to compile QT application
    By Maragato in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2005, 09:08 PM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. More linker problems
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 10:27 PM