libcpr / cpr

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

Link error when setting -DCMAKE_BUILD_TYPE=Debug #1017

Closed Zhaoyilunnn closed 8 months ago

Zhaoyilunnn commented 8 months ago

Description

Link error when setting -DCMAKE_BUILD_TYPE=Debug, I have followed the instructions to link lipcpr to my project, see: https://github.com/Zhaoyilunnn/quafu-cpp/blob/main/CMakeLists.txt

in function `void cpr::priv::set_option_internal<false, std::__debug::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, cpr::CaseInsensitiveCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&>(cpr::Session&, std::__debug::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, cpr::CaseInsensitiveCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)':

build/_deps/cpr-src/include/cpr/api.h:30: undefined reference to `cpr::Session::SetOption(std::__debug::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, cpr::CaseInsensitiveCompare, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'

Example/How to Reproduce

git clone https://github.com/Zhaoyilunnn/quafu-cpp.git
mkdir build
cmake -DCMAKE_BUILD_TYPE=Debug ..
make

This will produce the link error, however, compilation runs successfully through cmake .. without setting CMAKE_BUILD_TYPE

Possible Fix

No response

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

COM8 commented 8 months ago

@Zhaoyilunnn thanks for reporting. To me this looks like you are linking not correctly. You are linking as INTERFACE. Try linking PRIVATE or PUBLIC since it's not a header only library. Here is an example: https://github.com/libcpr/example-cmake-fetch-content/blob/main/CMakeLists.txt

Zhaoyilunnn commented 8 months ago

@Zhaoyilunnn thanks for reporting. To me this looks like you are linking not correctly. You are linking as INTERFACE. Try linking PRIVATE or PUBLIC since it's not a header only library. Here is an example: https://github.com/libcpr/example-cmake-fetch-content/blob/main/CMakeLists.txt

Hi @COM8 thanks a lot for your quick response. However, I have tried using PRIVATE but faced the same error.

Moreover, the error acutally happens when linking cpr against my CLIENT_TEST executable, where I originally use PUBLIC instead of INTERFACE. And this link error only happens when I set CMAKE_BUILD_TYPE to Debug (it builds successfully when not setting debug mode). So I guess the INTERFACE setting is not the root cause.

Zhaoyilunnn commented 8 months ago

Finally I find the root cause is the same as this issue: https://github.com/libcpr/cpr/issues/358.

Some other dependency of my projects may set D_GLIBCXX_DEBUG, which caused link error of cpr.

I manually remove this flag in my project by string(REPLACE "-D_GLIBCXX_DEBUG" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") and build successfully.

@COM8 could you help investigate the reason why we cannot enable D_GLIBCXX_DEBUG when debugging cpr-dependent projects?

Thanks

Zhaoyilunnn commented 8 months ago

I find that this only impacts building unit tests using googletest, building other executables without linking to googletest works fine: https://github.com/Zhaoyilunnn/quafu-cpp/tree/main/examples/01_simple. Maybe CPR is not compatible with googletest in debug mode