背景:
有的时候,非源码中的文件,需要使用到源码头文件中的一些宏,如连接脚本(link.ld)中,会用到源码中定义分配的内存地址宏,此时就可以用 gcc -E -P 命令对link.ld中的宏进行替换,否则无法识别宏。
待展开文件:link.ld
#include “memory.h"
SECTIONS
{
. = MEM_TEXT_BASE;
.text : {
*(.text)
}
. = MEM_DATA_BASE;
.data ALIGN(4) : {
*(.data)
}
.bss ALIGN(4) : {
*(.bss)
}
}
头文件:memory.h
#define MEM_BASE 0x800000000
#define MEM_TEXT_BASE MEM_BASE
#define MEM_TEXT_SIZE 0x10000
#define MEM_DATA_BASE (MEM_TEXT_BASE + MEM_TEXT_SIZE)
#define MEM_DATA_SIZE 0x20000
通过命令 gcc -E -P -<link.ld> link_out.ld
展开后文件:link_out.ld
SECTIONS
{
. = 0x80000000;
.text : {
*(.text)
}
. = (0x80000000 + 0x10000);
.data ALIGN(4) : {
*(.data)
}
.bss ALIGN(4) : {
*(.bss)
}
}
其中,-P 可以删除无用的信息