【问题标题】:How to build and use an external library with CMake如何使用 CMake 构建和使用外部库
【发布时间】:2021-12-25 06:03:38
【问题描述】:

我正在尝试使用跨平台库portaudio 构建便携式声音合成器。该库有自己的 CMake 文件可供构建。我想我已经设法将它构建为我的项目的一部分(构建以退出代码 0 完成),但我不知道如何实际使用它的导入。任何#include 我都尝试使用cannot open source file 中的结果。

这是我项目的 CMake 文件:

cmake_minimum_required(VERSION 3.0.0)
project(sound-synth VERSION 0.1.0)

include(CTest)
enable_testing()
include(ExternalProject)

# first try
# ExternalProject_Add(portaudio
#                GIT_REPOSITORY https://github.com/PortAudio/portaudio.git
# )

# second try after using git submodule add https://github.com/PortAudio/portaudio.git external/portaudio
add_subdirectory(external/portaudio EXCLUDE_FROM_ALL)

file(GLOB_RECURSE SRC_FILES src/*.cpp)
add_executable(sound-synth main.cpp ${SRC_FILES})
target_include_directories(sound-synth PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)


set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

And this is how it looks trying to include header files from the library

我正在使用 VSC 的 CMakeTools 和 MSVC 编译器。

【问题讨论】:

    标签: c++ visual-studio-code cmake shared-libraries portaudio


    【解决方案1】:

    我还必须将可执行文件与库链接到 target_link_libraries(sound-synth PortAudio)。 然后在 Windows 上,我遇到的问题是 .dll 被放置在与 .exe 不同的文件夹中。所以我只是添加了一个复制文件命令并修复了它。

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 2019-06-09
      • 2021-09-30
      • 2016-11-02
      • 1970-01-01
      • 2020-11-21
      • 2019-12-13
      相关资源
      最近更新 更多