gpt4 book ai didi

c - 重复符号错误 - 全局变量

转载 作者:行者123 更新时间:2023-12-02 08:43:09 25 4
gpt4 key购买 nike

所以我有三个文件:

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.cjarvismarch.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){

最佳答案

如果变量应该共享:

  • 在标题中使其成为extern
  • 在没有 extern 的 C 文件之一中定义它

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

关于c - 重复符号错误 - 全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14763190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com