I'm trying to write a bare-metal program with CMake(my project is locate in https://github.com/oska874/cmake_test). The demo source code are these:
my.c
void mymain(void)
{
int a=0;
a++;
a++;
}
and link script file is :
my.lds
ENTRY(mymain)
SECTIONS
{
. = 0x10000;
.text : { *(.text) }
. = 0x8000000;
.data : { *(.data) }
.bss : { *(.bss) }
}
I can compile it with command bellow:
gcc -c my.c
ld -T my.lds -o my my.o
But I don't know how to do the same thing with CMake for cmake always use its' own .lds script.
I tried search google and stackoverflow but all method failed.
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
I have tried CMakeLists.txt as bellow:
PROJECT(FreeRtos)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG " -T ${CMAKE_SOURCE_DIR}/my.lds -static")
AUX_SOURCE_DIRECTORY(. MYSRC)
ADD_EXECUTABLE(my ${MYSRC} )
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
And this
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(LINKERSCRIPT C)
# dummy.c must exist:
ADD_EXECUTABLE(EXE my.c dummy.c)
# linkerscript must exist:
SET_SOURCE_FILES_PROPERTIES(
dummy.c PROPERTIES OBJECT_DEPENDS ${CMAKE_SOURCE_DIR}/my.lds
)
I checked the link.txt
in CMakeFiles/EXE.dir/
,it shows that :
/usr/bin/cc -g -T /os_dev/workspace/test/ezio/tool/eo/freertos-pine64/Source/t2/my.lds -static CMakeFiles/my.dir/my.c.o -o my -rdynamic
I tried to exchange cc with ld with command that:
SET(CMAKE_C_LINK_EXECUTABLE "/usr/bin/ld <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET>")
It works ,the link.txt shows CMake will use my.lds.
CMake has no special variable for ldscript, so you may resort to appending appropriate linker flags:
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${CMAKE_SOURCE_DIR}/my.lds")
(This should be placed after project()
call in CMakeLists.txt
).
See that question for more information about setting linker flags in CMake.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With