【发布时间】:2013-10-16 04:19:54
【问题描述】:
您好,我只是想知道如何在 .c 文件之间共享全局变量。
我尝试添加后续代码,但仍然出现错误。
test.c 文件
#include <stdio.h>
int max = 50;
int main()
{
printf("max %d", max); // result max 50
}
通过.h
extern int max;
通过.c
#include <stdio.h>
#include "pass.h"
max;
int main()
{
printf("pass %d \n", max);
return 0;
}
但是当我编译passed.c我得到跟随错误
Undefined symbols for architecture x86_64:
"_max", referenced from:
_main in passed-iOMugx.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
有人可以帮忙吗?非常感谢。
【问题讨论】:
-
你真的是说有两个 main() 函数吗?
-
为什么有两个主要功能?它们是独立的程序还是要链接到一个程序中?如果它们在一个程序中链接在一起,它不应该抱怨 max - 它应该抱怨重复的主要功能。
-
是的,我知道这可能是不可能的
标签: c global-variables declaration