当我尝试链接测试文件时出现文件错误

时间:2013-12-26 09:02:17

标签: linux compiler-construction makefile shared-objects

我写了一个简单的make文件

test : main.cpp
    g++ -Wall -o $@ $^ -lhello_ext -L. -I/usr/include/python2.7/   -lboost_regex -lpython2.7 -ggdb
libhello_ext.so : hello_ext.o
    g++ -shared -o $@ $^ -Lboost-lib-path -I/usr/include/python2.7/   -lboost_regex -lpython2.7 -ggdb
hello_ext.o : hello_ext.cpp
    g++ -c -Wall -Werror -fpic -o $@ $^ -I/usr/include/python2.7/   -lboost_regex -lpython2.7 -ggdb
.PHONY : clean
clean:
    rm -rf test lib* *.o *.swf

但是当我做一个make我得到一个错误

/usr/bin/ld: cannot find -lhello_ext
collect2: error: ld returned 1 exit status
make: *** [test] Error 1

前两行中的问题,当我从make文件中删除这些命令并将手动方式写入终端时,它工作正常。也就是说,从我做的终端

g++ -Wall -o $@ $^ -lhello_ext -L. -I/usr/include/python2.7/   -lboost_regex -lpython2.7 -ggdb

一切正常。有什么问题?

1 个答案:

答案 0 :(得分:0)

当你直接执行你的行时,$@ $^可能是空的(使用echo自行检查)。在这种情况下,参数-lhello_ext不会被解释为要添加的库的链接器命令,而是解释为对象文件的输出名称。这就是它在命令行上工作的原因。

至于为什么它在Makefile中不起作用:我的猜测是你的库hello_ext位于当前目录中。要让链接器找到它,您需要在添加库之前添加库路径信息-L.