如何在CMake中使用特定的链接脚本(* .lds)? [英] How to use specific link script(*.lds) with CMake?
问题描述
我正在尝试使用CMake编写裸机程序(我的项目位于 https://github.com/oska874/cmake_test ).演示源代码如下:
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++;
}
和链接脚本文件是:
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
但是我不知道如何使用CMake进行cmake的相同操作,总是使用它自己的.lds脚本.
But I don't know how to do the same thing with CMake for cmake always use its' own .lds script.
我尝试搜索google和stackoverflow,但所有方法均失败.
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'
我尝试将CMakeLists.txt作为波纹管:
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)
还有
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
)
我检查了 CMakeFiles/EXE.dir/
中的 link.txt
,它表明:
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
我尝试使用以下命令与cc交换cc:
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>")
它有效,link.txt显示CMake将使用my.lds.
It works ,the link.txt shows CMake will use my.lds.
推荐答案
CMake对于ldscript没有特殊的变量,因此您可以求助于附加适当的链接器标志:
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")
(应将其放置在 CMakeLists.txt
中的 project()
调用之后).
(This should be placed after project()
call in CMakeLists.txt
).
有关该问题的更多信息,请参见该问题.在CMake中设置链接器标志.
See that question for more information about setting linker flags in CMake.
这篇关于如何在CMake中使用特定的链接脚本(* .lds)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!