C程序不能在WSL上编译,但在Linux上运行良好



我有下一个问题,我在笔记本上做了C的作业,我有Linux系统,我的作业编译得很好,工作得很好,但是,当我试图执行make命令在我的Pc上执行它(Windows 10使用WSL)时,我得到了这个错误。

make
gcc -o output main.o Unario.o Frec.o Inc.o ui.o utilities.o
/usr/bin/ld: Unario.o:(.bss+0x0): multiple definition of `c'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: Unario.o:(.bss+0x4): multiple definition of `g'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: Unario.o:(.bss+0x8): multiple definition of `order'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: Frec.o:(.bss+0x0): multiple definition of `c'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: Frec.o:(.bss+0x4): multiple definition of `g'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: Frec.o:(.bss+0x8): multiple definition of `order'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: Inc.o:(.bss+0x0): multiple definition of `c'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: Inc.o:(.bss+0x4): multiple definition of `g'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: Inc.o:(.bss+0x8): multiple definition of `order'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: ui.o:(.bss+0x0): multiple definition of `c'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: ui.o:(.bss+0x4): multiple definition of `g'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: ui.o:(.bss+0x8): multiple definition of `order'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: utilities.o:(.bss+0x0): multiple definition of `c'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: utilities.o:(.bss+0x4): multiple definition of `g'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: utilities.o:(.bss+0x8): multiple definition of `order'; main.o:(.bss+0x8): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:2: output] Error 1

这些cgorder是位于头文件中的变量,我将这个文件包含在我的所有文件中,基本上它是一个全局变量。

这是我的makefile

output: main.o Unario.o Frec.o Inc.o ui.o utilities.o
gcc -o output main.o Unario.o Frec.o Inc.o ui.o utilities.o
main.o: main.c
gcc -c main.c
Unario.o: Unario.c Unario.h
gcc -c Unario.c
Frec.o: Frec.c Frec.h
gcc -c Frec.c
Inc.o: Inc.c Inc.h
gcc -c Inc.c
ui.o: ui.c ui.h
gcc -c ui.c
utilities.o: utilities.c utilities.h
gcc -c utilities.c
clean:
rm *.o output

我不知道是什么问题。

我会显示完整的程序,但有许多文件和大约3000行代码(包括注释和换行符)

这是utilities.h文件

#ifndef UTILITIES_H
#define UTILITIES_H
// We save the numbers c and g given by the user
int c;
int g;
// This variable is used to save what representation was used
char *order;
// ---- Declaration of functions ----
#endif

This is main.c

#include<stdio.h>
#include<stdlib.h>
#include"Unario.h"
#include"Frec.h"
#include"Inc.h"
#include"ui.h"
#include"utilities.h"
// Main function
int main(){
// We print the welcome screen
welcome_screen();
// We request the number of elements of the array, and the number of elements of each divition
// We have c and g declared in utilities.h
// Idea from the teacher assistant, but it's only used in Inc.c, because in the others functions we calculate c in base of how many 0 exist in the representation
c=request_c();
g=request_g();
// We request the list
int *list=request_list(c);
// We send the groups to compress, and return the structure compressed with the best representation for each group
Conjunto_comprimido *conjunto_comprimido=comprimir(c, list, g);
// We call the menu, passigng the structure with the compressed groups
menu((void*)conjunto_comprimido);
// We free the memory
free(list);
memory_cleanner(conjunto_comprimido);
}

这是一个ui

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include"ui.h"
#include"utilities.h"

// This function request the list of numbers
int *request_list(int c){
// We says to the user that he must enter the numbers
printf(" * Ingrese los numeros de la lista separados por espacios: n");
printf(" > ");
// We create the list with numbers, adding a extra position to save a 0 like a end of the list
int *list=malloc(sizeof(int)*(c+1));
// This counter will be used to save the position of the list
int counter=0;
// Here is readed the separator and the line jump
char var;
// We read a string of c numbers separated by space
while(var!='n'){
scanf("%d%c", &list[counter], &var);
counter++;
}
// We put a 0 like a flag to know the end of the list
list[counter]=0;
// We return the list
return list;
}
// This function request the number of elements for each subdivition
int request_g(){
int g;
// Here we request the g number
printf(" * Ingrese el numero de elementos por grupo: ");
scanf("%d", &g);
return g;
}
// This function request the quantity of elements
int request_c(){

int c;
// Here we request the c number
printf(" * Ingrese el numero de elementos de la lista: ");
scanf("%d", &c);
// We return the scaned number
return c;
}
// ---- Other functions ----

在c语言中我使用gfor循环,例如

// We clean the posible trash data in the structure
for(int j=0; j<g; j++){
incremental[j].number=0;
incremental[j].representacion_size=0;
incremental[j].bits=NULL;
}

这些c, g和order是位于头文件中的变量,我将这个文件包含在我的所有文件中,基本上它是一个全局变量。

这正是你的问题所在。当.c文件包含该头文件时,整个内容被"复制"。到该编译单元并独立编译。因此,当链接器试图使用这些文件时,它会告诉你该变量在多个位置定义。

一个简单的解决方法是在.h文件中将变量声明为extern,如下所示:
// my_header.h
extern int c;

和初始化它们在一个.c文件,像这样:

// my_vars.c
int c = 0;

相关内容

最新更新