set(CMAKE_CXX_STANDARD 14) does not automatically link against stdc++ in linux

79 Views Asked by At

I successfully build and execute a tgbot example for both Windows (Visual Studio 2022) and Linux (gcc 11) using the following CMakeLists.txt.

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
    include_directories(${CURL_INCLUDE_DIRS})
    add_definitions(-DHAVE_CURL)
endif()

add_executable(tgbotExample1 echobot.cpp)
target_link_libraries(tgbotExample1 PRIVATE TgBot)
if (LINUX)
    target_link_libraries(tgbotExample1 PRIVATE stdc++ ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
endif ()

The CMakeLists.txt provided by the repo does not link against stdc++ but for my case if I don't include it target_link_libraries(tgbotExample1 PRIVATE stdc++ ..., I get many link errors.

# Original
target_link_libraries(echobot /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})

# versus Mine
target_link_libraries(tgbotExample1 PRIVATE stdc++ ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})

My question is:

  1. isn't the job of set(CMAKE_CXX_STANDARD 14) to use stdc++ for linking? Why do I have to include it but the original CMakeLists.txt does not have it?
  2. How the original CMakeLists.txt will link against stdc++ without explicitly mentioning it?
0

There are 0 best solutions below