[CMake] How to correctly set CMAKE custom commands for external projects

キキジキ kikijikispaccaspecchi at gmail.com
Sun May 25 09:58:55 EDT 2014


Hello everyone, I hope you can help me understand this. I originally asked
the same question on stackoverflow, so if you want you can check there too (
http://stackoverflow.com/questions/23591085/how-to-correctly-set-cmake-custom-commands-for-external-projects
).

I have a main project (A) depending on 2 library projects (B, C).

─A
 ├──src
 ├──dep
 │   ├─B
 │   │ ├──include
 │   │ ├──src
 │   │ └──CMakeLists.txt[B]
 │   └─C
 │     ├──include
 │     ├──src
 │     └──CMakeLists.txt[C]
 └──CMakeLists.txt[A]

The CMakeLists.txt of A is something like this (shortened):

project(A)

add_subdirectory(dep/B)
include_directories(dep/B/include)

add_subdirectory(dep/C)
include_directories(dep/C/include)

add_executable(A ...src/files...)

target_link_libraries(A B)
target_link_libraries(A C)

When B or C are built I'd like to have the output directories copied to the
output directory of A, so that it can find the updated compiled libraries
(.lib and .dll files).

This is what I tried using (in CMakeLists.txt[A]):

add_custom_command(TARGET B POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        $<TARGET_FILE_DIR:B>
        $<TARGET_FILE_DIR:A>)

add_custom_command(TARGET C POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        $<TARGET_FILE_DIR:C>
        $<TARGET_FILE_DIR:A>)

But apparently is not executed.

I tried doing the copy on the PRE_BUILD step of A like this:

add_custom_command(TARGET A PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        $<TARGET_FILE_DIR:B>
        $<TARGET_FILE_DIR:A>)

add_custom_command(TARGET A PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        $<TARGET_FILE_DIR:C>
        $<TARGET_FILE_DIR:A>)

It works, but I have to recompile A every time I'm working on B or C.

What's the correct way to configure this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20140525/1c137e0f/attachment-0001.html>


More information about the CMake mailing list