How to link libraries to MinGW?

This is a discussion on How to link libraries to MinGW? within the C Programming forums, part of the General Programming Boards category; I've been trying to get SDL to work with my MinGW GCC compiler but when I try to link the ...

  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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,269
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,269
    I don't what the reason is, but I do know that it should work with MinGW. You'll need to provide more information.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,269
    Right, but that completely ignores Salem's correct advice. What happens when you try this?
    Code:
    gcc foo.c -l SDLmain
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:13 PM
  2. Removing link libraries in static libs
    By 39ster in forum C Programming
    Replies: 1
    Last Post: 06-10-2008, 11:22 AM
  3. SDL and MinGW Studio
    By Vicious in forum Tech Board
    Replies: 0
    Last Post: 07-30-2004, 10: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


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