1

我想将 curlpp 添加到我的 C++ 项目中。目前,我有一个 main.cpp 文件,如下所示:

#include <iostream> 
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

int main() {
  return 0; 
}

我使用: “g++ -std=c++14 -I/usr/nguyenthesang/Desktop/myprogram/curlpp-0.8.1/include main.cpp”编译,它成功编译。

然后我在主函数中添加实现(下面是从 curlpp 的 repo 复制的):

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>


using namespace curlpp::options;

int main(int, char **)
{
    try
    {
    // That's all that is needed to do cleanup of used resources (RAII 
    style).
    curlpp::Cleanup myCleanup;

    // Our request to be sent.
    curlpp::Easy myRequest;

    // Set the URL.
    myRequest.setOpt<Url>("http://example.com");

    // Send request and get a result.
    // By default the result goes to standard output.
    myRequest.perform();
}

catch(curlpp::RuntimeError & e)
{
    std::cout << e.what() << std::endl;
}

catch(curlpp::LogicError & e)
{
    std::cout << e.what() << std::endl;
}

return 0;
}

当我使用"g++ -std=c++14 -I/usr/nguyenthesang/Desktop/myprogram/curlpp-0.8.1/include main.cpp"进行编译时,会出现编译错误,即“ ld: symbol( s) 未找到架构 x86_64 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) “。

错误可能来自我只将头文件链接到程序而不是库本身的事实。我四处搜索以找到链接库的方法(例如使用 -L 选项),但它不起作用。我需要帮助解决这个问题。

我还想问一下,有没有一种通用的方法可以将每个库添加到 C++ 项目中,比如 iOS 中的 Cocoapods?

我感谢您的帮助。

4

1 回答 1

1

在 ubuntu 上,我成功安装了这些软件包:

sudo apt-get install pkg-config libcurlpp-dev libcurl4-openssl-dev

(pkg-config 将在 CMakeLists.txt 中用于查找 curlpp 并设置指向它的 env var)
然后在我的 CMakeLists.txt 中添加:

include(FindPkgConfig)
pkg_check_modules(CURLPP REQUIRED curlpp)

然后仍然在 CMakeLists.txt 添加到 target_link_libraries ${CURLPP_LDFLAGS} 例如:

target_link_libraries(mylibcurlppprog ${CURLPP_LDFLAGS})
于 2018-08-14T11:39:42.557 回答