用-l标志链接静态库 [英] Linking static library with -l flag

查看:44
本文介绍了用-l标志链接静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用-l标志编译我的makefile?

How can I have my makefile compile with the -l flag?

我有一个看起来像的makefile

I have a makefile that looks like

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

然后我用

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

上面的makefile有效,但是如果我要替换

The above makefile works, but if I want to replace

libmyLibrary.a

-lmyLibrary 一起出现错误.两者不应该都一样工作吗?

with -lmyLibrary I get an error. Shouldn't both be working the same?

推荐答案

这是一个基本的,不切实际的makefile,它将使静态库 libmyLibary 在制作程序 main 之前,它将与静态库链接使用 -L (库搜索路径)和 -l <​​/code>(库)选项.

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

.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

运行方式:

$ 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

我想您知道,同时制作一个库和一个程序是不现实的它在同一makefile中链接,因为库的位置是您无需不断修改它即可将其与许多程序链接.你真的有 libmyLibrary.a 的makefile和程序的其他makefile使用它.

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. You'd really have a makefile for libmyLibrary.a and other makefiles for programs that use it.

这是gcc链接选项 -L -l <​​/code>的工作方式:

This is how the gcc linkage options -L and -l work:

-L/path/to/search

告诉链接器查找您在/path/to/search 中使用 -l <​​/code>选项指定的任何库,在它的默认搜索目录中查找它们之前.当前目录.,不是链接器的默认搜索目录之一.所以如果你想要在当前目录中找到使用 -l <​​/code>选项指定的库,则需要指定 -L.

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. 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.

-lfoo

告诉链接器搜索动态库 libfoo.so 还是静态库库 libfoo.a ,首先按照您的顺序在您的 -L 目录中(如果有)指定它们,然后在其默认搜索目录中.它停止搜索只要在其中一个搜索目录中找到 libfoo.so libfoo.a 即可.如果在同一目录中找到它们两者,则默认情况下它将链接 libfoo.so 与您的程序,而不是链接 libfoo.a .

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. It stops searching as soon as if finds either libfoo.so or libfoo.a in one of the search directories. 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.

这篇关于用-l标志链接静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆