General linking question

This is a discussion on General linking question within the C Programming forums, part of the General Programming Boards category; Greetings C experts I have a very simple question that I feel should be asked sooner rather than later. First ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    General linking question

    Greetings C experts

    I have a very simple question that I feel should be asked sooner rather than later. First off I will say I have MinGW installed, and am using Code::Blocks IDE on top of it, with a few plugins allowing for different project types like SDL and OpenGL, GLUT, Ogre3d, etc...

    If you have a source file (say test.c), that has an #include at the top that includes the SDL header (say SDL.h), you can then reference externally defined functions. After compilation, is there any specific job the user has to do in order to bind the definitions on SDL.h to the library (say SDL.lib)? Both the header and the library are in the default \include and \lib directories, and it definitely finds the header file because it compiles successfully.

    I ask this because when I create an SDL project with Code::Blocks, I haven't added any external references to a library, nor could I find anything in the settings of the IDE that references it by default (since it is an SDL project). It compiles and links correctly. But when I manually added PDCurses to the \header and \lib directories, and created a program with it, it could find the header with no problem, but only could link it if I explicitly added a reference to the PDCurses .lib file.

    Basically what I don't understand is why the SDL app worked with seemingly no reference to the corresponding lib, but the PDCurses app did not work unless I explicitly referenced its lib file.

    Thanks for the help in advance,
    Brandon

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Linking is not normally automatic. Your environment seems to link SDL automatically, because this is an "SDL project," but this is not the norm. Usually, you must link against a library explicitly. Including a header file won't cause the linker to do it automatically, since the linker has no clue what a header file is.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What happens if you create a non-SDL project? Can you compile SDL code? Probably not. Somewhere, somehow, the SDL is being explicitly linked to. Probably this is being done by your IDE. Just guessing.

    SDL.lib? I thought only libSDL.a worked with MinGW . . . well, my version at least.

    [edit]
    Including a header file won't cause the linker to do it automatically, since the linker has no clue what a header file is.
    Actually I think there are compiler-specific pragmas for MSVC that will link a to library. Something like this:
    Code:
    #pragma lib("SDL.lib")
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2

    Thanks

    Okay, that is as I thought. I figured Code::Blocks was just doing something behind the scenes that I couldn't find. I will try the empty project idea to confirm.

    And yes, the SDL.lib was just kind of made up. I didn't recall the exact name off the top of my head

    Thanks for the (extremely) prompt reply. I am gonna have to come back here for any C/C++ questions I have .

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Quote Originally Posted by dwks View Post
    SDL.lib? I thought only libSDL.a worked with MinGW . . . well, my version at least.
    Microsoft style .lib files are identical to .a files, at least these days. So I bet it could link a library called XXX as either XXX.lib or libXXX.a.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Hmm. I'll have to try renaming some .libs. You may have saved me a lot of hassle. Thank you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General question
    By saudi-vip in forum C Programming
    Replies: 11
    Last Post: 10-10-2008, 04:46 PM
  2. General question from a beginner to Windows C++ programming
    By Kontan in forum Windows Programming
    Replies: 1
    Last Post: 09-29-2006, 08:03 PM
  3. General Socket TCP/UDP Security Question
    By Zeusbwr in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-04-2005, 04:18 PM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM

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