【发布时间】:2020-10-18 11:43:38
【问题描述】:
我无法在 CMake 中正确导出一个包,以便可以使用 find_package 命令导入它,而且无论我做什么,它仍然无法正常工作。我重新创建了最简单的场景,展示了我的导出过程,因此应该更容易诊断。
我设置了两个项目,我将它们放在同一个文件夹中:foo 和 bar,其中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 给出的建议,不幸的是,它似乎并没有解决问题。我阅读了documentation that he sent,并遵循了与我当前方法不同的所有部分。
该方法的两个主要区别是创建单独的 -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 正在设置中,尽管错误消息会有所不同,我似乎无法弄清楚我还需要做些什么来安抚它。
任何帮助将不胜感激。
【问题讨论】:
-
CMake 有打包的文档,甚至还有一个例子:cmake.org/cmake/help/v3.19/manual/…。你读过那个文件吗?有什么不清楚的地方?
-
@Tsyvarev 我以前没有看过这个文档,我一直在看文档,但似乎从来没有来过这个页面。谢谢;如果有任何问题,我会看一下并更新帖子。
标签: c windows cmake dependency-management