CMake build project in subdirectory and copy output file

39 Views Asked by At

I am attempting a CMake project that depends on the PortAudio library. PortAudio is contained in a portaudio subdirectory. According to the PortAudio docs, one can build PortAudio via CMake (on Windows) with:

cmake {portaudio path} -G "Visual Studio 14 2015 Win64"
cmake --build . --config Release

Now the question is, how can I build PortAudio from the containing project's CMakeLists.txt and copy the resulting portaudio.lib file to a target directory?

My attempt so far is a CMakeLists.txt that contains:

add_subdirectory(portaudio)
configure_file(portaudio/Release/portaudio.lib portaudio.lib COPYONLY)

in a directory that contains the subdirectory portaudio with the PortAudio project contained within.

However, this attempts (and fails) to copy the file whenever cmake is run, but instead it should only do so after actually building the subproject (i.e. running the --build command on the portaudio subproject) so that the file alreday actually exists. More specifically, if I try to run just cmake . in the containing directory, it errs:

CMake Error: File C:/src/libs/portaudio/Debug/portaudio.lib does not exist.
CMake Error at CMakeLists.txt:2 (configure_file):
  configure_file Problem configuring file

Adding the -G option for the VS version results in the same error.

What changes to CMakeLists.txt and/or my commands would I need to be able to cmake . in the containing project directory so that it builds portaudio and copies the lib file?

0

There are 0 best solutions below