0

As I said, im getting this type of error in my current program(Working in C with CodeBlocks IDE mingw 64):

C:\msys64\mingw64\bin..\lib\gcc\x86_64-w64-mingw32\10.1.0........\x86_64-w64-mingw32\bin\ld.exe: obj\Debug\main.o:C:\Users\lgmfo\Desktop\SimpleDiet\simplediet.h|1|multiple definition of `matrixSize'; obj\Debug\filesManager.o:C:/Users/lgmfo/Desktop/SimpleDiet/simplediet.h:1: first defined here|

I really dont have a clue of what exactly is going on, because the entire code was working just fine untill I played with the values of some random variable(it has nothing to do with the header file).

My header file looks like this:

int matrixSize[2], nReal, nPosible;
float *targetParameters;
float **diets;

void Inspector(void);
int Combinations(int);
void MatrixSelector(int *, int, int, int);
void ViableDiets(int *, float *, int);
void NutritionPlan(void);
void GaussJordan(float matrix[][2*matrixSize[0]], float *, int);
void Menu(void);
void MergeDiets(void);
void PrintDiets(void);

As you might already guess, my program is structured in separate files, and the following is the filesManager file( this is the file where im getting the error, but i assume that it is the same for all of them):

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

void Inspector(void){
    FILE *fp;
    int c;
    int conti = 0, contj = 0;

    if((fp = fopen("TablaNutricionalPlatillos.txt","r")) == NULL){
        printf("No se puede abrir el archivo %s\n", "TablaNutricionalPlatillos.txt");
    }
    else{
        while((c = getc(fp)) != EOF){
            if((c == '.') && (contj == 1)){
                conti++;
            }
            else if(c == '\n'){
                contj++;
            }
        }
    }
    fclose(fp);
    matrixSize[0] = conti;
    matrixSize[1] = contj;
}

The message says that im defining multiple times the variable matrixSize, but i´m really not(based in the fact that it was working fine before this happended) because in the simpleDiet.h file i'm just declaring the variables, not defining them. So I really dont know what is causing the problem here. I think it has nothing to do with the actual code but the linkers or the compiler(I´m not really an expert in all of this)

Thanks

  • The simple answer is not to have definitions in header files. – Weather Vane Jun 20 '20 at 20:18
  • Do you have multiple .c including that .h? I so, so each of the compilation units define `matrixSize`, ending up with multiple variables with the same name. You should at most declare the array in the header file (e.g. use `extern`), while defining it in only one of the .c files. – ikegami Jun 20 '20 at 20:19

2 Answers2

3

I think it has nothing to do with the actual code but the linkers or the compiler(I´m not really an expert in all of this)

First rule: assume the problem is with your code first before blaming the tools.

i'm just declaring the variables, not defining them.

You are in fact defining the variables in your header file:

int matrixSize[2], nReal, nPosible;
float *targetParameters;
float **diets;

These are technically tentative definitions (because they are not initialized) which become true definitions if they don't appear anyplace else. This means that any .c file that includes simplediet.h will contain a definition of these variables, so when you link them you get a multiple definition error.

In your header file, apply the extern qualifier to each of these:

extern int matrixSize[2], nReal, nPosible;
extern float *targetParameters;
extern float **diets;

This makes them declarations. Then add the definitions to exactly one source file.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • thank you for your answer, it is now working again. I just wanted to ask why it was working in the past. I really didnt change any of that – LGM.vehnvey Jun 20 '20 at 20:33
  • 1
    @LGM.vehnvey There's no way to know that without knowing exactly what your code looked like before. – dbush Jun 20 '20 at 20:35
0

int matrixSize[2], nReal, nPosible; is a tentative declaration, per C 2018 6.9.2 2. If there is no external definition for those identifiers, the tentative declaration acts as a definition.

To make it a declaration that is not a definition, insert extern: extern int matrixSize[2], nReal, nPosible;.

Then provide a definition inside one source file (int matrixSize[2], nReal, nPosible; would suffice).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312