21

I have a CMake script where the final executable is linked with my own linker script:

cmake_minimum_required(VERSION 3.1)

project(test_app)

set(LINKER_SCRIPT "linker.ld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${LINKER_SCRIPT}")

add_executable(${PROJECT_NAME}.elf
    main.cpp
    startup.cpp
)

How do I make an executable dependent also on the linker script file (trigger linking if linker.ld was changed)?

6
  • How is this script produced? add_custom_target()?
    – arrowd
    Commented Sep 30, 2015 at 12:15
  • Linker script is edited by hand, at this time if I make changes in this script, then I must re-build project.
    – vlk
    Commented Sep 30, 2015 at 12:29
  • I try to add this: add_custom_target(OUTPUT ${PROJECT_NAME}.elf DEPENDS ${LINKER_SCRIPT}) but without success.
    – vlk
    Commented Sep 30, 2015 at 12:44
  • 7
    See LINK_DEPENDS target property. This works for all makefile generators except - until now - for Ninja (will be fixed with CMake 3.4, see 0014796: LINK_DEPENDS should work with Ninja backend)
    – Florian
    Commented Oct 1, 2015 at 10:15
  • 2
    Florian, look like this is the right way and is working for me (I use Makefile), can you create an answer, I accept it. Thanks.
    – vlk
    Commented Oct 1, 2015 at 11:13

2 Answers 2

32

You can add a LINK_DEPENDS property to your executable target, using set_target_properties. Add the following line after your add_executable command:

set_target_properties(${TARGET_NAME} PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT})

The first argument to set_target_properties is the target name, i.e. the first argument you passed to add_executable.

2
  • 3
    The problem is that cmake searching for ${LINKER_SCRIPT} in build directory instead of in source directory. Actually I have solution to add ${CMAKE_SOURCE_DIR}/${LINKER_SCRIPT}
    – vlk
    Commented Jan 19, 2018 at 13:22
  • 1
    Or ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_SCRIPT} to search in the directory that the add_executable is defined in.
    – Ryan
    Commented Mar 22, 2020 at 19:27
0

I found this mail which described three possible ways for forcing an executable to be dependent on a linker script. Its author prefers this way:

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(LINKERSCRIPT C)
FILE(WRITE main.c "void main(){}")
# dummy.c must exist:
ADD_EXECUTABLE(EXE main.c dummy.c)
# linkerscript must exist:
SET_SOURCE_FILES_PROPERTIES(
    dummy.c PROPERTIES OBJECT_DEPENDS ${CMAKE_SOURCE_DIR}/linkerscript
)

Here dummy.c is an empty file, which is listed for the add_executable() command only for make resulted executable dependent on the linker script via the OBJECT_DEPENDS property.

2
  • 2
    Yes, I known about this solution with dummy.c file, but I don't think it is very elegant. For example if into Makefile I add empty target: outupt.elf: linker.ln beside linking target: output.elf: main.o startup.o then I get what I want, but I don't know how can I create an empty target with CMake, or if this is possible.
    – vlk
    Commented Sep 30, 2015 at 19:14
  • 2
    Yes, with direct using of Makefile, adding linker dependency is simpler and more elegant. But CMake doesn't allow adding dependencies for one file more than once. First time dependencies are added in add_executable() call, but all of them should be source or header files, so you cannot add linker-dependency at this stage. From the other side, you needn't to bother about Makefiles produced by CMake. They are much more complex than manually written files in any case.
    – Tsyvarev
    Commented Sep 30, 2015 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.