伙计们,我在这里面临一个小问题。基本上,我正在尝试使用CMake构建这个出色的库https://github.com/socketio/socket.io-client-cpp

首先,我根据需要安装了boost,它进入了opt / local / include / boost和opt / local / lib。但是问题是CMake找不到所需的Boost库。在Configure CMake给我该日志后:

not define build type, set to release
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.6/Modules/FindBoost.cmake:1753 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.59.0

  Boost include path: /opt/local/include

  Could not find the following static Boost libraries:

          boost_date_time
          boost_random

  Some (but not all) of the required Boost libraries were found.  You may
  need to install these additional Boost libraries.  Alternatively, set
  BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
  to the location of Boost.
Call Stack (most recent call first):
  CMakeLists.txt:23 (find_package)


遇到此问题后,我尝试尝试手动设置BOOST_LIBRARYDIR,但这也不起作用。

这是CMake文件,该文件与我尝试编译失败的库一起提供:

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
PROJECT(sioclient)

option(BUILD_SHARED_LIBS "Build the shared library" OFF)
option(Boost_USE_STATIC_LIBS "Use Boost static version" ON)

set(MAJOR 1)
set(MINOR 6)
set(PATCH 0)

if(NOT CMAKE_BUILD_TYPE )
MESSAGE(STATUS "not define build type, set to release" )
set(CMAKE_BUILD_TYPE Release )
elseif(NOT (${CMAKE_BUILD_TYPE} STREQUAL "Release" OR ${CMAKE_BUILD_TYPE} STREQUAL "Debug" ))
MESSAGE(SEND_ERROR "CMAKE_BUILD_TYPE must be either Release or Debug")
return()
endif()

set(BOOST_VER "1.55.0" CACHE STRING "boost version" )

set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost ${BOOST_VER} REQUIRED COMPONENTS system date_time random)

aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src ALL_SRC)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src/internal ALL_SRC)
file(GLOB ALL_HEADERS ${CMAKE_CURRENT_LIST_DIR}/src/*.h )
set(SIO_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR})

add_library(sioclient ${ALL_SRC})
target_include_directories(sioclient PRIVATE ${Boost_INCLUDE_DIRS}
    ${CMAKE_CURRENT_LIST_DIR}/src
    ${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp
    ${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/include
)

set_property(TARGET sioclient PROPERTY CXX_STANDARD 11)
set_property(TARGET sioclient PROPERTY CXX_STANDARD_REQUIRED ON)
target_link_libraries(sioclient PRIVATE ${Boost_LIBRARIES})
if(BUILD_SHARED_LIBS)
set_target_properties(sioclient
    PROPERTIES
        SOVERSION ${MAJOR}
        VERSION ${MAJOR}.${MINOR}.${PATCH}
    )
endif()
list(APPEND TARGET_LIBRARIES sioclient)

find_package(OpenSSL)
if(OPENSSL_FOUND)
add_library(sioclient_tls ${ALL_SRC})
target_include_directories(sioclient_tls PRIVATE ${Boost_INCLUDE_DIRS}
    ${CMAKE_CURRENT_LIST_DIR}/src
    ${CMAKE_CURRENT_LIST_DIR}/lib/websocketpp
    ${CMAKE_CURRENT_LIST_DIR}/lib/rapidjson/include
    ${OPENSSL_INCLUDE_DIR}
)

set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD 11)
set_property(TARGET sioclient_tls PROPERTY CXX_STANDARD_REQUIRED ON)
target_link_libraries(sioclient_tls PRIVATE ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} )
target_compile_definitions(sioclient_tls PRIVATE -DSIO_TLS)
if(BUILD_SHARED_LIBS)
set_target_properties(sioclient_tls
    PROPERTIES
        SOVERSION ${MAJOR}
        VERSION ${MAJOR}.${MINOR}.${PATCH}
    )
endif()
list(APPEND TARGET_LIBRARIES sioclient_tls)

endif()

install(FILES ${ALL_HEADERS}
    DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/include"
)

install(TARGETS ${TARGET_LIBRARIES}
    DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}"
)

install(FILES ${Boost_LIBRARIES}
    DESTINATION "${CMAKE_CURRENT_LIST_DIR}/build/lib/${CMAKE_BUILD_TYPE}"
)


谢谢大家的帮助!还有一个观察:如果我没有记错,它会尝试在boost_random文件夹中找到include时,将其命名为random。也许那是问题吗?

最佳答案

我想,您还没有构建Boost库的静态版本。

如果使用Boost.Build(b2程序)构建了Boost,则应使用link=static,shared(运行b2 link=static,shared <other_flags>)标志集对其进行重新编译,以便同时构建动态库和静态库。

upd:另外,该项目的自述文件指出,您应该在指定了适当Boost版本的cmake -DBOOST_ROOT:STRING=/opt/local -DBOOST_VER:STRING=<your boost version> ./下运行CMake。

关于c++ - 使用Boost库构建socket.io C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41473458/

10-17 01:32