libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.29k stars 903 forks source link

Link error when creating a cpr::Header, segment fault when making cpr::Get request #1029

Closed JuliusSchwartz closed 2 months ago

JuliusSchwartz commented 4 months ago

I'm trying to use cpr in a small CLion project on MacOS where I make http requests to a local server. My CMakeFiles.txt looks as follows :

cmake_minimum_required(VERSION 3.21)

project(HashClient)

set(CMAKE_CXX_STANDARD 20)

find_package(cpr CONFIG REQUIRED)

add_executable(HashClient main.cpp)
target_link_libraries(HashClient PRIVATE cpr::cpr)

and my main.cpp file looks as follows:

#include <iostream>
#include <string>
#include "cpr/cpr.h"

int main()
{

    cpr::Url URL{"http://localhost:3000"};
    cpr::Header header{{"Content-Type", "application/json"}};   // This line gives me link errors

    cpr::Response response{cpr::Get(URL)};  // This line gives me a seg fault

    return 0;
}

If I leave in the line where I create a cpr::Header object, I get strange link errors:

====================[ Build | HashClient | Debug ]==============================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/<my_name>/MyStuff/HashClient/cmake-build-debug --target HashClient
[2/2] Linking CXX executable HashClient
FAILED: HashClient 
: && /usr/local/Cellar/gcc/13.1.0/bin/g++-13 -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/HashClient.dir/main.cpp.o -o HashClient  -Wl,-rpath,/usr/local/lib  /usr/local/lib/libcpr.1.11.0.dylib  ../vcpkg/installed/x64-osx/debug/lib/libcurl-d.a  -framework SystemConfiguration  -framework Security  -framework CoreFoundation -framework CoreServices  ../vcpkg/installed/x64-osx/debug/lib/libz.a && :
Undefined symbols for architecture x86_64:
  "__ZNK3cpr22CaseInsensitiveCompareclERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_", referenced from:
      __ZNSt8_Rb_treeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt4pairIKS5_S5_ESt10_Select1stIS8_EN3cpr22CaseInsensitiveCompareESaIS8_EE29_M_get_insert_hint_unique_posESt23_Rb_tree_const_iteratorIS8_ERS7_ in main.cpp.o
      __ZNSt8_Rb_treeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt4pairIKS5_S5_ESt10_Select1stIS8_EN3cpr22CaseInsensitiveCompareESaIS8_EE10_M_insert_IRKS8_NSE_11_Alloc_nodeEEESt17_Rb_tree_iteratorIS8_EPSt18_Rb_tree_node_baseSM_OT_RT0_ in main.cpp.o
      __ZNSt8_Rb_treeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt4pairIKS5_S5_ESt10_Select1stIS8_EN3cpr22CaseInsensitiveCompareESaIS8_EE24_M_get_insert_unique_posERS7_ in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

If I don't bother with headers and try to make a simple get request, I get a segment fault Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) (with this being to an address I can navigate to in my browser).

I've got no idea what's gone wrong here. I've initially installed cpr using vcpgk (following the instructions in the readme), and when I encountered issues with that, I also cloned the cpr repo and built it locally (again, following the instructions in the readme). After googling around a little I also tried adding the lines

find_package(curl CONFIG REQUIRED)
target_link_libraries(HashClient PRIVATE cpr::cpr CURL::libcurl)

to my cmakeslists.txt but this didn't change anything.

Where did you get it from?

vcpkg

Additional Context/Your Environment

MacOS Ventura 13.6

COM8 commented 3 months ago

Sorry for the delayed response. I've been on vacation :)

Does the following example compile for you? https://github.com/libcpr/example-cmake-fetch-content