我对CMake和使用C ++框架相当新,并且无法设置我的项目。当我尝试构建它时,我收到以下错误
/Users/adjust/Documents/C++/WebService/main.cpp:7:10: fatal error: 'restbed' file not found
我将restbed repo克隆到我的项目文件夹WebService中。 Build没有问题,在restbed.build中,make test成功达到了100%。
对问题进行排查并没有带来成功(但是)所以我希望通过我的答案解决方案,其他新手也可能在未来得到帮助。
我在OS X和我的CMakeList.txt文件中使用了CLion,如下所示。
cmake_minimum_required(VERSION 3.7)
project(WebService)
#Set Version
set (WebService_VERSION_MAJOR 1)
set (WebService_VERSION_MAJOR 0)
#Setup Asio
find_path( asio_INCLUDE asio.hpp HINTS "${PROJECT_SOURCE_DIR}/dependency/asio/asio/include" "/usr/include" "/usr/local/include" "/opt/local/include" )
if ( asio_INCLUDE )
set( ASIO_FOUND TRUE )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DASIO_STANDALONE=YES" )
message( STATUS "${Green}Found ASIO include at: ${asio_INCLUDE}${Reset}" )
else ( )
message( FATAL_ERROR "${Red}Failed to locate ASIO dependency.${Reset}" )
endif ( )
set(CMAKE_CXX_STANDARD 11)
#Setup OpenSSl
find_library( ssl_LIBRARY ssl ssleay32 HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/out32dll" "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
find_library( crypto_LIBRARY crypto libeay32 HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/out32dll" "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
find_path( ssl_INCLUDE openssl/ssl.h HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/inc32" "${PROJECT_SOURCE_DIR}/dependency/openssl/include" "/usr/local/opt/openssl/include" "/usr/include" "/usr/local/include" "/opt/local/include" )
if ( ssl_INCLUDE AND ssl_LIBRARY AND crypto_LIBRARY )
set( OPENSSL_FOUND TRUE )
add_definitions( -DBUILD_SSL=TRUE )
if ( APPLE AND BUILD_SSL )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations" )
endif( )
message( STATUS "${Green}Found OpenSSL library at: ${ssl_LIBRARY}${Reset}" )
message( STATUS "${Green}Found OpenSSL include at: ${ssl_INCLUDE}${Reset}" )
message( STATUS "${Green}Found Crypto library at: ${crypto_LIBRARY}${Reset}" )
else ( )
message( FATAL_ERROR "${Red}Failed to locate OpenSSL dependency. see restbed/dependency/openssl; ./config shared; make all${Reset}" )
endif ( )
# RestBED
set( restbed_SOURCE "${PROJECT_SOURCE_DIR}/restbed" )
if ( restbed_SOURCE )
set( restbed_FOUND TRUE )
set( restbed_BUILD "${CMAKE_CURRENT_BINARY_DIR}/restbed_build" )
set( restbed_DISTRIBUTION "${CMAKE_CURRENT_BINARY_DIR}/distribution" )
set( restbed_INCLUDE "${restbed_DISTRIBUTION}/include" )
set( restbed_LIBRARY "${restbed_DISTRIBUTION}/library" )
message( STATUS "${Green}Found Restbed include at: ${restbed_SOURCE}${Reset}" )
else ( )
message( FATAL_ERROR "${Red}Failed to locate Restbed dependency.${Reset}" )
endif ( )
# find all files from restbed library
file(GLOB_RECURSE restbed_LIBRARY_FILES
"${restbed_LIBRARY}/*.a")
include_directories(${restbed_INCLUDE})
add_executable(WebService main.cpp)
target_link_libraries(WebService ${restbed_LIBRARY_FILES})
我明白我只需要库和包含分发文件夹。我做错了什么?
我发现了错误,只需确保您设置路径实际目录,这是解决方案:
cmake_minimum_required(VERSION 3.7)
project(WebService)
#Set Version
set (WebService_VERSION_MAJOR 1)
set (WebService_VERSION_MAJOR 0)
set(CMAKE_CXX_STANDARD 11)
#Setup Asio
find_path( asio_INCLUDE asio.hpp HINTS "${PROJECT_SOURCE_DIR}/dependency/asio/asio/include" "/usr/include" "/usr/local/include" "/opt/local/include" )
if ( asio_INCLUDE )
set( ASIO_FOUND TRUE )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DASIO_STANDALONE=YES" )
message( STATUS "${Green}Found ASIO include at: ${asio_INCLUDE}${Reset}" )
else ( )
message( FATAL_ERROR "${Red}Failed to locate ASIO dependency.${Reset}" )
endif ( )
#Setup OpenSSl
find_library( ssl_LIBRARY ssl ssleay32 HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/out32dll" "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
find_library( crypto_LIBRARY crypto libeay32 HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/out32dll" "${PROJECT_SOURCE_DIR}/dependency/openssl" "/usr/local/opt/openssl/lib" "/usr/lib" "/usr/local/lib" "/opt/local/lib" )
find_path( ssl_INCLUDE openssl/ssl.h HINTS "${PROJECT_SOURCE_DIR}/dependency/openssl/inc32" "${PROJECT_SOURCE_DIR}/dependency/openssl/include" "/usr/local/opt/openssl/include" "/usr/include" "/usr/local/include" "/opt/local/include" )
if ( ssl_INCLUDE AND ssl_LIBRARY AND crypto_LIBRARY )
set( OPENSSL_FOUND TRUE )
add_definitions( -DBUILD_SSL=TRUE )
if ( APPLE AND BUILD_SSL )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations" )
endif( )
message( STATUS "${Green}Found OpenSSL library at: ${ssl_LIBRARY}${Reset}" )
message( STATUS "${Green}Found OpenSSL include at: ${ssl_INCLUDE}${Reset}" )
message( STATUS "${Green}Found Crypto library at: ${crypto_LIBRARY}${Reset}" )
else ( )
message( FATAL_ERROR "${Red}Failed to locate OpenSSL dependency. see restbed/dependency/openssl; ./config shared; make all${Reset}" )
endif ( )
# RestBED
find_path( restbed_SOURCE "${PROJECT_SOURCE_DIR}/restbed" )
if ( restbed_SOURCE )
#set( restbed_FOUND TRUE )
set( restbed_BUILD "${CMAKE_CURRENT_BINARY_DIR}/restbed_build" )
set( restbed_DISTRIBUTION "${restbed_SOURCE}/distribution" )
set( restbed_INCLUDE "${restbed_DISTRIBUTION}/include" )
set( restbed_LIBRARY "${restbed_DISTRIBUTION}/library" )
message( STATUS "${Green}Found Restbed include at: ${restbed_INCLUDE}${Reset}" )
else ( )
message( FATAL_ERROR "${Red}Failed to locate Restbed dependency.${Reset}" )
endif ( )
# find all files from restbed library
file(GLOB_RECURSE restbed_LIBRARY_FILES
"${restbed_LIBRARY}/*.a")
include_directories(${restbed_INCLUDE})
#add_executable(WebService ${SOURCE_FILES})
add_executable(WebService main.cpp)
target_link_libraries(WebService ${restbed_LIBRARY_FILES})