c++ - 在 makefile 中链接库

标签 c++ boost curl makefile

我在简单的 makefile 中链接库 boost 和 curl 时遇到问题。当我使用单个命令时:

g++ -I /usr/lib/boost/include -L /usr/lib/boost/lib website.cpp website.h main.cpp data.cpp data.h -lboost_regex -lcurl

一切正常,但是当我使用 makefile 时

a.out: website.o data.o main.o
    g++ -L /usr/lib/boost/lib -lboost_regex website.o data.o main.o -o a.out -lcurl 
data.o: data.cpp data.h
    g++ -c data.cpp -o data.o
website.o: website.cpp website.h
    g++ -I /usr/lib/boost/include -c website.cpp -o website.o 
main.o: main.cpp
    g++ -c main.cpp -o main.o

我有很多错误:

g++ -c main.cpp -o main.o 
g++ -L /usr/lib/boost/lib -lboost_regex website.o data.o main.o -o a.out -lcurl
website.o: In function boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
website.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)]+0x22): undefined reference to `boost::basic_

我在website.cpp中使用了boost和curl(当然头条在website.h中)

最佳答案

问题是 -lboost_regex 被放置在编译器命令中的 .o 之前,不像你的单个命令有 -lboost_regex在最后。

改变:

g++ -L /usr/lib/boost/lib -lboost_regex website.o data.o main.o -o a.out -lcurl

到:

g++ -L/usr/lib/boost/lib website.o data.o main.o -o a.out -lcurl -lboost_regex

参见 GCC Link Options解释为什么顺序很重要。

关于c++ - 在 makefile 中链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511043/

相关文章:

c++ - future 是检查单个线程完成的安全方法吗?

c++11 - 使用自定义张量数据结构的 boost::odeint 输出错误

php - OpenSSL header 版本 != 影响 APNS 的 HTTP/2 的 OpenSSL 库版本

c++ - 应该允许 std::unique_ptr<void>

c++ - 运算符重载、名称解析和命名空间

C++ 错误 : redefinition of class

c++ - Boost SSL-Server 因 SSLv3 错误而失败

c++ - Windows.h 宏未定义

php - 如何在PHP中使用curl GET发送原始数据?

php - 可以通过浏览器查看 URL,但不能使用 CURL 查看 URL