CMake add_custom_command with target_link_libraries
Asked Answered
S

2

6

For a number of reasons I have to manually generate a static library through a custom command.

However, it seems that the custom command is only executed when a target specifically requests its output files.

If I try to link the generated static library with target_link_libraries, CMake complains that it cannot find a rule to generate it.

# Building library on the fly
add_custom_command(OUTPUT mylib.a
    COMMAND ...
)
add_executable(myexe main.cpp)                                                                                                                                                     
target_link_libraries(myexe mylib.a) # Fails miserably

I imagine I have to insert a target or dependency somehow between the add_custom_command call and the target_link_libraries one, but I cannot understand how to do so correctly.

Smoothen answered 20/5, 2017 at 8:53 Comment(0)
H
1

For preserve dependency between executable and library file, you need to use full path to the library file when link with it:

target_link_libraries(my_exe ${CMAKE_CURRENT_BINARY_DIR}/mylib.a)

When use relative path, CMake expects library to be found by the linker (according to its rules), so CMake cannot adjust dependency with the library file in that case..

Healion answered 20/5, 2017 at 9:47 Comment(2)
Hm, so CMake tracks only changing in the library file without binding to add_custom_command. Looks like you need to resort to IMPORTED library target, as described in this question. BTW that question looks like duplicate for yours.Healion
Yeah it looks like it's the same problem, couldn't find it before.Smoothen
C
0

I've had to do this to invoke MATLAB's RTW to build DLLs for me. The function I used was add_custom_target.

add_custom_target(Name [ALL] [command1 [args1...]]
                  [COMMAND command2 [args2...] ...]
                  [DEPENDS depend depend depend ... ]
                  [BYPRODUCTS [files...]]
                  [WORKING_DIRECTORY dir]
                  [COMMENT comment]
                  [VERBATIM] [USES_TERMINAL]
                  [COMMAND_EXPAND_LISTS]
                  [SOURCES src1 [src2...]])

For you it may look like this:

add_custom_target(MyLib ALL 
                  <Put your command here>
                  COMMENT "Building MyLib"
                  )
add_executable(MyExe main.cpp)
target_link_libraries(MyExe MyLib)

If that doesn't work I've heard that you can use add_library() to create a dummy library. Then, use set_target_properties() to create an INTERFACE property for it.

Refences:

add_custom_target

Canaanite answered 23/5, 2017 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.