简体   繁体   English

如何使用静态库编译gcc?

[英]How to compile gcc with static library?

I have static library lib.a and in all tutorials using:我有静态库lib.a并在所有教程中使用:

gcc -o main main.o -L. -lib

But I cant, I have errors:但我不能,我有错误:

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

I need to use:我需要使用:

gcc -o main main.o -L. -lib.a

Why?为什么? What should I do to repair it ?我该怎么做才能修复它?

From the documentation of gcc -l :gcc -l的文档中:

-l library : -l

The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a .链接器搜索库的标准目录列表,该库实际上是一个名为 liblibrary.a文件 The linker then uses this file as if it had been specified precisely by name.然后链接器使用这个文件,就好像它是按名称精确指定的一样。

... ...

The only difference between using an -l option and specifying a file name is that -l surrounds library with 'lib' and '.a' and searches several directories.使用 -l 选项和指定文件名之间的唯一区别是 -l 用 'lib' 和 '.a' 包围库并搜索多个目录。

So you cannot use -l with a library named 'lib.a'.因此,您不能将-l与名为“lib.a”的库一起使用。 Use 'lib.a' without the -l to include it.使用不带-l 'lib.a' 来包含它。 Of course, you cannot use -L then to set the directories to be searched for this particular library.当然,您不能使用-L then 来设置要搜索此特定库的目录。

Do you have the error with this line ?你有这条线的错误吗?

gcc -o main main.o -L. -llib

As MicroVirus found in the documentation, you will have to rename your library in liblib.a to use my previous line or just pass your library to gcc like a simple file.正如 MicroVirus 在文档中发现的那样,您必须在 liblib.a 中重命名您的库以使用我之前的行,或者像一个简单的文件一样将您的库传递给 gcc。

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

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