我可以在.ld文件中使用预处理器指令吗? [英] Can I use Preprocessor Directives in .ld file

查看:135
本文介绍了我可以在.ld文件中使用预处理器指令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在.ld文件中使用预处理器指令吗?
我需要使用两组.ld文件中的一个,并且希望让Build引擎使用宏,我可以这样做吗?

Can I use Preprocessor Directives in .ld file? I need to to use one of two sets of .ld file and wants to let Build engine deside that using macro, Can I do that?

推荐答案

是的,你可以。您需要为链接脚本手动运行预处理器,如下所示:

Yes, you can. You need to run preprocessor manually for your linker script, like this:

in="your_linker_script.ld"
out="generated_script.ld"
cflags=-Iinclude/

gcc -E -x c $cflags $in | grep -v '^#' >$out

标记:


  • -E 指定GCC只运行预处理程序

  • <$ c $

  • -E specifies GCC to only run preprocessor
  • -x c tells GCC to treat your linker script as C source file (it's needed to run GCC with your LD script)

或者您可以简单地使用 cpp 工具,它实际上是C预处理器。

Or you can simply use cpp tool, which is actually C preprocessor.

您将能够使用生成的链接器脚本来构建您的程序(例如,在Makefile中)。

After this you will be able to use generated linker script to build your program (e.g. in Makefile).

以下是我在项目中解决这个问题的方法:

Here is how I solved this problem in my project:


  1. 这里是我的链接器脚本使用预处理器( #include 指令和 CONFIG_TEXT_BASE 常量)。摘录:

  1. Here is my linker script uses preprocessor (#include directive and CONFIG_TEXT_BASE constant). Excerpt:

#include <config.h>


  • 这里是生成预处理链接描述文件的脚本。摘录:

  • Here is script to generate preprocessed linker script. Excerpt:

    gcc -E -x c -Iinclude $cflags $in | grep -v '^#'    >>$out
    


  • 这里是我的 Makefile ,它正在生成在 $(LDS_GEN)目标(第53行)中预处理链接器脚本,并使用此生成的脚本构建结果二进制文件(第42行)。摘录:

  • Here is my Makefile, it's generating preprocessed linker script at $(LDS_GEN) target (line 53) and the this generated script is being used to build result binary (line 42). Excerpt:

    $(LDS_GEN): $(LDS)
            build/gen-lds.sh $(LDS) $(LDS_GEN) $(CFLAGS)
    
    $(APP).bin: $(OBJS) $(LDS_GEN)
            $(LD) $(OBJS) -T $(LDS_GEN) -o $(APP).elf
    


  • 这篇关于我可以在.ld文件中使用预处理器指令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆