将curlpp添加到CMake项目中。

4
我正在使用curlcpp和cmake在C++中进行项目开发,但是我无法编译该项目。
我对CMakeLists不熟悉,很难理解如何添加所需内容以使项目正常工作。通常情况下,我只是复制其他帖子中的内容,而不真正理解它们的工作原理;教程和文档通常要么太基础,要么太复杂。更糟糕的是,我有这样一种印象,即每个库的添加方式都不同,因此当需要添加新库时,我唯一的办法是搜索以前的帖子并询问如何添加。在这种情况下,我找不到任何有用的信息。
那么我已经做了什么呢:
在使用homebrew安装curlpp和curl之后
brew install curl
brew install curlpp

I write my project:

Cmakelists.txt:

cmake_minimum_required(VERSION 3.0)

project(stock_analysis)

set(CMAKE_CXX_FLAGS "-g -Wall -std=c++11")


######## BOOST STUFF ########
#DELETE NEXT LINE
set(Boost_NO_BOOST_CMAKE ON)

# set(Boost_USE_STATIC_LIBS        ON) # only find static libs
# set(Boost_USE_MULTITHREADED      ON)
# set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost COMPONENTS
                   filesystem
)

######## CURL STUFF ########
include(FindCURL)
find_package(CURL REQUIRED)
if(CURL_FOUND)
 message(STATUS "Found CURL version: ${CURL_VERSION_STRING}")
 message(STATUS "Using CURL include dir(s): ${CURL_INCLUDE_DIRS}")
 message(STATUS "Using CURL lib(s): ${CURL_LIBRARIES}")
else()
 message(FATAL_ERROR "Could not find CURL")
endif()




SET(HEADERS
   include
)

SET(SOURCE_FILES
  src/main.cpp
  src/analyse.cpp
  src/helpers.cpp
  src/simulation.cpp
  src/data.cpp
)

include_directories(${HEADERS}
                   ${Boost_INCLUDE_DIRS}
                   ${CURL_INCLUDE_DIRS}
)

add_executable(app ${SOURCE_FILES})

target_link_libraries(app
       ${Boost_LIBRARIES}
       ${CURL_LIBRARIES}
)

data.cpp中的函数(我基本上复制了curlcpp网页上的示例):

#include "data.h"

#include <iostream>

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

using namespace curlpp::options;


void fetchData(){

  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;
    }
}

The error I get:

Undefined symbols for architecture x86_64:
  "curlpp::OptionBase::OptionBase(CURLoption)", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Option(CURLoption, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in data.cpp.o
  "curlpp::OptionBase::~OptionBase()", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Option(CURLoption, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in data.cpp.o
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~Option() in data.cpp.o
  "curlpp::UnsetOption::UnsetOption(char const*)", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::updateMeToOption(curlpp::OptionBase const&) in data.cpp.o
      curlpp::OptionTrait<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, (CURLoption)10002>::updateHandleToMe(curlpp::internal::CurlHandle*) const in data.cpp.o
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::getValue() const in data.cpp.o
  "curlpp::RuntimeError::~RuntimeError()", referenced from:
      curlpp::UnsetOption::~UnsetOption() in data.cpp.o
  "curlpp::libcurlRuntimeAssert(char const*, CURLcode)", referenced from:
      void curlpp::internal::CurlHandle::option<void*>(CURLoption, void*) in data.cpp.o
  "curlpp::Easy::perform()", referenced from:
      StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
  "curlpp::Easy::Easy()", referenced from:
      StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
  "curlpp::Easy::~Easy()", referenced from:
      StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
  "curlpp::Cleanup::Cleanup()", referenced from:
      StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
  "curlpp::Cleanup::~Cleanup()", referenced from:
      StockAnalysis::fetchData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool const&) in data.cpp.o
  "curlpp::OptionBase::operator<(curlpp::OptionBase const&) const", referenced from:
      vtable for curlpp::OptionTrait<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, (CURLoption)10002> in data.cpp.o
      vtable for curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in data.cpp.o
  "typeinfo for curlpp::LogicError", referenced from:
      GCC_except_table0 in data.cpp.o
  "typeinfo for curlpp::OptionBase", referenced from:
      curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::updateMeToOption(curlpp::OptionBase const&) in data.cpp.o
      typeinfo for curlpp::Option<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > in data.cpp.o
  "typeinfo for curlpp::RuntimeError", referenced from:
      GCC_except_table0 in data.cpp.o
      typeinfo for curlpp::UnsetOption in data.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [app] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2

我之前跟随了下面的帖子: 使用CMake将curl链接到项目中 在cmakelists中添加curlcpptarget_link_libraries()只会出现另一个错误。

ld: library not found for -lcurlpp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [app] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2

顺便说一下,我不明白为什么curlcpp会突然出现,而且没有传统的cmakelists符号${Some_variable}


您需要链接cURL库,而不是独立于cURL的cURLpp库。 - Some programmer dude
1
@squareskittles 我尝试按照链接中的信息,但它不起作用。我会编辑问题并添加错误信息。 - Alexis
1
我觉得你可能想要将这个库导入到CMake中,或者至少告诉CMake在哪里寻找已安装的库。请参考这个问题的答案。 - Kevin
这个解决方案实际上是有效的,但它既不优雅也不可移植。调用link_directories(/usr/local/Cellar/curlpp/0.8.1/lib/),这是homebrew安装库的位置,可以使编译工作正常进行。然而,我仍然不明白为什么我必须输入target_link_libraries curlpp而不是库的名称libcurlpp(libcurlpp.a和libcurlpp.dylib可以在路径上找到)。 - Alexis
1
很抱歉,@Alexis,curlpp库不支持与CMake一起使用。你必须要么为该库添加支持,要么编写一个查找模块文件。这两个任务都不容易,但你可以在这里找到更多信息:It's Time To Do CMake Right - Guillaume Racicot
@GuillaumeRacicot 谢谢您的反馈。我会研究一下。 - Alexis
1个回答

2
使用curlpp的最简代码示例如下:
cmake_minimum_required(VERSION 3.14)
project(testCurlPP LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FindPkgConfig)
pkg_check_modules(CURLPP REQUIRED curlpp)

add_executable(testCurlPP
  main.cpp
)
target_link_libraries(testCurlPP
    ${CURLPP_LDFLAGS}
    )

注意:在项目中使用curlpp之前,请不要忘记构建和安装它。

构建(使用最新版本)curlpp

mkdir build
cd build
cmake ..
make -j8
sudo make install

将其位置添加到环境变量中:

LD_LIBRARY_PATH=/usr/local/lib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接