简体   繁体   English

使用-l标志链接静态库

[英]Linking static library with -l flag

How can I have my makefile compile with the -l flag? 如何使用-l标志编译makefile?

I have a makefile that looks like 我有一个看起来像的makefile

myLibrary:
    gcc -c myLibrary.c -o myLibrary.o
    ar cr libmyLibrary.a myLibrary.o

and then I compile my main program with 然后我编译我的主程序

main:
    gcc -g -c -o main.o main.c
    gcc main.o -o main libmyLibrary.a

The above makefile works, but if I want to replace 上面的makefile工作,但如果我想替换

libmyLibrary.a

with -lmyLibrary I get an error. -lmyLibrary我收到一个错误。 Shouldn't both be working the same? 两个都不应该一样吗?

Here is a rudimentary, unrealistic makefile that will make the static library libmyLibary before it makes the program main , which it will link with the static library using the -L (library search-path) and -l (library) options. 这是一个基本的,不切实际的makefile,它将使静态库libmyLibary成为程序main ,它将使用-L (库搜索路径)和-l (库)选项与静态库链接。

Makefile Makefile文件

.PHONY: all clean

all: libmyLibrary.a main

main: main.o | libmyLibrary.a
    $(CC) -o main main.o -L. -lmyLibrary

libmyLibrary.a: myLibrary.o
    $(AR) rcs libmyLibrary.a myLibrary.o

clean:
    rm -f *.o libmyLibrary.a main

which runs like: 运行方式如下:

$ make
cc    -c -o myLibrary.o myLibrary.c
ar rcs libmyLibrary.a myLibrary.o
cc    -c -o main.o main.c
cc -o main main.o -L. -lmyLibrary

As I think you know, it's unrealistic to make both a library and a program that links with it in the same makefile, since the point of a library is that you don't need to keep remaking it to link it with many programs. 我想你知道,在同一个makefile中同时创建一个库和一个与它链接的程序是不现实的,因为库的重点在于你不需要继续重建它以将它与许多程序链接起来。 You'd really have a makefile for libmyLibrary.a and other makefiles for programs that use it. 对于使用它的程序,你真的有libmyLibrary.a的makefile和其他makefile。

This is how the gcc linkage options -L and -l work: 这是gcc链接选项-L-l工作方式:

-L/path/to/search

tells the linker to look for any libraries that you specify with the -l option in /path/to/search , before it looks for them in its default search directories. 告诉链接器在/path/to/search您使用-l选项指定的任何库,然后在其默认搜索目录中查找它们。 The current directory, . 当前目录, . , isn't one of the linker's default search directories. ,不是链接器的默认搜索目录之一。 So if you want it to find a library specified with the -l option in the current directory, then you need to specify -L. 因此,如果您希望它在当前目录中找到使用-l选项指定的库,则需要指定-L.

-lfoo

tells the linker to search for either a dynamic library, libfoo.so , or a static library, libfoo.a , first in your -L directories, if any, in the order you've specified them, and then in its default search directories. 告诉链接器按照您指定的顺序,首先在-L目录中搜索动态库libfoo.so或静态库libfoo.a ,然后在其默认搜索目录中搜索。 It stops searching as soon as if finds either libfoo.so or libfoo.a in one of the search directories. 如果在其中一个搜索目录中找到libfoo.solibfoo.a ,它会立即停止搜索。 If it finds both of them in the same directory, then by default it will link libfoo.so with your program and not link libfoo.a . 如果它发现他们在同一个目录,然后默认情况下它会链接libfoo.so与您的程序,而不是链接libfoo.a

To link purely statically library, use -static, Like 要纯粹链接静态库,请使用-static,Like

gcc -static main.c libmyLibrary.a

And run executable file ./a.out GCC Linux. 并运行可执行文件./a.out GCC Linux。

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

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