gpt4 book ai didi

c - 多重定义和首次定义错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:54:10 28 4
gpt4 key购买 nike

我写了一个由几个文件组合而成的小 C 程序。编译时,出现“多个定义”错误。

我的主.c:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "general_structs.h"

#define FOREVER for(;;)
#define INPUT_LEN 30

int main()
{
char command[INPUT_LEN];
char *func;
int i;
int t;

FOREVER
{
if(scanf("%s", command) == 1)
{
func = strtok(command, " ");
for(i=0;cmd[i].func != NULL;i++)
{
if(strcmp(func, cmd[i].name) == 0)
{
(*((cmd[i].func)));
t = 1;
}
}
if(t == 1)
{
printf("No such command");
}
}
}
return 0;
}

我的 mat.c 文件:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "general_structs.h"

#define LENGTH 100
#define SIXTEEN 16
#define SIZE 4


void read_mat()
{
int i = 0;
int j = 0;
int k = 0;
char tmp_name[LENGTH];
char num_buffer[LENGTH];
char *token;
double num_list[16];
double tmp_num = 0;

scanf("%[^,], %s", tmp_name, num_buffer);

token = strtok(num_buffer, ",");

while(token != NULL)
{
if(strcmp(token, "0") == 0)
{
num_list[i] = 0;
}
else
{
tmp_num = atof(token);
if(tmp_num == 0)
{
printf("Error in parameter: %d\n", (i-1));
break;
}
else
{
num_list[i] = tmp_num;
}
}
i++;
token = strtok(NULL, ",");
}

if(!strcmp(tmp_name, "MAT_A"))
{
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
mats[0].mat[0][i][j] = num_list[k];
k++;
}

}
else if(!strcmp(tmp_name, "MAT_B"))
{
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
mats[1].mat[0][i][j] = num_list[k];
k++;
}
}
else if(!strcmp(tmp_name, "MAT_C"))
{
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
mats[2].mat[0][i][j] = num_list[k];
k++;
}
}
else if(!strcmp(tmp_name, "MAT_D"))
{
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
mats[3].mat[0][i][j] = num_list[k];
k++;
}
}
else if(!strcmp(tmp_name, "MAT_E"))
{
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
mats[4].mat[0][i][j] = num_list[k];
k++;
}
}
else if(!strcmp(tmp_name, "MAT_F"))
{
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{
mats[5].mat[0][i][j] = num_list[k];
k++;
}
}
else
{
printf("No such matrix name.");
}
}

我的 general_structs.h 文件:

#define SIZE 4
#define SIZE_NAME 5
#define SIZE_FUNC 10

typedef double matrix[SIZE][SIZE];

matrix MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;

void read_mat(void);

struct
{
char name[SIZE_NAME];
matrix *mat;
} mats[] = {
{"MAT_A", &MAT_A},
{"MAT_B", &MAT_B},
{"MAT_C", &MAT_C},
{"MAT_D", &MAT_D},
{"MAT_E", &MAT_E},
{"MAT_F", &MAT_F},
{"non", NULL}
};

struct
{
char name[SIZE_FUNC];
void (*func)(void);
} cmd[] = {
{"read_mat", read_mat},
{"not_valid", NULL}
};

我的制作文件:

int_loop: my_math.o int_loop.o
gcc -g -ansi -Wall -pedantic my_math.o int_loop.o -o int_loop

int_loop.o : int_loop.c
gcc -c -ansi -Wall -pedantic int_loop.c -o int_loop.o

my_math.o : my_math.c
gcc -c -ansi -Wall -pedantic my_math.c -o my_math.o

我一直在尝试用各种技术解决这个问题,但都没有成功。

我收到的错误是:

gcc -g -Wall -ansi -pedantic main.o mat.o -o mamantest
mat.o:(.data+0x0): multiple definition of `mats'
main.o:(.data+0x0): first defined here
mat.o:(.data+0x70): multiple definition of `cmd'
main.o:(.data+0x70): first defined here
collect2: ld returned 1 exit status
make: *** [mamantest] Error 1

为什么会出现这个错误?我该如何解决这个问题?

谢谢

最佳答案

在头文件中,您定义变量matscmd,意思是translation units (包含头文件的两个源文件)将定义这些文件。

变量应该只定义在一个地方,在一个源文件中,比如

struct mat mats[7] = { ... };

上面的定义数组mats,就像我说的那样应该只在一个地方完成。

对于其他源文件,您可以声明变量,这可以在头文件中完成,例如

extern struct mat
{
...
} mats[7];

上面声明变量mats为一个包含七个mat结构的数组。它还定义了结构,因此它可以用于例如定义数组。


经过上面建议的修改后,完整的头文件应该是这样的

// First header include guards (see https://en.wikipedia.org/wiki/Include_guard)
#ifndef GENERIC_STRUCTS_H
#define GENERIC_STRUCTS_H

#define SIZE 4
#define SIZE_NAME 5
#define SIZE_FUNC 10

typedef double matrix[SIZE][SIZE];

// Declare the variables (note the added use of the extern keyword)
extern matrix MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;

void read_mat(void);

// Define a structure named mat (note added structure tag name)
struct mat
{
char name[SIZE_NAME];
matrix *mat;
};

// Define a structure named command (note added structure tag name)
struct command
{
char name[SIZE_FUNC];
void (*func)(void);
};

// Now declare variables of the previous structures
extern struct mat mats[7];
extern struct command cmd[2];

// End of header include guard
#endif

该头文件仅声明变量,并且可以包含在您所有的源文件中。

然后在单个 源文件(例如您的main.c 文件)中进行实际的变量定义:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "general_structs.h"

matrix MAT_A, MAT_B, MAT_C, MAT_D, MAT_E, MAT_F;

struct mat mats[7] = {
{"MAT_A", &MAT_A},
{"MAT_B", &MAT_B},
{"MAT_C", &MAT_C},
{"MAT_D", &MAT_D},
{"MAT_E", &MAT_E},
{"MAT_F", &MAT_F},
{"non", NULL}
};

struct command cmd[2] = {
{"read_mat", read_mat},
{"not_valid", NULL}
};

#define FOREVER for(;;)
#define INPUT_LEN 30

int main()
{
...
}

在这里您需要了解的重要一点是,声明定义 之间是有区别的。

声明基本上是告诉编译器“这个东西存在于某处”,而定义是告诉编译器“这东西”。

问题是,除非已经声明了一个事物,否则定义也是声明,许多人简单地将这些组合的定义/声明称为声明,这有点混淆了整个概念。

关于c - 多重定义和首次定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397611/

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