Splitting project into multiple headers causes error: Dereferencing Pointer to incomplete type

Asked

Viewed 317 times

-2

I was wondering how to divide a project into several files. I had created a project divided into 3 files first, one main. c (containing main function), a lib.c (implementing functions used in main.c) and a lib.h (containing prototypes of functions), "uniting" files using #include "lib.h" in main. c and lib.c - and it worked perfectly.

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

"error: Dereferencing Pointer to incomplete type"

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

More information about my project: Many of the functions use implementations that are in other "file. c". I am using Codeblock and Devc++. A 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 files. c and file. h)

How do I make it work?

  • Po, who edits there my question edits wrong, together libraries. HAS MORE THAN ONE LIB... LIB1.H, LIB2.H, LIB3.H...

  • Hobbit @L. edition does not seem to have removed anything http://answall.com/revisions/160260/2, but if something goes wrong just edit again, the edit only occurred because you used the Stacksnippets unnecessarily. Ps: I edited the title to be more intuitive to the problem ;)

  • @Hobbit I just normalized your post, didn’t remove anything, you just posted the 2 file snippet .h, lib1 and lib2

  • I’ve already edited, but I may have made a mistake, in which case I’m sorry.

  • I understood that this is wrong. You should put the structs inside the files . h, because it is as if the main. c knows that there is a typedef struct but does not know what it has in struct.. (more or less that).

1 answer

2


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

Here are some tips in your code:

Main is calling the header files and lib1.c also calls the same files. Imagine you have 50 files. c This means that the 50 files will call the header. Resolve: Use the #define directory 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 within ifndef and there it defines LIB1. When lib1.c calls the same file it will do again the ifndef test that main has already run and already set with a LIB1 constant, it ignores the entire code block. These macros with # are used for compilation, so abuse them that they serve for that very reason. I was clear?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.