c++ - 将已编译的库和包含文件添加到 CMake 项目?

标签 c++ gcc cmake

将预建库包含到 cmake 项目中的最佳方法是什么?我想将 FreeType 包含到我正在处理的项目中,文件结构如下:

  • Build
    • MacOS
      • Make/
      • XCode/
    • Windows
      • VisualStudio/
  • Source
    • libs
      • MacOS
        • libfreetype
      • Windows
        • freetype.dll
    • includes
      • freetype/ (Various header files that are included automatically by ftbuild.h)
      • ftbuild.h (this is what is included in code from my understanding.)
    • MyProject
      • main.cpp
      • foo.cpp
      • foo.h

库已经编译。 MyProject 是当前项目的名称。

谢谢! 迈克

最佳答案

只需将 target_link_libraries 与预构建库的完整路径一起使用。

所以,像这样:

# In the file Source/MyProject/CMakeLists.txt
add_executable(my_exe main.cpp foo.cpp foo.h)
if(WIN32)
  target_link_libraries(my_exe ${CMAKE_CURRENT_SOURCE_DIR}/../libs/Windows/freetype.lib)
endif()
if(APPLE)
  target_link_libraries(my_exe ${CMAKE_CURRENT_SOURCE_DIR}/../libs/MacOS/libfreetype.a)
endif()

关于c++ - 将已编译的库和包含文件添加到 CMake 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2601798/

相关文章:

visual-c++ - 内联汇编器: flags — to save or not to save

c++ - 无法构建 surround360_render - CMake 问题

java - 我无法在 ubuntu 19.04 中为 java cmake opencv

c++ - 标准映射复合键

c++ - 哪个 C++ 概念更适合为数据库查询建模——流或输入迭代器?

c++ - 如何使用#pragma 在 G++ 中启用优化

c - C中的 "!!"是什么?

c++ - 无法发出 QThread 的信号

c++ - Shape.SetAsBox() 中的断言失败?

c++ - Windows 上的 Clang 和 CMake - clang.exe 损坏,程序不可执行