Search code examples
cstructcompiler-errorsinclude

C struct type not found including library, error "unknown type name"


I didn't find a solution to a very simple problem.

File "commons.h":

struct test_struct{
   int a;
}

File "work.c":

#include "commons.h"
void myfunction(test_struct this_is_a_test){
// ....
}

what I do:

gcc commons.c -c -o commons.o    (no errors)
gcc work.c -c -o work.o ( "unknown type name "test_struct")

What am I doing wrong?

I have also another .c which include "commons.h" and while compiling everything is fine, only with work.c I get error.


Solution

  • Your header file only defines struct test_struct, not test_struct. You need a typedef so you can refer to it without saying struct first.

    typedef struct test_struct {
        int a;
    } test_struct;
    

    Or change work.c to use struct.

    void myfunction(struct test_struct this_is_a_test) {
        //...
    }