我正在尝试编译C程序,同时链接APR库。 我收到以下错误消息:
cc -g -Wall -pthread -I/usr/local/apr/include/apr-1 -I/usr/local/apr/include/apr-util-1 -L/usr/local/apr/lib -L .aprutil-1 -L .apr-1 devpkg.c bstrlib.o db.o shell.o commands.o -o devpkg /tmp/cczC53x5.o: In function `main': /home/yotam/Dropbox/Development/C/devpkg/devpkg.c:14: undefined reference to `apr_pool_initialize' /home/yotam/Dropbox/Development/C/devpkg/devpkg.c:15: undefined reference to `apr_pool_create_ex' /home/yotam/Dropbox/Development/C/devpkg/devpkg.c:29: undefined reference to `apr_getopt_init' /home/yotam/Dropbox/Development/C/devpkg/devpkg.c:31: undefined reference to `apr_getopt' db.o: In function `DB_init': /home/yotam/Development/C/devpkg/db.c:89: undefined reference to `apr_pool_initialize' /home/yotam/Development/C/devpkg/db.c:90: undefined reference to `apr_pool_create_ex' /home/yotam/Development/C/devpkg/db.c:93: undefined reference to `apr_dir_make_recursive' /home/yotam/Development/C/devpkg/db.c:105: undefined reference to `apr_pool_destroy' /home/yotam/Development/C/devpkg/db.c:109: undefined reference to `apr_pool_destroy' shell.o: In function `Shell_exec': /home/yotam/Development/C/devpkg/shell.c:16: undefined reference to `apr_pool_create_ex' /home/yotam/Development/C/devpkg/shell.c:38: undefined reference to `apr_pool_destroy' /home/yotam/Development/C/devpkg/shell.c:44: undefined reference to `apr_pool_destroy' shell.o: In function `Shell_run': /home/yotam/Development/C/devpkg/shell.c:55: undefined reference to `apr_procattr_create' /home/yotam/Development/C/devpkg/shell.c:58: undefined reference to `apr_procattr_io_set' /home/yotam/Development/C/devpkg/shell.c:62: undefined reference to `apr_procattr_dir_set' /home/yotam/Development/C/devpkg/shell.c:65: undefined reference to `apr_procattr_cmdtype_set' /home/yotam/Development/C/devpkg/shell.c:68: undefined reference to `apr_proc_create' /home/yotam/Development/C/devpkg/shell.c:71: undefined reference to `apr_proc_wait' commands.o: In function `Command_fetch': /home/yotam/Development/C/devpkg/commands.c:44: undefined reference to `apr_uri_parse' /home/yotam/Development/C/devpkg/commands.c:48: undefined reference to `apr_fnmatch' /home/yotam/Development/C/devpkg/commands.c:51: undefined reference to `apr_fnmatch' /home/yotam/Development/C/devpkg/commands.c:70: undefined reference to `apr_fnmatch' /home/yotam/Development/C/devpkg/commands.c:78: undefined reference to `apr_dir_make_recursive' /home/yotam/Development/C/devpkg/commands.c:84: undefined reference to `apr_fnmatch' /home/yotam/Development/C/devpkg/commands.c:90: undefined reference to `apr_dir_make_recursive' collect2: error: ld returned 1 exit status make: *** [devpkg] Error 1
这是我的makefile,它应该能够在不同的计算机上编译,其中PREFIX
变量是相对于计算机的位置。
(这个程序本质上应该有一天可以移植到任何操作系统。现在我只想能够成功编译它)
PREFIX?=/usr/local LDFLAGS= -L${PREFIX}/apr/lib -L .aprutil-1 -L .apr-1 CFLAGS=-g -Wall -pthread -I${PREFIX}/apr/include/apr-1 -I${PREFIX}/apr/include/apr-util-1 all: devpkg devpkg: bstrlib.o db.o shell.o commands.o install: all \ install -d $(DESTDIR)/$(PREFIX)/bin/ \ install devpkg $(DESTDIR)/$(PREFIX)/bin/ clean: rm -f *.o \ rm -f devpkg \ rm -rf *.dSYM
我已经离开并自己在文件夹中搜索它,这就是我得到的:
yotam@yotam-HP-ProBook-450://usr$ grep -r apr_pool_initialize . ./local/apr/include/apr-1/apr_pools.h:APR_DECLARE(apr_status_t) apr_pool_initialize(void); Binary file ./local/apr/lib/libapr-1.so.0.4.6 matches Binary file ./local/apr/lib/libapr-1.a matches Binary file ./local/apr/lib/libapr-1.so.0.5.1 matches Binary file ./local/apr/lib/libapr-1.so.0.4.5 matches ./local/apr/lib/apr.exp:apr_pool_initialize
在我做了之后,我清楚地知道这是一个链接器问题。但是我无法找到执行这个技巧的命令。
提前致谢。
答案 0 :(得分:4)
您好像缺少告诉cc
要链接的库的链接选项。我不确定您.aprutil-1
宏中的.apr-1
和LDFLAGS
是什么,因为您使用-L指定它们,我会假设它们是目录。
但是,如果您更改行以添加-l
选项以指定库,则应该让您更接近。
编辑:因为链接器是单通道工具,所以需要在对象之后列出所有库,因此符号不会被优化掉。 LDLIBS
是指定库的宏的典型名称,您可以将它们添加到compile / link命令的末尾,它应该可以工作。
LDFLAGS= -L${PREFIX}/apr/lib -L .aprutil-1 -L .apr-1
LDLIBS= -lapr-1 -laprutil-1
最后两个args告诉链接器要使用哪些库名称(前缀为 lib 并附加 .a )。 -L
选项指定要搜索库的其他目录,它实际上不包含任何内容。
或者,您也可以将LD_LIBRARY_PATH
设置为指向库目录。
如果您需要更多帮助调试,可以参考this site(以及其他)作为参考。
答案 1 :(得分:0)
尝试用LDLIBS替换LDFLAGS,如:
LDLIBS=-L${PREFIX}/apr/lib -lapr-1 -pthread -laprutil-1
而不是:
LDFLAGS=-L${PREFIX}/apr/lib -lapr-1 -pthread -laprutil-1
这是因为LDFLAGS变量适用于链接器的非库选项,而在这种情况下,您使用的是库。