gpt4 book ai didi

c - 链接描述文件,初始化 C 变量

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:24 28 4
gpt4 key购买 nike

我尝试了很长时间才找到答案。抱歉,我真的找不到。

我使用 gcc、ld、gcc(用于汇编程序编译)。

我写了ld脚本:

SECTIONS
{
.text : *{.text}
}
LS_size = (SIZEOF(.text) + 2048 ) & ( 0xF800 ) >> 9 ;

我尝试使用汇编程序中的 LS_size:

_rom_size:
.byte LS_size

它工作正常,但在 C 代码上我无法为变量设置值:

struct my_struct vari = {
.codesize = LS_size,
}

如果我使用 extern LS_size,我得到:

error: initializer element is not constant

如何从 ld 脚本中生成的变量为常量 C 变量设置默认值?

最佳答案

用C写:

struct my_st { 
long the_code_sz;
};

struct my_st vv = {
.the_code_sz = ((long)(&LS_size))
};

然后就可以使用了

vv.the_code_sz

LS_size 是一个符号,该符号的地址是您在链接描述文件中赋予它的值。

关于c - 链接描述文件,初始化 C 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19268409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com