gpt4 book ai didi

c - get_current_dir_name简单程序无法编译->库函数的隐式声明

转载 作者:行者123 更新时间:2023-12-02 10:50:50 24 4
gpt4 key购买 nike

我有一个非常简单的程序,可以用来更好地学习C:

#include <stdio.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <string.h>
#include <unistd.h>

int main() {
printf("%s\n", get_current_dir_name());
return 0;
}
但是问题是这样编译程序:
gcc -o pushtob2 pushtob2.c -Wall -pedantic -std=gnu11  -static -Wextra -Wconversion
产生错误:
pushtob2.c: In function ‘main’:
pushtob2.c:8:17: warning: implicit declaration of function ‘get_current_dir_name’ [-Wimplicit-function-declaration]
printf("%s\n", get_current_dir_name());
^~~~~~~~~~~~~~~~~~~~
pushtob2.c:8:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s\n", get_current_dir_name());
~^ ~~~~~~~~~~~~~~~~~~~~~~
%d
有人告诉我 get_current_dir_name应该在 https://linux.die.net/man/3/getcwdunistd.h中定义
为什么编译器没有找到 get_current_dir_name而不进行编译?

最佳答案

您需要先定义_GNU_SOURCE(以及一般的功能测试宏),然后再包含任何 header -即,不仅是包含所讨论结构的 header -因此,请更改以下内容:

#include <stdio.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <string.h>
#include <unistd.h>
对此:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

关于c - get_current_dir_name简单程序无法编译->库函数的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64468381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com