0

Honestly I'm not used to working in c and maybe I'm wrong as I approach the problem.

I have 3 files and I need to pass data from one to another

//main.c

#include <stdio.h>
#include "process.h"
#include "readFile.h"    

int main() {
    int numIn=2, numOut=1;

    struct data *allData=readData(numIn, numOut);

    process(allData, numIn, numOut);

    return 0;
}

// readFile.h

#include <stdio.h>
#include <stdlib.h>

struct data {
    double *in;
    double *out;
};


struct data * readData(int numIn, int numOut) {
   //here I initialize and fill an "allData" array of struct data

   return allData;
}

//process.h

#include <stdio.h>
#include <stdlib.h>
#include "readFile.h"

int process(struct data * allData, int numIn, int numOut) {

    return 0;
}

If I delete "process.h" and try to print "allData" in the main, the correct data are printed without errors, but when I try to process the data in "process.h" I get this compilation error:

In file included from C:\...\main.c:4:0:
C:\...\readFile.h:11:8: error: redefinition of 'struct data'
 struct data
        ^
In file included from C:\...\process.h:11:0,
                 from C:\...\main.c:2:
C:\...\readFile.h:11:8: note: originally defined here
 struct data
        ^
In file included from C:\...\main.c:4:0:
C:\...\readFile.h:24:15: error: conflicting types for 'readData'
 struct data * readData(int numIn, int numOut)
               ^
In file included from C:\...\process.h:11:0,
                 from C:\...\main.c:2:
C:\...\readFile.h:24:15: note: previous definition of 'readData' was here
 struct data * readData(int numIn, int numOut)
               ^
Dr. Slump
  • 3
  • 3
  • This has nothing to do with global `struct`s. Your issue is that you've *defined* a non-inline function in a header file, so it gets compiled and linked multiple times. Function definitions must either be in *source* (`.c`) files or must be inline. – jamesdlin Jan 19 '19 at 19:31

1 Answers1

0

Do not place any code in the .h files

.h files are for the data declarations, extern variables declarations and function declarations.

.c files are correct place to have variable and functions definitions.

Move all the code form the .h files to .c files

Also add the .h file guards. It is just the definition. If this definition is already defined - it means that this file was already included and its content should be skipped

#ifdef MYGUARD_H
#define MYGUARD_H

/* .h file content

#endif
0___________
  • 60,014
  • 4
  • 34
  • 74