adriel1019
adriel1019

Reputation: 1

Compile structs in c separate files

I want to compile two C separate files, one which will contain a struct type like

typedef struct {  
  int renglones;  
  int columnas;  
} Matriz

And in another file make the call to that struct and use it, but when I try to compile but files, mark error, I want to know if there's a way to do this, or can't be done.

Upvotes: 0

Views: 679

Answers (1)

Giuseppe Pes
Giuseppe Pes

Reputation: 7912

I haven't fully understood what you are trying to do. However, the definition of structure should be within and header file, for example a Matriz.h. Once you have define this file you can use the new structure by including Matriz.h via the include keyword in both files. This is the correct way to define a structure which is used by multiple files.

For example :

Matriz.h

#ifndef MATRIZ_H
#define MATRIZ_H

typedef struct {  
    int renglones;  
    int columnas;  
} Matriz;


#endif /* end of include guard: MATRIZ_H */

Then in your c file , you can just use

#include "Matriz.h" 

You get a error because the compile does not know the size of the Matriz object when it is compiling the file where the structure is not defined.

Let me know if you need more info.

Upvotes: 2

Related Questions