问题
With the Green Hill compiler it is possible to create variables in the linker that is visible at runtime. In the linker:__ghs_ramstart = MEMADDR(dram_memory);
In the code:if (__ghs_ramstart == 0) {do something}
Is it possible to do the same type of thing when using gcc?
回答1:
Yes, you can do that using GNU ld linker scripting. http://sourceware.org/binutils/docs-2.21/ld/Scripts.html#Scripts You can defined symbols in scripts that are accessible from gcc. I've also used scripts to create data tables (an array of addresses, for example).
In the linker script, you can say something like
__ghs_ramstart = dram_memory;
and access it from C, e.g.
extern char __ghs_ramstart[];
...
You may need to add or remove a leading underscore, depending on your target. Some targets add them to symbols, some do not.
回答2:
You can define a macro using -D option.
来源:https://stackoverflow.com/questions/6278219/how-to-create-runtime-visible-variables-using-gcc