Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c - How to use specific link script(*.lds) with CMake?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...