C语言 GCC,链接库,未找到?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5683058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GCC, linking libraries, not found?
提问by user697111
- OS: Windows 7 Pro X64
- IDE: Eclipse IDE for C/C++ Developers
- Compiler: MinGW (lastest, 4.5.2)
- 操作系统:Windows 7 Pro X64
- IDE:面向 C/C++ 开发人员的 Eclipse IDE
- 编译器:MinGW(最新,4.5.2)
Compiling HelloWorld.c works; but when I try to add some external libraries it chokes.
编译HelloWorld.c 有效;但是当我尝试添加一些外部库时,它会窒息。
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.
我将 .a 和 .dll 文件添加到我的“库”中;将两者的路径添加到 PATH 和 Library Path。我还放置了包含文件并配置了包含。据说我拥有的库与 win/mingw 兼容。他们还有一个不同的 MSVC 下载,它确实有效。
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).
令人沮丧。ld.exe 给出了完整路径,显然在那里,我有读/写它们的权限。我还包括没有路径的它们(它们在库路径和路径中)。
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:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: 找不到 -lC:\rhino\data\ lib\glfw.dll c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:找不到-lC: \rhino\data\lib\libglfwdll.ac:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:不能找到 -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.
我什至将它们添加到我的 C:\MinGW\lib 路径中,但它仍然找不到它们。
回答by eriktous
Michael Burr pointed out the correct way of referencing libraries on the command line. The path to the library is given with the -Lswitch, and the name of the library with the -lswitch (the name of the library being the file name, without the libpart at the beginning, and the .asuffix at the end).
Michael Burr 指出了在命令行上引用库的正确方法。库的路径用-L开关给出,库的名称用-l开关给出(库的名称是文件名,lib开头没有部分,.a结尾没有后缀)。
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.
还有一点要指出的是,您正试图同时链接到库的静态 (libglfw.a) 和动态 (glfw.dll) 版本,这两个版本都包含在下载中。相反,您应该根据您的需求/愿望选择一个,并且只链接到那个。
Linking against the static version is straightforward. Just add -lglfwto the command line.
链接静态版本很简单。只需添加-lglfw到命令行。
To use the dynamic library, you should link against the import library for the dll (libglfwdll.a), by using the -lglfwdllswitch, 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.)
要使用动态库,您应该libglfwdll.a通过使用-lglfwdll开关链接到 dll ( )的导入库,并从链接命令中省略 dll 本身。基本上,导入库不包含任何目标代码,而只包含定义;实际代码在 dll 中。dll 将在运行时动态链接。(为此,系统必须能够找到 dll;即它必须在当前工作目录中,在路径中的目录中,或者它的目录必须添加到使用的特殊环境变量中对于这件事;但要使其变得重要,您首先必须成功构建可执行文件。)
回答by Michael Burr
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 libprefix or the .aextension. Try:
我的经验(不包括在 Eclipse 中可能如何配置)是ld(gcc 将调用的)想要没有lib前缀或.a扩展名的 lib 名称。尝试:
gcc -LC:\rhino\data\libs -LC:\rhino\data\lib -oTestC.exe TestC.o -lglfw -lglfwdll
I'm not sure that the glfw.dllfile 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.
我不确定该glfw.dll文件是否应列为库;该 DLL 的导入库(我假设是 libglfwdll.lib)应该负责链接到 DLL。
回答by TonyK
Try this:
尝试这个:
gcc -LC:\rhino\data\libs -LC:\rhino\data\lib -oTestC.exe TestC.o -lglfw libglfw.a libglfwdll.a

