Enzo
Enzo

Reputation: 105

fail including libraries in C

first I want to say I speak very little English, so excuse my spelling errors.

I am having a problem compiling some libraries in C using Code: Blocks as IDE

I have the following code:

//main.c

#include "lib1.h"

int main(){

}

And the "lib1.h" is

#ifndef GUARD_LIB1
#define GUARD_LIB1

MyTypedef variable123;

#endif

the "lib1.c" is

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

finally, "lib2.h" is

#ifndef GUARD_LIB2
#define GUARD_LIB2

typedef int MyTypedef;

#endif

But always gives me compilation error, not recognize MyTypedef in "lib1.h", any suggestions?

The error is:

"Unknow type name: 'MyTypedef'"

EIDT:

The real code that i have the problem is.

// EnzoLib.c
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include "EnzoLib.h"




// EnzoLib.h

//blah blah blah...

//Estructuras
typedef struct {
    SOCKET sock;
    char nombre[64];
    char activo;
    struct sockaddr_in from;
} Socket;

//blah blah blah...

and main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include "EnzoLib.h"

and the error:

C:\Users\Enzo\Documents\codeblocks\lp1\ServidorChat\EnzoLib.h|6|error: unknown type name 'SOCKET'|

should I add #include before #include"EnzoLib.h" in main.c? I only using functions that i declared in EnzoLib.h

EDIT2:

Is that I am trying to program a neat way. Do not put the "#includes" always on ".C files"?

Upvotes: 0

Views: 156

Answers (2)

Raedok
Raedok

Reputation: 51

Move the included files from the EnzoLib.c to EnzoLib.h and only keep the #include "EnzoLib.h"

The contents of the EnzoLib.h should be something like

#ifndef ENZOLIB_H
#define ENZOLIB_H

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>

typedef struct {
    SOCKET sock;
    char nombre[64];
    char activo;
    struct sockaddr_in from;
} Socket;

#endif
//end of EnzoLib.h

Contents of EnzoLib.c should be online

#include "EnzoLib.h"

//Freely use the Socket structure
Socket mySocket;

//end of EnzoLib.c

Upvotes: 0

Billy ONeal
Billy ONeal

Reputation: 106530

When compiling main.c, the header lib2.h, which contains the definition of MyTypedef, is never included. Therefore, when compiling main.c, the compiler has no idea what that type is.

That is, after the preprocessor runs, the compiler sees two "translation units" (the standard calls a c file and all the headers it includes collectively a translation unit):

main.c becomes:

//main.c

#ifndef GUARD_LIB1
#define GUARD_LIB1

MyTypedef variable123;

#endif

int main(){

}

and lib1.c becomes:

#ifndef GUARD_LIB2
#define GUARD_LIB2

typedef struct int MyTypedef;

#endif
#ifndef GUARD_LIB1
#define GUARD_LIB1

MyTypedef variable123;

#endif

In lib1.c the compiler knows what MyTypedef is, but in main.c it does not.

If you want to use a type in a header, then the header should include its dependencies. Have lib1.h include lib2.h, so that anyone who uses lib1.h can tell what MyTypedef is.

Upvotes: 1

Related Questions