Jossue M
Jossue M

Reputation: 21

CMake install rules

I'm trying to make sense of the installation rules while using cmakepresets and a vcpkg manifest. I have a project that runs perfectly in debug mode, but in release mode it does not work,I am attributing this to the way my installation rules are set up

CMkeLists.txt at root directory:

cmake_minimum_required(VERSION 3.25)project(TestCppHttpImageTransfer VERSION 1.0 LANGUAGES CXX)

#Specify the C++ standard

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#Required Libraries

find_package(Boost REQUIRED)
find_package(RapidJSON CONFIG REQUIRED)
find_package(OpenCV REQUIRED)

Include submodules directory

add_subdirectory(submodules/my-service)

#Set list of header files
set(FILES_HEADERS "")

#set list of source files
set (FILES_SOURCES"${CMAKE_CURRENT_LIST_DIR}/TestCppHttpImageTransfer.cpp")

#Add the executable for the main project

add_executable(${PROJECT_NAME} ${FILES_HEADERS} ${FILES_SOURCES})

#Set compile definitions

target_compile_definitions(${PROJECT_NAME} PRIVATE
TEST_CONTENT_DIRECTORY="${CMAKE_CURRENT_SOURCE_DIR}/Data")

#Link necessary libraries to the executable

target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBS} MyService)

#Include directories for the executable

target_include_directories(${PROJECT_NAME} PRIVATE
${Boost_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS})

install(TARGETS ${PROJECT_NAME}
        EXPORT ${PROJECT_NAME}
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        RUNTIME DESTINATION bin
        INCLUDES DESTINATION include)

install(IMPORTED_RUNTIME_ARTIFACTS ${OpenCV_LIBS} DESTINATION bin

CMakeLists.txt inside the submodules/my-service directory:

cmake_minimum_required(VERSION 3.25)

project(MyService VERSION 1.0 LANGUAGES CXX)

#Specify the C++ standard

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#Required Libraries

find_package(Boost REQUIRED)
find_package(RapidJSON CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)

#Ensure Boost Paths exist

find_path(Boost_Asio_FOUND boost/asio.hpp PATHS ${Boost_INCLUDE_DIRS} REQUIRED)
find_path(Boost_Beast_FOUND boost/beast.hpp PATHS ${Boost_INCLUDE_DIRS} REQUIRED)

find_library(OPENSSL_LIB libssl.lib REQUIRED)
find_library(CRYPTO_LIB libcrypto.lib REQUIRED)

#Make sure OpenSSL paths exist.
find_path(OPENSSL_INCLUDE openssl/opensslconf.h REQUIRED)

#Find Protobuf package

if(NOT protobuf_FOUND)
    find_package(protobuf CONFIG REQUIRED)
endif()

#Add source to this project's library

add_library(${PROJECT_NAME} STATIC
    "include/image_message.pb.h"
    "include/MyService.h"
    "include/MyServiceCommon.h"
    "include/MyServiceSession.h"
    "src/image_message.pb.cc"
    "src/MyService.cpp"
    "src/MyServiceSession.cpp")

#Include directories for the project

target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include")

target_include_directories(${PROJECT_NAME} PRIVATE
${Boost_INCLUDE_DIRS}
${OPENSSL_INCLUDE}
${RAPIDJSON_INCLUDE_DIRS})

#Link libraries to the project

target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENSSL_LIB} ${CRYPTO_LIB} protobuf::libprotobuf)

#Google Test

add_subdirectory(external)
add_subdirectory(test)

#Installation rules

install(TARGETS ${PROJECT_NAME}EXPORT ${PROJECT_NAME}
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        RUNTIME DESTINATION bin
        INCLUDES DESTINATION include)

Also I have a vcpkg.json:

{
  "name": "test-cpp-ui-http-image-transfer",
  "version": "1.0.0",
  "dependencies": [
    {
      "name": "boost-asio",
      "features": [ "ssl" ]
    },
    "boost-beast",
    "rapidjson",
    "protobuf",
    "opencv",
    "pugixml",
    "libzip"
  ]
}

And I have these set up in the windows-base configuration in my cmakepresets.json:

"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"toolchainFile": "$env{VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake"

So cmake installs the dependencies that I need under out/build/x64-release/Debug, and I can sucessfully run the program within the directory:

enter image description here

but when it comes down to installing it, it only installs the .exe under out/install/x64-release/bin. I tried experimenting with install(IMPORTED_RUNTIME_ARTIFACTS ..) also as shown in the root cmakelists.txt, which installs the opencv libraries in the bin folder as well, but it install all of them and not only the ones used as in out/build/x64-release/Debug.

Is there a way to have the same results as in in the out/build/x64-release/Debug when installing?

Upvotes: 1

Views: 196

Answers (1)

Jossue M
Jossue M

Reputation: 21

So I mostly use vcpkg libraries in my project, I found a repo on github where they use set(X_VCPKG_APPLOCAL_DEPS_INSTALL ON) which is suppose "Automatically copy dependencies into the install target directory for executables. Requires CMake 3.14.". This is stated under the scripts/buildsystems/vcpkg.cmake on your vcpkg installation directory.

However, this did not work for me, and after trying multiple things,I figured that when you're using CMakePresets.json, you need to specify the value for this option under your "cacheVariables" in your CMakePresets.json configurations. After doing that, the vcpkg dlls that I needed were installed in the "bin" folder.

This fixed this issue for me.

Upvotes: 1

Related Questions