我有一个非常简单的程序,可以用来更好地学习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/getcwd的unistd.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/