我正在尝试编译包含头文件到.c文件的ac程序.但只有一个.c文件真正使用头文件中定义的变量.这是一些将生成链接器问题的示例代码.我试图让我的头文件包含由2个不同的.c文件使用的全局变量...任何类型的帮助将不胜感激.谢谢.
tmp1.h文件
#ifndef TMP1_H_1
#define TMP1_H_1
double xxx[3] = {1.0,2.0,3.0};
#endif
Run Code Online (Sandbox Code Playgroud)
tmp1.c文件
#include "tmp1.h"
void testing()
{
int x = 0;
x++;
xxx[1] = 8.0;
}
Run Code Online (Sandbox Code Playgroud)
main1.c文件
#include <stdio.h>
#include "tmp1.h"
int main()
{
printf("hello world\n");
}
Run Code Online (Sandbox Code Playgroud)
问题是你正在初始化头文件中的变量,所以你得到重复的符号.您需要double xxx使用extern关键字声明,然后在.c文件中初始化它.
像这样:
#ifndef TMP1_H_1
#define TMP1_H_1
extern double xxx[3];
#endif
Run Code Online (Sandbox Code Playgroud)
然后在其中一个.c文件中:
double xxx[3] = {1.0,2.0,3.0};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |