0

I read a few questions and tried their suggestions but did not work for me.

I'm trying to build a program that uses #include <boost/asio/ssl.hpp> header using CMake. My CMakeLists.txt file is:

cmake_minimum_required(VERSION 3.16)

project(Test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

add_executable(myapp
   main.cpp
)

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS system REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(myapp ${BOOST_LIBRARIES})

When I compile this (using cmake . followed by ninja) I get a whole lot of undefined reference errors related to SSL. After reading several questions, I tried something like

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -crypto -ssl")

However, then I get the error:

clang-9: error: unknown argument '-crypto'; did you mean '-mcrypto'?
clang-9: error: unknown argument: '-ssl'

The referenced question lead me the right way. For those who might get a similar issue, you need to link against OpenSSL. So the final CMakeLists.txt becomes:

cmake_minimum_required(VERSION 3.16)

project(Test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

add_executable(myapp
   main.cpp
)

find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIRS})
target_link_libraries(myapp ${OPENSSL_LIBRARIES})

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS system REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(myapp ${BOOST_LIBRARIES})
kovac
  • 4,945
  • 9
  • 47
  • 90
  • I have replaced the duplicate reference to one about OpenSSL specifically. – Tsyvarev Apr 10 '20 at 10:58
  • @Tsyvarev I saw your previous deleted comment about linking lib. That seems to work too. Which is better? So should I use OpenSSL or is it better to link crypto and ssl? – kovac Apr 10 '20 at 10:59
  • 1
    If `find_package` is available for the library, then its usage is **preferred**: it usually provides platform-independent way, and sometimes cares about dependencies. Linking with a plain name of the library is used only as the last resort, when other ways are not supported for this library. – Tsyvarev Apr 10 '20 at 12:18

0 Answers0