'Program in C not compile on WSL but on Linux works perfectly [duplicate]

I have the next problem, I did a homework of C in my notebook, I have Linux system there, and my homework compiles good and works perfectly, but, when I try to execute the make command to execute it in my Pc (Windows 10 using WSL) I get this error.

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

These c, g and order are variables located in a header file, and I include this file in all of my files, basically It's a global variable.

This is my 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

I don't know what could be the problem.

I would show the complete program but are many files and approximately 3000 lines of code (With comments and line breaks)

This is the utilities.h file

#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);
}

This is a ui.c

#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 ----

And in Inc.c I use the g for loops, for example

// 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;
}


Solution 1:[1]

These c, g and order are variables located in a header file, and I include this file in all of my files, basically It's a global variable.

And that's exactly what your problem is. When a .c file includes that header, the whole content is "copied" to that compilation unit and compiled independently. So when the linker tries to use those files it will tell you that the variable is defined in multiple locations.

An easy way to get around that is to declare the variables as extern in the .h file, like this:

// my_header.h
extern int c;

And the initialized them in ONLY ONE .c file, like this:

// my_vars.c
int c = 0;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JuanR