Why is it that when I include my . h header, the . c implementation is not included as well?

Asked

Viewed 73 times

4

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

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;
                   ^

As my tests indicate, the compiler cannot receive the implementation of struct product, that is, it cannot receive produtos.c, only produtos.h, what my mistake?

1 answer

2


First, don’t do it for the same reason that if you want to type da and tightens the d, the a won’t go alone, after all would have to guess whether you want it or not. But there’s another problem

sruct is just statement, there is no implementation of struct so this C code doesn’t make sense, so do header:

struct produto {
    char nome[100];
};

I put in the Github for future reference.

And it’s settled.

  • This solves, yes, but the problem will continue if instead of me declaring a struct, I declare a function. I can’t put the entire implementation of a function in my header.

  • 1

    But you put the function header in the header... no?

  • 1

    Implementation does not need to be included, so no problem.

Browser other questions tagged

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