When splitting project into multiple headers the error occurs: dereferencing pointer to incomplete type

-2

I was wondering how to split a project into multiple files. I had created a project divided into 3 files first, a main.c (containing main function), a lib.c (implementation of the functions used in main.c) and a lib.h (contains function prototypes), "uniting" the files using #include "lib.h" in main.c and lib.c - and worked perfectly.

But now I need to split the project (the same I finished using only 3 files) into more files, eg main.c, lib1.c, lib1.h, lib2.c, lib2.h ... being almost all functions remain the same. But now I can not 'make it work' and I get messages like:

  

"error: dereferencing pointer to incomplete type"

I would like to know how to "merge" these files when compiling.

More information about my project: Many of the functions use implementations that are in other "file.c". I'm using CodeBlock and DevC ++. An example of how the code is now:

main.c:

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


#include "lib1.h"
#include "lib2.h"
#include "lib3.h"
...

int main()
{
...
   [uso das funções...]
...
}

lib1.c

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


#include "lib1.h"
#include "lib2.h"
#include "lib3.h"
...

struct item1{
   ...
};

ITEM1* funcao1(ITEM2* lista, int valor)
{
...
}

ITEM1* funcao2(ITEM1* lista)
{
...
}
...

lib1.h

typedef struct item1 ITEM1;

ITEM1* funcao1(ITEM2* lista, int valor);
ITEM1* funcao1(ITEM1* lista);
...

lib2.h

typedef struct item2 ITEM2;

ITEM2* funcao5(...);
...

... (other .c and .h files)

How do I make it work?

    
asked by anonymous 20.10.2016 / 21:04

1 answer

2

The main is calling lib1.h and trying to compile typedef struct item1 ITEM1; But lib1.c has not yet been called to declare the struture. The lib1.c file will still be called for compilation. That is you can not use typedef struct for a structure that does not yet exist. The solution should put the declaration in the header and you use typedef no .c Was I clear?

Here are some tips in your code:

The main is calling the header files and lib1.c also calls the same files. Imagine that you have 50 .c files so the 50 files will call the header. Solve: Use the #define directive Example lib1.h:

#ifndef LIB1
#define LIB1
/* lembra de mudar essa linha */
typedef struct item1 ITEM1;

ITEM1* funcao1(ITEM2* lista, int valor);
ITEM1* funcao1(ITEM1* lista);
...
#endif

Main calls lib1.h. ifndef (if = if, n = no, def = defined) it executes the code inside of ifndef and there it defines LIB1. When lib1.c calls the same file it will redo the ifndef test that the main has already run and has already defined with a LIB1 constant, it ignores the entire block of code. These macros with # are used for compilation, so abuse them as they are for this. I was clear?

    
22.10.2016 / 06:27