如何修复 C 中的错误或警告:“函数‘inet_aton’的隐式声明”;您的意思是“inet_pton”吗?
我无法使用 inet_aton()
<< /a>要将ASCII IP地址转换为“ 192.168.0.1”
在网络字节顺序中的 struct in_addr
地址,因为我无法将代码编译。
在这里是我的构建命令:
gcc -Wall -Wextra -Werror -O3 -std=c17 \
socket__geeksforgeeks_udp_server_GS_edit_GREAT.c -o bin/server \
-lm && bin/server
这是我的命令和错误输出:
eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 socket__geeksforgeeks_udp_server_GS_edit_GREAT.c -o bin/server -lm && bin/server
socket__geeksforgeeks_udp_server_GS_edit_GREAT.c: In function ‘main’:
socket__geeksforgeeks_udp_server_GS_edit_GREAT.c:159:15: error: implicit declaration of function ‘inet_aton’; did you mean ‘inet_pton’? [-Werror=implicit-function-declaration]
retcode = inet_aton("127.0.0.1", &addr_server.sin_addr);
^~~~~~~~~
inet_pton
cc1: all warnings being treated as errors
我要修复的部分是:
implicit declaration of function ‘inet_aton’; did you mean ‘inet_pton’? [-Werror=implicit-function-declaration]
i am 包括 arpa/inet.h
,其中包含<<的声明代码> inet_aton(),在我的源文件的顶部,所以我知道这不是问题:
#include <arpa/inet.h>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番研究,我明白了!
(如果您偶然发现这个问题,请首先确保您的文件顶部有
#include
,如问题中所述。)事实证明,将
-std=c17
比仅仅保留该部分更具限制性。如果没有-std=c17
,gcc 编译器将包含更多不属于 C 标准的功能,包括 POSIX 功能、gcc 扩展和特殊的 Linux 系统功能。因此,使用-std=c17
会导致inet_aton()
的声明从 中排除。
,即使它本来会被包含在内。以下是各种修复,包括。使用
-std=gnu17
与-std=c17
:-Werror
构建命令,以便将该错误作为警告而不是错误进行编译。-std=c17
以便您获得 gcc 提供的额外功能,而不是局限于 C 标准。-std=gnu17
而不是-std=c17
(谢谢,@ikegami!)-D_DEFAULT_SOURCE
到您的构建命令中,为整个程序定义宏_DEFAULT_SOURCE
,以便 gcc 允许来自的
为您提供(更多信息请参见下文)。现在这是我的完整构建命令:inet_aton()
#define _DEFAULT_SOURCE
添加到主源文件的顶部之前,包括任何标头,以便它在包含标头之前定义,从而允许在包含
的位置使用inet_aton()
。inet_pton()
“互联网演示(文本)到网络(二进制)格式”功能(请参阅 此处 和 此处)而不是非 POSIXinet_aton()
“互联网 ascii 字符串到网络”函数(参见 此处)。请参阅下面的更多内容。这些选项中的任何一个都有效。我推荐选项 3、4 或 5,但我最喜欢 3,然后是 4。不过,在所有情况下,我都计划也实施选项6。
gcc 中的“功能测试宏”
上面的选项 4 和 5 如何工作?我了解到它们是 gcc 的“功能测试宏”系统的一部分。在这里阅读所有相关内容: https://man7.org/linux/手册页/man7/feature_test_macros.7.html。本质上,当您包含头文件时,GNU glibc 库实现的某些功能被排除,除非在包含该头文件之前定义了某些“功能测试宏”。
请查看
inet_aton()
的man
页面:https://man7.org/linux/man-pages/man3/inet.3.html。它在页面顶部“概要”部分的底部指出:因此,使用
ldd --version
检查您的 glibc 版本。我的显示2.27
:这意味着上面的
Since glibc 2.19:
部分适用于我,我必须定义“功能测试宏”_DEFAULT_SOURCE
在包含任何标头之前的源代码中(我上面的选项 5),或 在构建命令中(我上面的选项 4)包含这些功能,包括inet_aton()
。简单地省略
-std=c17
也可以,因为 feature_test_macros 手册页(也可通过man feature_test_macros
在命令行中使用)指出:和:
请注意,它提到了
-std=c99
。这个概念适用于我对-std=c17
的使用,因此我必须定义_DEFAULT_SOURCE
来对抗-std=c17
创建的功能删除效果。代码>.只需使用 POSIX 兼容的
inet_pton()
而不是非 POSIXinet_aton()
最后,这是一个比使用非 POSIX 更好的选择 - POSIX函数
inet_aton()
(“ASCII字符串到网络”函数)是使用POSIX函数inet_pton()
(“表示字符串到网络”函数)。此源(https://man7.org/linux/man-pages /man3/inet.3.html)说(强调):
inet_pton()
甚至更好,可以使用问题中我的原始构建命令来使用,如下所述:https://man7.org/linux/man-pages/man3/inet_pton.3.html。它不需要需要任何 gcc 功能测试宏来包含它,并且它比inet_aton
具有更多功能,并且可以处理两个 IPv4 地址系列 (AF_INET
)和 IPv6 地址族 (AF_INET6
)。另请参阅 gcc glibc 一页用户手册。以下是
int inet_pton (int af, const char *cp, void *buf)
部分的链接:https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-inet_005fpton(添加了重点):inet_aton()
和inet_pton()
的完整示例对于我的完整示例代码(包括错误检查),请在此文件中搜索
inet_aton
和inet_pton
(推荐),在我的 eRCAGuy_hello_world 仓库:socket__geeksforgeeks_udp_server_GS_edit_GREAT.c。示例:这是我的
inet_pton()
< /a> 示例代码:After a bunch of study, I figured it out!
(If you are stumbling upon this question, first ensure you have
#include <arpa/inet.h>
at the top of your file, as already stated in the question.)It turns out that putting
-std=c17
is more-restrictive than just leaving that part off. WithOUT-std=c17
, the gcc compiler includes many more features which are not part of the C standard, including POSIX features, gcc extensions, and special Linux system features. So, using-std=c17
causes the declaration ofinet_aton()
to get excluded from<arpa/inet.h>
, even though it would otherwise be included.Here are the various fixes, incl. using
-std=gnu17
vs-std=c17
:-Werror
from the build command so that it compiles with that error as a warning instead of as an error.-std=c17
so that you get the extra features provided by gcc instead of being limited to the C standard.-std=gnu17
instead of-std=c17
(thanks, @ikegami!)-D_DEFAULT_SOURCE
to your build command to define the macro_DEFAULT_SOURCE
for your entire program, so that gcc will allow ininet_aton()
from<arpa/inet.h>
for you (more on this below). Here is my full build command now:#define _DEFAULT_SOURCE
to the top of your main source file before including any headers, so that it is defined before your headers get included, thereby allowing ininet_aton()
from where you include<arpa/inet.h>
.inet_pton()
"internet presentation (textual) to network (binary) format" function (see here and here) instead of the non-POSIXinet_aton()
"internet ascii string to network" function (see here). See more on this below.Any of those options work. I recommend option 3, 4, or 5, but I prefer 3 the most, and 4 after that. In all cases, I plan on also implementing option 6, however.
"Feature test macros" in gcc
How do options 4 and 5 above work? I learned they are part of gcc's "feature test macros" system. Read all about it here: https://man7.org/linux/man-pages/man7/feature_test_macros.7.html. In essence, certain features of gnu's glibc library implementation are excluded as you include header files unless certain "feature test macros" are defined before you include that header.
Go look at the
man
page forinet_aton()
: https://man7.org/linux/man-pages/man3/inet.3.html. It states at the bottom of the "SYNOPSIS" section at the top of the page:So, check your version of glibc with
ldd --version
. Mine shows2.27
:That means the part that says
Since glibc 2.19:
above applies to me, and I must define the "feature test macro"_DEFAULT_SOURCE
in the source code before including any headers (my option 5 above), or in the build command (my option 4 above) to include these features, includinginet_aton()
.Simply leaving off
-std=c17
also works because the feature_test_macros man page (also available at the command-line viaman feature_test_macros
) states:and:
Notice that it mentions
-std=c99
. That concept applies to my usage of-std=c17
, so I have to define_DEFAULT_SOURCE
to counter the feature-removal effect created by-std=c17
.Just use the POSIX-compliant
inet_pton()
instead of the non-POSIXinet_aton()
Lastly, an even better option than using the non-POSIX function
inet_aton()
("ASCII string to network" function) is to use the POSIX functioninet_pton()
("presentation string to network" function).This source (https://man7.org/linux/man-pages/man3/inet.3.html) says (emphasis added):
inet_pton()
is even better, and available with my original build command in the question, and is described here: https://man7.org/linux/man-pages/man3/inet_pton.3.html. It does not require any gcc feature test macros to include it, and it has more functionality thaninet_aton
and handles both IPv4 address families (AF_INET
) and IPv6 address families (AF_INET6
).See also the gcc glibc one-page user manual. Here is the link to the
int inet_pton (int af, const char *cp, void *buf)
part: https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-inet_005fpton (emphasis added):Full example of
inet_aton()
andinet_pton()
For my full example code, including error checking, search this file for
inet_aton
andinet_pton
(recommended), in this file from my eRCaGuy_hello_world repo here: socket__geeksforgeeks_udp_server_GS_edit_GREAT.c.Example: here is my
inet_pton()
example code: