Linking and Libraries

This is a discussion on Linking and Libraries within the C++ Programming forums, part of the General Programming Boards category; I have two questions First off, When you include a header file in your program, does the compiler then link ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    Linking and Libraries

    I have two questions

    First off,

    When you include a header file in your program, does the compiler then link the program to the library that it intails? Or are the libraries always linked? I´d assume that they´re only linked when the header is included, but how does that work?

    Secondly,

    Recently, I wrote a program as a test that was composed to two cpp files. I then wrote two functions that were *exactly* the same and put on into each file. I then prototyped the one function in the first file (the on with main() in it) and made a call to it in main(). I assumed that I would get some sort of error, ambiguity or such, but no such error occurred, and the program simply called the function in the first file. Why?


    thanks very very much for the help!

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    1. The header files are linked to the libraries and at compile time those libraries are then included in your program.

    2. Well, I do recieve an error as expected from the linker informing me that the function was already defined in another file.
    I am using VS.NET 2002. What are you using?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I´d assume that they´re only linked when the header is included
    Or they're linked if a name from the header is actually used. That way you don't pay for what you don't use. It really depends on the linker involved, so your question is implementation dependent. However, to save yourself unnecessary grief, you should assume that each header forces the linker to load the object file for that header. If the header isn't included then the object file isn't loaded, even if you declare the name yourself.

    >Why?
    I get a linker error complaining about multiple definitions. Post the code you've been using as well as your compiler and the switches you're using to invoke it (if applicable).
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    does the compiler then link the program to the library that it intails?
    No, that is the linkers job - not the compilers.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    bithub: right

    im not near my compiler right now, but it is visual studio .net 2003

    The code was something like this:

    first file
    Code:
    void test();
    
    int main()
    {
         test();
    }
    
    void test()
    {
         std::cout << "test";
    }
    second file
    Code:
    void test()
    {
         std::cout << "test";
    }
    mayble it only links the files if it sees a function call to an alien function? and it just assumes theres only one test since its only prototyped once?
    Last edited by krygen; 12-26-2004 at 01:35 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Did you compile and link, or just compile? After including iostream in both files, the project will compile cleanly, but as soon as it tries to link you'll get an error complaining about test being defined more than once.
    My best code is written with the delete key.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Considering that both functions give the exact same output how can you tell which is being called?

    Of course if you want multiple definations you can change the linkage
    bt9.cpp
    Code:
    #include <iostream>
    void test();
    void foo();
    
    int main()
    {
      test();
      foo();
    }
    
    void test()
    {
      std::cout<<"Test in bt9"<<std::endl;
    }
    bt10.cpp
    Code:
    #include <iostream>
    static void test();
    
    void foo()
    {
      test();
    }
    
    static void test()
    {
      std::cout<<"Test in bt10"<<std::endl;
    }

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >static void test();
    This is a deprecated feature. To give a global name internal linkage, nest it in an unnamed namespace:
    Code:
    namespace {
      void test();
    }
    >static void test()
    >{
    Just FYI, only the first occurance of static is required to give a function internal linkage. So this is perfectly acceptable:
    Code:
    #include <iostream>
    static void test();
    
    void foo()
    {
      test();
    }
    
    void test()
    {
      std::cout<<"Test in bt10"<<std::endl;
    }
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    yeah, i compiled and linked... weird...
    are you using visual studio .net 2003 prelude?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >are you using visual studio .net 2003 prelude?
    That's one of the compilers I use, yes.
    My best code is written with the delete key.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks Prelude. I do sooo much internal linkage that I forgot about those rules

    edit: Hey Prelude I'm having trouble finding where in the standard it says that static is deprecated for linkage. Mind pointint me to the right section? I'm still learning where everything is at
    Last edited by Thantos; 12-26-2004 at 02:00 PM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I do sooo much internal linkage that I forgot about those rules
    Internal linkage is your happy fuzzy security blanket. By forcing strict linkage, you save yourself headaches when working on large projects.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    prelude: the program i posted above gives a linking error when you build in vc++ .net 2003? because if it does i must have just been mistaken in thinking it worked.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the program i posted above gives a linking error when you build in vc++ .net 2003?
    ------ Rebuild All started: Project: Cpp, Configuration: Debug Win32 ------

    Deleting intermediate files and output files for project 'Cpp', configuration 'Debug|Win32'.
    Compiling...
    Cpp2.cpp
    Cpp.cpp
    Generating Code...
    Linking...
    Cpp2.obj : error LNK2005: "void __cdecl test(void)" (?test@@YAXXZ) already defined in Cpp.obj
    C:\Documents and Settings\CCoder\My Documents\Visual Studio Projects\Cpp\Debug\Cpp.exe : fatal error LNK1169: one or more multiply defined symbols found

    Build log was saved at "file://c:\Documents and Settings\CCoder\My Documents\Visual Studio Projects\Cpp\Debug\BuildLog.htm"
    Cpp - 2 error(s), 0 warning(s)


    ---------------------- Done ----------------------

    Rebuild All: 0 succeeded, 1 failed, 0 skipped
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    alright, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linking to libraries
    By davbeck in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 04:56 PM
  2. Help with linking to other c++ libraries via .net
    By pipercubusa in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2005, 01:08 PM
  3. Two questions (Linking Libraries and Creating Them)
    By Thantos in forum Linux Programming
    Replies: 3
    Last Post: 03-21-2004, 04:01 PM
  4. Dynamically linking libraries
    By neandrake in forum Windows Programming
    Replies: 5
    Last Post: 06-18-2003, 02:54 PM
  5. Linking .h files to libraries
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2001, 08:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21