如何在CMake中包含一个未安装在系统范围内的项目?

k10s72fa  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(154)

我试图将这个C++ https://github.com/sikang/DecompUtil包包含到我的项目中,但我不确定如何在我的CMakelists.txt中实现它。我不断地遇到这个错误:

CMake Error at CMakeLists.txt:8 (find_package):
  By not providing "FindDecompUtil.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "DecompUtil", but CMake did not find one.

  Could not find a package configuration file provided by "DecompUtil" with
  any of the following names:

    DecompUtilConfig.cmake
    decomputil-config.cmake

  Add the installation prefix of "DecompUtil" to CMAKE_PREFIX_PATH or set
  "DecompUtil_DIR" to a directory containing one of the above files.  If
  "DecompUtil" provides a separate development package or SDK, be sure it has
  been installed.

复制问题的步骤:

  1. git clone https://github.com/sikang/DecompUtil到项目目录
  2. cd DecompUtil
  3. mkdir build && cd build && cmake .. && make构建包(成功)
  4. cd project_root
  5. mkdir build && cd build
  6. cmake ..(错误在此)
    这里是我的CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(test)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/DecompUtil)

add_subdirectory(DecompUtil)

find_package(DecompUtil REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include
  ${DECOMP_UTIL_INCLUDE_DIRS})

add_executable(test src/test.cpp)

target_link_libraries(test DecompUtil)

install(TARGETS
test
  DESTINATION lib/${PROJECT_NAME})

我的项目结构是这样的

内部test.cpp

#include <iostream>
#include <decomp_geometry/polyhedron.h>

int main(){
    Polyhedron<3> poly3DViz;
    std::cout << "Hello world" << std::endl;
}
hec6srdp

hec6srdp1#

错误消息非常清楚:

Add the installation prefix of "DecompUtil" to CMAKE_PREFIX_PATH or set
  "DecompUtil_DIR" to a directory containing one of the above files.

decomp_utilConfig.cmake位于该repo的根。
例如.(因为您将DecompUtil/目录放在项目的root / source目录下)

list(APPEND CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/DecompUtil")
find_package(DecompUtil REQUIRED)

set(DecompUtil_DIR "${PROJECT_SOURCE_DIR}/DecompUtil")
find_package(DecompUtil REQUIRED)

decomp_utilConfig.cmake没有定义一个名为DecompUtil的目标,并且您已经使用DECOMP_UTIL_INCLUDE_DIRS添加了include目录,所以我很确定您的target_link_libraries(test DecompUtil)是错误的,应该被删除。
此外,几乎没有理由将add_subdirectoryfind_package混合用于一个东西。您的add_subdirectory(DecompUtil)应该被删除。

相关问题