【问题标题】:Why this header file include create issue? [duplicate]为什么这个头文件包含创建问题? [复制]
【发布时间】:2021-01-28 01:27:23
【问题描述】:

当我尝试在 main.c 中打印 font.h 中的缓冲区时,我有 main.c、test.c、test.h 和 font.h 文件。它创建编译计时器错误。我做错了什么吗?请帮忙。

错误:

tmp/ccnygNfe.o:(.data+0x0): `c'的多重定义

/tmp/ccGk8E1o.o:(.data+0x0): 这里先定义

collect2: 错误:ld 返回 1 个退出状态

/* Main.c*/


#include <stdio.h>
#include "font.h"
#include "test.h"

int main()
{
    printf("c=%s",c);

    return 0;
}


/*test.c*/

#ifndef _test_
#define _test_

#include "test.h"
#include "font.h"

int add(int a, int b)
{
    return (a+b);
}

#endif

/*test.h*/
extern void add(int a, int b);

/*font.h*/
#ifndef _font_
#define _font_

char c[10]="saji";
extern char c[10];
#endif

【问题讨论】:

  • 不是先生。我想对访问 font.h 中的缓冲区进行哪些更改?

标签: c


【解决方案1】:
  1. 在头文件中声明变量,在源文件中初始化变量。
  • 字体.h
extern char c[10];
  • font.c
char c[10] = "saji\0";
  1. 您的 test.c 中有一些问题。函数签名无效,但返回 int。

【讨论】:

  • 很好的答案——但你可以进一步整理它,因为当它只有 extern decl 时,你不应该需要头文件中的#ifndef
【解决方案2】:

在您的font.h 中,您声明了两次char c

char c[10]="saji";
extern char c[10];

extern char c[10]; 移动到 main.c / main.h。声明应该放在 font.h 中,而 extern 应该放在使用 extern 引用的变量的文件中。

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2011-01-11
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多