6
  • OS: Windows 7 Pro X64
  • IDE: Eclipse IDE for C/C++ Developers
  • Compiler: MinGW (lastest, 4.5.2)

Compiling HelloWorld.c works; but when I try to add some external libraries it chokes.

I added the .a and .dll files to my 'Libraries'; add the path of both to PATH and Library Path. I also put the include files and configured the Include. The libraries I have are said to be compatible with win/mingw. They also have a different download for MSVC which does work.

Frustrating. The ld.exe gives the full path and obviously there and I have permissions to read/write them. I also included them without path (they are in library path and path).

I don't understand why this isn't working.

c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lC:\rhino\data\lib\glfw.dll c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lC:\rhino\data\lib\libglfwdll.a c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lC:\rhino\data\lib\libglfw.a

C:\Users\rhino>dir C:\rhino\data\lib\libglfw.a
04/15/2011  05:24 PM            70,384 libglfw.a

Updated:

I've even added them to my C:\MinGW\lib path and it still can't find them.

user697111
  • 2,232
  • 8
  • 29
  • 40

4 Answers4

12

Michael Burr pointed out the correct way of referencing libraries on the command line. The path to the library is given with the -L switch, and the name of the library with the -l switch (the name of the library being the file name, without the lib part at the beginning, and the .a suffix at the end).

One more thing to point out is that you're trying to link to both the static (libglfw.a) and the dynamic (glfw.dll) version of the library, which are both included in the download, at the same time. Instead, you should pick one, based on your needs/desires, and only link to that one.

Linking against the static version is straightforward. Just add -lglfw to the command line.

To use the dynamic library, you should link against the import library for the dll (libglfwdll.a), by using the -lglfwdll switch, and omit the dll itself from the link command. Basically, the import library doesn't contain any object code, but only definitions; the actual code is in the dll. The dll will be dynamically linked at run time. (For this to work, the system has to be able to find the dll; i.e. it has to be in the current working directory, in a directory that is in the path, or its directory has to be added to a special environment variable used for this thing; but for this to become important, you first have to succeed in building the executable.)

eriktous
  • 6,569
  • 2
  • 25
  • 35
4

My experience (which doesn't include how this might be configured in Eclipse) is that ld (which gcc will invoke) wants the lib names without the lib prefix or the .a extension. Try:

gcc -LC:\rhino\data\libs -LC:\rhino\data\lib -oTestC.exe TestC.o -lglfw -lglfwdll

I'm not sure that the glfw.dll file should be listed as a library; the import library for that DLL (I assume that's libglfwdll.lib) should take care of linking to the DLL.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • You can link to dll's too in recent versions of GCC. the you need to add the DLL path to the `-L` option as well. – rubenvb Apr 16 '11 at 11:17
  • @rubenvb: interesting - I didn't know that. Now the questions are: why doesn't MSVC's linker let you do that, and what took so long? – Michael Burr Apr 16 '11 at 17:52
  • well, you can, through `LoadLibrary` and associated messiness. As to what took so long; I don't know exactly when it was implemented, but it seems somewhere before/around 2007 looking through old forum posts... – rubenvb Apr 16 '11 at 20:26
0

Try this:

gcc -LC:\rhino\data\libs -LC:\rhino\data\lib -oTestC.exe TestC.o -lglfw libglfw.a libglfwdll.a
TonyK
  • 16,761
  • 4
  • 37
  • 72
  • C:\rhino\EProjects\TestC\Debug>gcc -LC:\MinGW\lib -oTestC.exe TestC.o -lglfw libglfw.a libglfwdll.a gcc: libglfw.a: No such file or directory gcc: libglfwdll.a: No such file or directory C:\rhino\EProjects\TestC\Debug>gcc -LC:\MinGW\lib -oTestC.exe TestC.o -lglfw libglfw libglfwdll gcc: libglfw: No such file or directory gcc: libglfwdll: No such file or directory C:\rhino\EProjects\TestC\Debug>gcc -LC:\MinGW\lib -oTestC.exe TestC.o -lglfw -llibglfw.a libglfwdll.a gcc: libglfwdll.a: No such file or directory – user697111 Apr 16 '11 at 00:10
  • C:\rhino\EProjects\TestC\Debug>gcc -LC:\MinGW\lib -oTestC.exe TestC.o -lglfw -llibglfw.a -llibglfwdll.a c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibglfw.a c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibglfwdll.a collect2: ld returned 1 exit status – user697111 Apr 16 '11 at 00:12
  • c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot finC:\rhino\EProjects\TestC\Debug>gcc -LC:\MinGW\lib -oTestC.exe TestC.o -lglfw -llibglfw -llibglfwdll c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibglfw c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibglfwdll collect2: ld returned 1 exit statusd -llibglfw.a c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -llibglfwdll collect2: ld returned 1 exit status – user697111 Apr 16 '11 at 00:12
0

Although this is quite old, I came across this question from a recent problem and the solution is quite different to the above, so am sharing.

In my case, due to the shell $PATH order (echo $PATH), gcc was using ld from the anaconda install, and this ld couldn't find a particular library.

Just changing the path order solved it:

export PATH=/usr/bin:$PATH

There are no harmful effects, see here.

Colin
  • 453
  • 4
  • 16