gpt4 book ai didi

c++ - 将 curlpp 添加到 cmake 项目

转载 作者:行者123 更新时间:2023-12-05 06:22:38 25 4
gpt4 key购买 nike

我想在 curlcpp 和 cmake 的帮助下用 C++ 做一个项目,但我无法编译该项目。

我是 CMakeLists 的新手,很难理解如何添加使项目正常运行所需的内容。我基本上通常会在没有真正理解它们是如何工作的情况下复制我在其他帖子中可以找到的内容;教程和文档通常要么过于基础,要么过于复杂。更糟糕的是,我的印象是每个库的添加方式都不同,所以当有新的库要添加时,我必须添加的唯一方法是搜索以前的帖子询问如何做。在这种情况下,我找不到任何对我有帮助的东西。

所以这是我所做的:

用 homebrew 安装 curlpp 和 curl 之后

brew install curl
brew install curlpp

我写我的项目:

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

我得到的错误:

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

我之前关注过下一个话题: Linking curl in a project using CMake

在 cmakelists 中将 curlcpp 添加到 target_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}

最佳答案

像下面这样使用 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

关于c++ - 将 curlpp 添加到 cmake 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59056392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com