图书馆连结 [英] Library Linking

查看:100
本文介绍了图书馆连结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Zed Shaw的Learn C学习硬编程.我一直在研究ex26,我们在其中创建了一个用于安装软件的程序"devpkg".此练习需要安装Apache Portable Runtime库.在编写了本练习的代码之后,我无法使用以下makefile来编译程序:

I am trying to learn to C programing using Zed Shaw's Learn C the hard way. I have been working on ex26 where we create a program "devpkg" used to install software. This exercise requires installing Apache Portable Runtime library. After writing the code for this exercise I could not get program to compile using the following makefile:

    PREFIX?=/user/local
    CFLAGS=-g -Wall -I${PREFIX}/apr/include/apr-1 -I{PREFIX}/apr/include/apr-util-1
    LDFLAGS=-L${PREFIX}/apr/lib -lapr-1 -pthread -laprutil-1

    all: devpkg

    install: all
             install -d${DESTDIR}/${PREFIX}/bin/
             install devpkg ${DESTDIR}/${PREFIX}/bin/

    clean:
            rm -f *.o
            rm -f devpkg
            rm -f *.dSYM

该生成文件似乎不起作用,因为当我使用"$ make devpkg"时,并未声明所有APR库函数.附带说明一下,我正在Ubuntu虚拟机上运行它.文中给出的解决方案说,要更改配置文件,然后运行ldconfig"以帮助链接程序找到适当的库.
我对ldconfig的手册页不够了解,无法正确使用该功能.如何正确运行ldconfig?

This makefile did not seem to work as when I used "$make devpkg" not all of the APR library functions were declared. As a side note I am running this on a Ubuntu virtual machine. A solution given in the text says to alter a config file and then "run ldconfig" to help the linker find the appropriate library.
I do not understand the man page for ldconfig well enough to correctly utilize the function. How do run ldconfig correctly?

经过一些挖掘,我还发现了一个参考,该参考在makefile中使用"LDLIBS"而不是"LDFLAGS"解决了该问题.我更改了makefile,并编译了程序.

Also after some digging I found a reference that using "LDLIBS" instead of "LDFLAGS" in the makefile fixed the problem. I altered the makefile and the program compiled.

允许C编译器正确链接到APR库的"LDFLAGS"和"LDLIBS"之间有什么区别?在某处是否有一些方便的命令列表,可以帮助我更好地了解如何正确生成makefile?

What is the difference between "LDFLAGS" and "LDLIBS" that allowed the C compiler to correctly link to the APR library? Is there a handy list of commands somewhere that can help me better understand how a makefile is correctly generated?

感谢您的时间.

推荐答案

来自 GNU Make手册 10.2隐式规则目录:

链接单个目标文件
通过C编译器运行链接器(通常称为 ld ),可以从 n.o 自动创建 n .精确的配方是" $(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS) ".

Linking a single object file
n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is '$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)'.

如您所见,LDFLAGS在目标文件之前,LDLIBS在目标文件之前.有时,顺序可能很重要-显然对您而言确实如此.

As you can see, LDFLAGS comes before your object file and LDLIBS after. Sometimes that order can matter - apparently it does in your case.

编者注:尽管有时使用make的隐式规则支持可能会很方便,但在使用过程中,它几乎总是会更加混乱.我敦促您编写一个完整的makefile-它可以帮助您了解正在发生的事情,并希望将来避免此类问题.

Editorial note: While it might sometimes be convenient to use make's implicit rule support, it almost always ends up more confusing down the road. I'd urge you to write a complete makefile - it'll help you understand what's going on better and hopefully avoid this sort of problem in the future.

这篇关于图书馆连结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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