简体   繁体   English

在C中包含库失败

[英]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 我在使用Code:Blocks作为IDE编译C语言中的某些库时遇到问题

I have the following code: 我有以下代码:

//main.c

#include "lib1.h"

int main(){

}

And the "lib1.h" is 而“ lib1.h”是

#ifndef GUARD_LIB1
#define GUARD_LIB1

MyTypedef variable123;

#endif

the "lib1.c" is “ lib1.c”是

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

finally, "lib2.h" is 最后,“ lib2.h”是

#ifndef GUARD_LIB2
#define GUARD_LIB2

typedef int MyTypedef;

#endif

But always gives me compilation error, not recognize MyTypedef in "lib1.h", any suggestions? 但总是给我编译错误,无法识别“ lib1.h”中的MyTypedef,有什么建议吗?

The error is: 错误是:

"Unknow type name: 'MyTypedef'"

EIDT: 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 和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? 我应该在main.c中的#include“ EnzoLib.h”之前添加#include吗? I only using functions that i declared in EnzoLib.h 我只使用我在EnzoLib.h中声明的函数

EDIT2: EDIT2:

Is that I am trying to program a neat way. 是我要尝试编写一种整洁的方法吗? Do not put the "#includes" always on ".C files"? 不要将“ #includes”始终放在“ .C文件”上吗?

When compiling main.c , the header lib2.h , which contains the definition of MyTypedef , is never included. 编译main.c ,永远不会包含包含MyTypedef定义的标头lib2.h Therefore, when compiling main.c , the compiler has no idea what that type is. 因此,在编译main.c ,编译器不知道该类型是什么。

That is, after the preprocessor runs, the compiler sees two "translation units" (the standard calls ac file and all the headers it includes collectively a translation unit): 也就是说,在预处理器运行之后,编译器会看到两个“翻译单元”(标准调用ac文件,并且它的所有标头共同包括一个翻译单元):

main.c becomes: main.c变为:

//main.c

#ifndef GUARD_LIB1
#define GUARD_LIB1

MyTypedef variable123;

#endif

int main(){

}

and lib1.c becomes: lib1.c变为:

#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. lib1.c ,编译器知道MyTypedef是什么,但在main.c中则不知道。

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. lib1.h包含lib2.h ,以便使用lib1.h任何人lib1.h可以知道MyTypedef是什么。

Move the included files from the EnzoLib.c to EnzoLib.h and only keep the #include "EnzoLib.h" 将包含的文件从EnzoLib.c移到EnzoLib.h,仅保留#include“ EnzoLib.h”

The contents of the EnzoLib.h should be something like EnzoLib.h的内容应类似于

#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 EnzoLib.c的内容应该在线

#include "EnzoLib.h"

//Freely use the Socket structure
Socket mySocket;

//end of EnzoLib.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM