Thread: How to link libraries to MinGW?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    20

    Post How to link libraries to MinGW?

    I've been trying to get SDL to work with my MinGW GCC compiler but when I try to link the libs from the command line, it searches through all the default search paths and misses the libraries that I have already saved in there. I'm trying to link them through options from the DOS prompt like so:

    Code:
    gcc foo.c -l libSDL.la -l libSDLmain.a -l libSDL.dll.a
    I'm not using any IDE with MinGW because I'm accustomed to compiling on the command line.

    So does anyone know what the issue is?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The Unix tradition is that a library called libfoo.a is passed to the compiler/linker using -lfoo

    That is, the lib prefix and .a suffix is automatically assumed.
    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
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    Extension or not, it doesn't work on my windows system, I know for fact that the rules are different, but I can't figure them out.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are those .a files in the compiler's lib directory? If not, did you specify the directory that they are in with the -L option?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    yes, they are in the lib directory, but they aren't being recognized by the compiler for some reason.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't what the reason is, but I do know that it should work with MinGW. You'll need to provide more information.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    say I put in the following compilation command when writing an SDL program:

    GCC foo.c
    I get a heap of error messages due to the libraries failing to be referenced. I try it again, this time manually appending the libs to compiler, like so:

    GCC foo.c -l libSDLmain.a
    It goes through the default search paths, and still find nothing, even though the libraries are lying right inside the search path.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, but that completely ignores Salem's correct advice. What happens when you try this?
    Code:
    gcc foo.c -l SDLmain
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Also, it could be that the order matters. Have you tried specifying the library before your file foo.c?

    BTW, I use SDL 1.3 on Linux, it's fantastic. Good choice!

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    Huh. Well what do you know, it worked, I actually tried it the way Salem suggested, but I forgot to exclude "lib" from the name, thanks for writing it out, now I see what was the issue.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by ahernan17 View Post
    Huh. Well what do you know, it worked, I actually tried it the way Salem suggested, but I forgot to exclude "lib" from the name, thanks for writing it out, now I see what was the issue.
    Enjoy writing your OpenGL code, or whatever it is you're doing. If it's OpenGL stuff, I recommend DevIL as an image loader. It's easy enough that I was able to figure it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  2. Removing link libraries in static libs
    By 39ster in forum C Programming
    Replies: 1
    Last Post: 06-10-2008, 10:22 AM
  3. SDL and MinGW Studio
    By Vicious in forum Tech Board
    Replies: 0
    Last Post: 07-30-2004, 09:59 PM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM

Tags for this Thread