gcc: Link Library In Same Folder As Source Files

社会主义新天地 提交于 2020-12-05 11:12:33

问题


I'm trying to compile a C project using gcc. All source files and the .a library file are in the same folder. How can I successfully compile the project?

I've tried:

gcc -o test main.c IPT.c logitem_list.c -L -./ -libpt

But I receieve error:

/usr/bin/ld: cannot find -libpt
collect2: error: ld returned 1 exit status

回答1:


You specify the directory to -L and the 'core' name to -l:

gcc -o test main.c IPT.c logitem_list.c -L . -lpt

When given -lpt, the linker looks for libpt.a or libpt.so or equivalents (extensions like .dylib or .sl or .dll or .lib on other platforms).

The -L -./ is suggesting that the linker look in a directory called 'dash dot', which is unlikely to exist and isn't where libpt.a is found anyway.




回答2:


Because it's a static lib, you can also most easily just specify the file directly on the command line. Remember that a static library is just an indexed archive of object files:

gcc -o test main.c IPT.c logitem_list.c ./libpt.a

You can also do this with shared libraries, but you probably shouldn't.



来源:https://stackoverflow.com/questions/37971316/gcc-link-library-in-same-folder-as-source-files

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