C++/C 导入第三方库到CMake

C++/C Importing Third Party Library to CMake

你好我想知道这里是否有人可以帮助我确定我在尝试向我的 CMake 项目添加库时做错了什么: 所以最初我在命令行中用 cmake.Heres 我所做的

构建了库 https://github.com/recp/cglm

I created a build folder in the desktop(mkdir build)

  1. I changed directory to it (cd build)
  1. And then I created the sln with cmake(cmake /path-to/cglm)

之后我打开 Visual Studio 2019 看到了 5 个项目:ALL_BUILD、cglm、INSTALL、PACKAGE、ZERO_CHECK

我构建了 cglm 项目并在 Build Folder

中收到了它

然后在构建文件夹的调试文件夹中,我看到了 4 个文件:cglm.exp、cglm.lib、cgl-0.dll 和 cglm-0.pdb

然后我去另外一个项目添加库,创建了如下CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

project ("MathPlease")

add_executable(MathPlease "MathPlease.cpp" "MathPlease.h")

link_directories("path-to/desktop/dev/cglm/build

find_package("cglm")

当我尝试保存时收到以下错误

Severity    Code    Description Project File    Line    Suppression State
Warning     CMake Warning at C:\Users\asupr\source\repos\MathPlease\CMakeLists.txt:14 (find_package):
  By not providing "Findcglm.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "cglm", but
  CMake did not find one.

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

    cglmConfig.cmake
    cglm-config.cmake

  Add the installation prefix of "cglm" to CMAKE_PREFIX_PATH or set
  "cglm_DIR" to a directory containing one of the above files.  If "cglm"
  provides a separate development package or SDK, be sure it has been
  installed.    MathPlease  C:\Users\asupr\source\repos\MathPlease\CMakeLists.txt   14  

如果有人需要cmakeoutput.log,我也可以将其粘贴在这里,如有任何帮助,我们将不胜感激!

通常你有两种方法 -

  1. 安装 cglm 库,然后在您的项目中使用它
  2. 将 cglm 构建为项目的一部分

这两种方法我都用过,发现后者要好得多。由于这些原因,特别是对于较小的项目 -

  • 更好的智能感知,您可以跳转到第 3 方代码甚至编辑
  • 易于打包和发布项目工件
  • 在 CI 中易于管理,第 3 方项目的版本升级

我使用 FectchContent CMake api 来实现这个。 (或者同样可以通过手动添加第三方 source-code 到您的项目来实现)

现在我个人还没有在 cglm 上工作过,但仍然是一个示例构建文件

cmake_minimum_required(VERSION 3.16)

project("MathPlease")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)

fetchcontent_declare(
  cglm
  GIT_REPOSITORY https://github.com/recp/cglm.git
  GIT_TAG v0.8.5
  GIT_PROGRESS TRUE
)

if(NOT cglm_POPULATED)
  message("populating cglm")
  fetchcontent_populate(cglm)
  add_subdirectory(${cglm_SOURCE_DIR} ${cglm_BUILD_DIR})
endif()

add_executable(${PROJECT_NAME} MathPlease.cpp)

target_link_libraries(${PROJECT_NAME} cglm)

P.S。 FetchContent 是一个相当新的 CMake 特性。您将需要 CMake > 3.11