【问题标题】:Redefinition Issue using Makefile使用 Makefile 重新定义问题
【发布时间】:2013-03-01 23:37:55
【问题描述】:

ma​​in.c:

#include <stdio.h>
#include "proto.h"

int main(void)
{

    return(0);
} // end main

support.c:

#include "proto.h"

\\ only function defintions

proto.h:

#ifndef proto
#define proto
double PI = 3.14159;
int LOOP_LIMIT = 90;

#endif

制作文件:

main: main.o  support.o 
    gcc -lm -o main main.o support.o
main.o: main.c proto.h
    gcc -c main.c
support.o: support.c proto.h
    gcc -c support.c

每当我使用上述定义的文件运行 makefile 时,我总是会遇到多重定义错误,尽管有条件编译。

我不确定这里发生了什么以及如何解决问题。

错误信息是:

multiple definition of `PI'
multiple definition of `LOOP_LIMIT'

【问题讨论】:

  • 包含实际的错误信息。

标签: c linux makefile definition multiple-definition-error


【解决方案1】:

您不能在从多个编译单元包含的标头中定义变量。 PILOOP_LIMIT 最终都出现在 main.osupport.o 中,因此会出现链接器错误。正确的做法是在标题中声明它们extern

proto.h

#ifndef proto
#define proto
extern double PI;
extern int LOOP_LIMIT;

#endif

然后在一个且只有一个 .c 文件中定义它们:

support.c

#include "proto.h"

double PI = 3.14159;
int LOOP_LIMIT = 90;

\\ only function defintions

顺便说一句,看起来这些家伙可能是常量而不是变量,所以要么将它们声明并定义为const,要么将它们写为预处理器定义:

#define PI 3.14159
#define LOOP_LIMIT 90

有了这些,您还可以避免链接问题。

【讨论】:

  • 宾果游戏,这似乎可以解决问题 :) 只需一个注释,为什么 #define 会起作用?是否会尝试重新定义相同的东西,就像变量的多个定义一样。
  • 没有。 #define 几乎像复制/粘贴操作一样工作,由预处理器处理,甚至在编译器看到代码之前。所以如果你写double circumference = PI * diameter;,编译器会看到double circumference = 3.14159 * diameter;。实际符号 PI 根本不会出现在编译的目标文件中。
猜你喜欢
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
相关资源
最近更新 更多