简体   繁体   English

gcc:与源文件位于同一文件夹中的链接库

[英]gcc: Link Library In Same Folder As Source Files

I'm trying to compile a C project using gcc . 我正在尝试使用gcc编译C项目。 All source files and the .a library file are in the same folder . 所有源文件和.a库文件都在同一文件夹中 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

You specify the directory to -L and the 'core' name to -l : 您将目录指定为-L ,并将“核心”名称指定为-l

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

When given -l pt , the linker looks for libpt.a or libpt.so or equivalents (extensions like .dylib or .sl or .dll or .lib on other platforms). 给定-l pt ,链接程序将查找libpt.alibpt.so或等效文件(在其他平台上的扩展名如.dylib.sl.dll.lib )。

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. -L -./是在暗示了链接看目录中称为“双点”,这是不可能存在的,以及未libpt.a无论如何发现。

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. 您也可以使用共享库执行此操作,但您可能不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM