Linking a .lib file on Windows 7

孤者浪人 提交于 2019-12-13 16:02:49

问题


I have a C++ program test.cpp and I want to link two .lib files to it(fhlib.lib and gc_lib.lib).I have the .lib files in the same folder as my .cpp program. I'm on Windows 7.

What I have tried so far is the following:

g++ -o main main.cpp -L/Users\Documents\Visual Studio 2015\Projects\My Project -lfhlib

But I get an

No such file or directory error.

I'm sure the path is correct because I copied it from Properties->Location. But I had do delete the "C:\", because it was not compiling.

EDIT: I found this http://www.mingw.org/wiki/Specify_the_libraries_for_the_linker_to_use.

So I tried using

"-I" instead of "-l"

But still doesn't work.I get:

undefined reference to 'fh_set'...

回答1:


If you're compiling with g++ on windows, I guess you're using MinGW: MinGW relies on .a libraries. When using the "-l" option, the compiler is looking for a library file with the extension .a.

Libraries in the format .lib are compiled with visual studio: you can't use it as this. Compile your libraries with MinGW if you have the sources or consider migrating your project to visual studio.




回答2:


So the problem was that the lib files where compiled in VS. And I had to use the VS compiler instead of g++ and everything worked fine.



来源:https://stackoverflow.com/questions/38584344/linking-a-lib-file-on-windows-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!