CMake: How to add linker script dependency for executable

I have a CMake script where the final executable is linked to 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 to make the executable also dependent on the linker script file (cause linking if linker.ld been changed)?

+9
source share
2 answers

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

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

The first argument to set_target_properties is the name of the target, that is, the first argument that you passed add_executable .

+12
source

I found this letter that describes three possible ways to make the executable depend on the linker script. The author prefers like this:

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 that is specified for the add_executable() command only for the make add_executable() executable, which depends on the linker script through the OBJECT_DEPENDS property.

+1
source

All Articles