我如何在C中引用需要此功能的静态函数?

问题描述 投票:0回答:1
#include <stdio.h>
typedef unsigned char uint8_t;
// I want this at the end of the file
//static const uint8_t hello[] = { 'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};
int main()
{   // how do I declare a forward reference to 'hello' here?
    printf("%s\n", hello);
    return;
}
// but down here, the linker can't resolve it
static const uint8_t hello[] = { 'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};

错误C2065'hello':未声明的标识符

出于装饰性原因,我想将大型静态数据表放在c源文件的末尾。但是我该如何引用呢?我在函数中使用了前向引用来解析稍后出现在文件中的函数,但是静态变量使我头疼。我尝试了extern(作为最后的希望),但是链接器看起来在模块之外(这很有意义),但是仅在需要它的函数之后的几行就无法解析为darn变量。这是C的限制吗(我已经在两个编译器上尝试过此限制),或者我只是缺少了难以置信的显而易见的东西?

c static forward-declaration
1个回答
0
投票

您在这里。

#include <stdio.h>
#include <stdint.h>

static const uint8_t hello[]; 

int main( void )
{
    printf("%s\n", hello);
}

static const uint8_t hello[] = { 'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};

这里

static const uint8_t hello[]; 

是数组hello的所谓临时定义。

© www.soinside.com 2019 - 2024. All rights reserved.