如何在CMake中导出一个包?

x3naxklr  于 2023-10-16  发布在  其他
关注(0)|答案(2)|浏览(125)

我在CMake中正确导出一个包时遇到了问题,这样就可以使用find_package命令导入它,无论我做什么,它仍然不起作用。我已经重新创建了一个最简单的场景,从导出的Angular 展示了我的过程,因此应该更容易诊断。
我已经设置了两个项目,我把它们放在同一个文件夹中:foobar,其中foo是库,bar是依赖于foo的可执行文件。

├───bar
│       bar.c
│       CMakeLists.txt
│
└───foo
        CMakeLists.txt
        foo.c
        foo.h

请注意,这是两个独立的项目,而不是一个总体项目的子项目。
foo/CMakeLists.txt的内容:

cmake_minimum_required (VERSION 3.8)

project (Foo)

set (LIB_NAME foo)

add_library (${LIB_NAME} STATIC foo.c foo.h)

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

install (FILES foo.h
        DESTINATION include
)

install (EXPORT ${LIB_NAME}Targets
        NAMESPACE foo::
        DESTINATION lib/cmake
)

bar/CMakeLists.txt的内容:

cmake_minimum_required (VERSION 3.8)

project (Bar)

set (EXE_NAME bar)

find_package (foo REQUIRED)

add_executable (${EXE_NAME} bar.c)
target_link_libraries (${EXE_NAME} PRIVATE foo::foo)

我用来构建两个项目的命令序列:

md foo_build
md bar_build
cd foo_build
cmake ..\foo -DCMAKE_INSTALL_PREFIX=..\install\foo
cmake --build . --target INSTALL
cd ..\bar_build
cmake ..\bar -DCMAKE_PREFIX_PATH=..\install\foo

当所有这些都完成时,产生的错误是:

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

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

    fooConfig.cmake
    foo-config.cmake

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

当我尝试使用Config后缀而不是Target后缀导出包导出时,输出似乎没有任何区别:

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

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

    fooConfig.cmake
    foo-config.cmake

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

我不知道我做错了什么,CMake Documentation似乎对我一点帮助都没有。
我的CMake版本是3.17.3
先谢谢你了。

编辑

我遵循了@Tsyvarev给出的建议,不幸的是,它似乎没有解决问题。我阅读了他发送的文档,并遵循了其中与我目前方法不同的所有部分。
该方法的两个主要区别是创建单独的-Targets、-Config和-ConfigVersion cmake文件,并进行了以下更改。
整个项目的新结构:

├───bar
│       bar.c
│       CMakeLists.txt
│
└───foo
    │   CMakeLists.txt
    │   foo.c
    │   foo.h
    │
    └───cmake
            fooConfig.cmake

我添加到foo的CMakeLists.txt文件中的行:

include (CMakePackageConfigHelpers)
write_basic_package_version_file (
        ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake
        VERSION ${${PROJECT_NAME}_VERSION}
        COMPATIBILITY AnyNewerVersion
)

install (
        FILES
            cmake/${LIB_NAME}Config.cmake
            ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake
        DESTINATION lib/cmake
)

fooConfig.cmake的内容:

include ("${CMAKE_CURRENT_LIST_DIR}/fooTargets.cmake")

作为参考,安装foo的输出如您所料:

├───include
│       foo.h
│
└───lib
    │   foo.lib
    │
    └───cmake
            fooConfig.cmake
            fooConfigVersion.cmake
            fooTargets-debug.cmake
            fooTargets.cmake

bar项目上运行cmake时,也会出现同样的错误:

By not providing "Findfoo.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "foo", but
  CMake did not find one.

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

    fooConfig.cmake
    foo-config.cmake

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

顺便说一句,CMAKE_PREFIX_PATH正在设置,尽管错误消息可能会有所不同,但我似乎不知道还需要做些什么来安抚它。
任何帮助将不胜感激。

3lxsmp7m

3lxsmp7m1#

经过一些 * 非常令人沮丧 * 的研究,我终于找到了这个问题的答案,这就是find_package的工作方式。原来,我给它的目录结构是不够好,但只要我改变了“lib/cmake“安装到“lib/cmake/${LIB_NAME}“,并改变了CMAKE_PREFIX_PATH..\install而不是..\install\foo,一切似乎都很好,我 * 终于 * 得到了这个简单的程序工作。

kokeuurv

kokeuurv2#

从cmake 3.17开始,可以使用

cmake ... --debug-find

# CMakeLists.txt
   set(CMAKE_FIND_DEBUG_MODE True)

让cmake find_package()记录它正在查找的位置的详细信息。
(one我注意到的警告,至少从3.27.3开始:如果某个目录不存在,即使它出现在CMAKE_PREFIX_PATH或类似文件中,也不会显示为已被考虑过)

相关问题