【发布时间】:2021-04-11 08:22:09
【问题描述】:
我创建了两个模块:files.h 和 connection.h。
files.h 包含在connection.h 中。
files.h 使用 usleep() 函数,connection.h 在各个 .c 文件的某个点使用 inet_aton() 函数。这些函数需要以下定义:
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
所以,由于files.h 包含在connection.h 中,我认为我可以在files.h 中编写这些定义,但是当我编译时出现以下错误:
connection.c:23:6: 错误:函数‘inet_aton’的隐式声明
所以我决定尝试在 connection.h 而不是 files.h 中编写这些定义,只是为了编译并得到以下错误:
files.c:298:3: 错误:函数‘usleep’的隐式声明
此时,我的下一个选择是在各自的 .c 文件中编写定义来解决这个问题。但是,我在编译时遇到了这个错误:
files.c:302:3: 错误:函数‘usleep’的隐式声明
connection.c:23:6: 错误:函数‘inet_aton’的隐式声明
我不明白这是什么问题。如何同时使用这两个功能?
files.h
#ifndef _FILES_H_
#define _FILES_H_
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
// ...
#endif
connection.h
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "files.h"
#include <ctype.h>
#include <pthread.h>
// ...
#endif
【问题讨论】:
-
您发布的
files.h不包含指定的宏定义。请发帖Minimal, Reproducible Example。 -
与您的问题无关,但请注意,任何以下划线开头后跟大写字母的符号都是保留的。请参阅this reserved identifier list 中的第 3 点。
-
定义 _XOPEN_SOURCE 500、_POSIX_C_SOURCE 和 _GNU_SOURCE 之前,包括任何可能使用它们的头文件。
-
那是我的错,我再次编辑了问题。我描述的问题发生在包含之前的定义中