Home > Back-end >  How to build and use an external library with CMake
How to build and use an external library with CMake

Time:11-16

I am trying to build a portable sound synthesiser using the cross-platform library portaudio. The library has it's own CMake file to be built with. I think I have managed to build it as part of my project (build finishes with exit code 0) but I can't figure how to actually use it's imports. Any #include I try to use results in cannot open source file.

This is my project's CMake file:

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

I am using VSC's CMakeTools and the MSVC compiler.

CodePudding user response:

I had to also link the executable with the library with target_link_libraries(sound-synth PortAudio). Then on windows I had the problem that the .dll was placed in a separate folder from the .exe. So I just added a copy file command and that fixed it.

  • Related