重复符号错误 - 全局变量

时间:2013-02-07 23:36:39

标签: c linker linker-errors

所以我有三个文件:

jarvismarch.c
jarvismarchtools.c
jarvismarchtools.h

运行make后,收到以下错误:

Andrew-Carpenters-Laptop:Independent Study ahcarpenter$ make
cc    -c -o jarvismarch.o jarvismarch.c
cc    -c -o jarvismarchtools.o jarvismarchtools.c
cc -o jarvismarch jarvismarch.o jarvismarchtools.o
ld: duplicate symbol _string1 in jarvismarchtools.o and jarvismarch.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

string 1jarvismarchtools.c内宣布。 jarvismarchtools.h以及jarvismarchtools.c中都包含jarvismarch.c

关于如何解决此错误的任何想法?

jarvismarchtools.h内:

/*
    FILENAME: jarvismarchtools.h
    AUTHOR: Andrew H. Carpenter
    DATE: 2 Feb 2013
    DESCRIPTION: This is a header file containing tools for running Jarvis' March.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

extern int string1 = 1;

typedef struct Point {
    double x, y;
    char *label;
} point;

/*
    DESCRIPTION: A function that determines the angle (in degrees) between two points.
    INPUT: Requires two points as input.

jarvismarchtools.c内:

/*
    FILENAME: jarvismarchtools.c
    AUTHOR: Andrew H. Carpenter
    DATE: 2 Feb 2013
    DESCRIPTION: This is an implemenetation file containing tools for running Jarvis' March.
*/
#include "jarvismarchtools.h"

int string1 = 1;

float getAngle(point p1, point p2){

1 个答案:

答案 0 :(得分:6)

如果应该共享变量:

  • 在标题
  • 中设置extern
  • 在没有extern
  • 的其中一个C文件中定义它

如果不应该共享变量,请在前面添加static

相关问题