Why when I include my .h header, is the .c implementation not included too?

2

I have three files, produtos.h , produtos.c and main.c . produtos.h is located in the "headers" folder, produtos.c is located in "sources" and main.c is in the same folder as "headers" and "sources", something like this:

pasta
\_headers
| \_produtos.h
\_sources
| \_produtos.c
\_main.c

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "headers/produtos.h"

int main(int argc, char** argv){
     struct produto p;
     p.id = 123;

     printf("Id: %d\n", p.id);

     return 0;
}

products.h:

#ifndef PRODUTOS_H
#define PRODUTOS_H

struct produto;

#endif

products.c:

#include "../headers/produtos.h"

struct produto {
    char nome[100];
};

When compiling, I get the following error message:

main.c: In function 'main':
main.c:6:20: error: storage size of 'p' isn't known
   struct produto p;
                   ^

    

asked by anonymous 08.10.2018 / 17:23

1 answer

1

First of all, do not make any reason that if you want to type da and press d , a will not go alone, after all you would have to guess whether you want it or not. But there is another problem

sruct is only declaration, there is no implementation of struct so this C code does not make sense, do so in header :

struct produto {
    char nome[100];
};

And it's solved.

    
08.10.2018 / 17:39