cmake 如何解决app和src文件夹之间的库依赖?

w8rqjzmb  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(542)

我有一个包含srcapp文件夹的项目。每个文件夹包含一个CMakeLists.txt文件。该项目有一个CMakeLists.txt文件,该文件当前位于src文件夹中的add_subdirectory。这个通用的CMakeLists.txt使用src文件夹中的代码生成dlllib并安装它。
问题是app有一个CMakeLists.txt文件,该文件编译了一个类,该类使用在运行src文件夹时安装的库。
1.如何配置app文件夹中的CMakeLists.txt,以使用从src文件夹运行CMakeLists.txt时创建和安装的库?

  1. app文件夹中的CMakeLists.txt可以从通用CMakeLists.txt调用吗?还是应该在app文件夹中创建一个完全独立的CMAkeLIsts.txt
    一般CMakeLists.txt
cmake_minimum_required( VERSION 3.15..3.25 FATAL_ERROR )

# Create Project
project( "myproject"
        VERSION 0.1
        LANGUAGES CXX)
set(LIBRARY_NAME ${PROJECT_NAME}_lib)
set(CMAKE_INSTALL_INCLUDEDIR include)
set(CMAKE_INSTALL_LIBDIR lib)
set(CMAKE_INSTALL_BINDIR bin)
set(CMAKE_INSTALL_PREFIX $ENV{OCTREE_HOME})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "$ENV{PCL_ROOT}/cmake/Modules/" "$ENV{OCTREE_HOME}/share/cmake/Modules/")

message(STATUS "CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")

# All users of this library will need at least C++11
if(MSVC)
    add_compile_options(/Zc:__cplusplus)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /bigobj")
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
else(MSVC)
    target_compile_features(apps PUBLIC cxx_std_17)
endif(MSVC)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g")
set(CMAKE_VERBOSE_MAKEFILE ON)

# find packages
set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x86-windows/share/opencv3")
find_package(OpenCV CONFIG REQUIRED) # COMPONENTS opencv_world)
if (OpenCV_FOUND)
    message(STATUS "OpenCV version: ${OpenCV_VERSION}")
endif()

option(USE_PCL "Use PCL" ON)
find_package(PCL CONFIG REQUIRED COMPONENTS common io filters visualization)
if (PCL_FOUND)
    message(STATUS "PCL version: ${PCL_VERSION}")
    add_definitions(-DPOINTCLOUDREAD_USES_PCL)
endif()

# The executable code is here
add_subdirectory(src)
add_subdirectory(apps)

#Install the targets and their associated files
install(TARGETS ${LIBRARY_NAME}
        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
        INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
        PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
        )
install(TARGETS ${LIBRARY_NAME} DESTINATION "$ENV{OCTREE_HOME}/${CMAKE_INSTALL_LIBDIR}")
install(TARGETS ${LIBRARY_NAME} LIBRARY DESTINATION "$ENV{OCTREE_HOME}/${CMAKE_INSTALL_BINDIR}")
install(DIRECTORY include/ DESTINATION "$ENV{OCTREE_HOME}/${CMAKE_INSTALL_INCLUDEDIR}")
install(FILES cmake/Modules/Find${PROJECT_NAME}.cmake DESTINATION "$ENV{OCTREE_HOME}/share/cmake/Modules/")

src CMakeLists.txt

set(PROJECT_INCLUDE_DIRS ${CMAKE_HOME_DIRECTORY}/include)
set(HEADER_LIST ../include/octreesegmentation.h ../include/templates.h)
set(SOURCE_LIST ../src/octreesegmentation.cpp)
list(APPEND INCLUDE_DIRS
        "${EIGEN_INCLUDE_DIR}"
        "${OpenCV_INCLUDE_DIRS}"
        "${PROJECT_INCLUDE_DIR}"
        "${PCL_INCLUDE_DIRS}"
        "$ENV{PCL_ROOT}/3rdParty/VTK/include/vtk-${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}"
        "${PROCESSING_INCLUDE_DIRS}"
        "${PROJECT_INCLUDE_DIRS}")
list(APPEND TARGET_LINK_LIB_DIRS
        "$ENV{OCTREE_HOME}/lib"
        "${OpenCV_DIR}/${OpenCV_ARCH}/${OpenCV_RUNTIME}/lib"
        "${PCL_LIBRARY_DIRS}"
        "${PCL_LIBRARY_DIRS}/../3rdParty/VTK/lib"
        "${PCL_LIBRARY_DIRS}/../3rdParty/Eigen/lib"
        "$ENV{OCTREE_HOME}/lib")
list(APPEND TARGET_LINK_LIBRARIES
        "${OpenCV_LIBS}"
        "${PCL_LIBRARIES}"
        "${PCL_VISUALIZATION_LIBRARIES}"
        "${PREPROCESSING_LIBRARIES}"
        "${PROCESSING_LIBRARIES}"
        "${POSTPROCESSING_LIBRARIES}"
        "${PCL_COMMON_LIBRARY_DEBUG}"
        "VTK::CommonCore")

if(CMAKE_BUILD_TYPE MATCHES Debug)
    message(STATUS "========================== DEBUG INFO ==========================")
    message(STATUS "HEADER_LIST: ${HEADER_LIST}")
    message(STATUS "SOURCE_LIST: ${SOURCE_LIST}")
    message(STATUS "INCLUDE_DIRS: ${INCLUDE_DIRS}")
    message(STATUS "TARGET_LINK_LIB_DIRS: ${TARGET_LINK_LIB_DIRS}")
    message(STATUS "TARGET_LINK_LIBRARIES: ${TARGET_LINK_LIBRARIES}")
    message(STATUS "========================== END OF DEBUG INFO ==========================")
endif()

# Generate LIB file
add_library(${LIBRARY_NAME} STATIC ${SOURCE_LIST} ${HEADER_LIST})
set_target_properties(${LIBRARY_NAME} PROPERTIES
        VERSION ${PROJECT_VERSION}
        OUTPUT_NAME ${LIBRARY_NAME}
        ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib)

target_include_directories(${LIBRARY_NAME} PUBLIC ${INCLUDE_DIRS})
target_link_directories(${LIBRARY_NAME} PRIVATE ${TARGET_LINK_LIB_DIRS})
target_link_libraries(${LIBRARY_NAME} PRIVATE ${TARGET_LINK_LIBRARIES})

# Generate DLL file
add_library(${LIBRARY_NAME}_dll SHARED ${SOURCE_LIST} ${HEADER_LIST})
set_target_properties(${LIBRARY_NAME}_dll PROPERTIES
        VERSION ${PROJECT_VERSION}
        OUTPUT_NAME ${LIBRARY_NAME}
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib)

target_include_directories(${LIBRARY_NAME} PUBLIC ${INCLUDE_DIRS})
target_link_directories(${LIBRARY_NAME} PRIVATE ${TARGET_LINK_LIB_DIRS})
target_link_libraries(${LIBRARY_NAME} PRIVATE ${TARGET_LINK_LIBRARIES})

app CMakeLists.txt。此CMakeLists.txt应使用在src文件夹中生成的库。

list(APPEND INCLUDE_DIRS
        "${EIGEN_INCLUDE_DIR}"
        "${OpenCV_INCLUDE_DIRS}"
        "${PROJECT_INCLUDE_DIR}"
        "${PCL_INCLUDE_DIRS}"
        "$ENV{PCL_ROOT}/3rdParty/VTK/include/vtk-${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}"
        "${PROCESSING_INCLUDE_DIRS}")
list(APPEND TARGET_LINK_LIB_DIRS
        "$ENV{OCTREE_HOME}/lib"
        "${OpenCV_DIR}/${OpenCV_ARCH}/${OpenCV_RUNTIME}/lib"
        "${PCL_LIBRARY_DIRS}"
        "${PCL_LIBRARY_DIRS}/../3rdParty/VTK/lib"
        "${PCL_LIBRARY_DIRS}/../3rdParty/Eigen/lib")
list(APPEND TARGET_LINK_LIBRARIES
        "${OCTREESEGMENTATION_LIBRARIES}"
        "${OpenCV_LIBS}"
        "${PCL_LIBRARIES}"
        "${PCL_VISUALIZATION_LIBRARIES}"
        "${PREPROCESSING_LIBRARIES}"
        "${PROCESSING_LIBRARIES}"
        "${POSTPROCESSING_LIBRARIES}"
        "${PCL_COMMON_LIBRARY_DEBUG}"
        "VTK::CommonCore"
        "VTK::SystemTools")
include_directories("${INCLUDE_DIRS}" )
link_directories(${TARGET_LINK_LIB_DIRS})
#add_definitions(${PCL_DEFINITIONS})
#add_library(main SHARED ${PROCESSING_SOURCES})
add_executable(main main.cpp)
target_link_libraries(main ${OCTREESEGMENTATION_LIBRARIES})
cqoc49vn

cqoc49vn1#

你有两条路。

  • 使app成为一个独立项目。这意味着您需要调用cmake_minimum_required(...)project()以及您希望在项目配置中设置的任何其他内容,例如您希望设置的CMAKE_*变量。然后编写src项目以导出查找配置脚本(另请参阅the CMakePackageConfigHelpers module),并通过find_packageinclude从安装中导出的目标文件或from the build tree if you use export()查找它。
  • 将执行add_subdirectory(src ...)的项目也设置为add_subdirectory(app ...)。参见the add_subdirectory docs

其他反馈:

  • 最好在CMakeLists.txt中使用list(APPEND CMAKE_MODULE_PATH ...)而不是set(CMAKE_MODULE_PATH ...),这样就不会覆盖用户在命令行中设置的任何值。
  • 我想你会对the GNUInstallDirs module感兴趣

相关问题