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

标签 c gcc compiler-errors gnu

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

相关文章:

c++ - gcc gprof/gcov/other - 如何获得函数调用/退出序列+控制流语句

c - 0..9 约束在 GCC 内联汇编中有什么作用?

visual-studio-2010 - Visual Studio要编译目录

C 指针和访问冲突读取位置

c++ - 从 C 和 C++ 中的父进程获取子进程列表(跨平台,无命令行)

C编译器可以在运行时消除这个条件测试吗?

C++11 - 无法使用 constexpr 函数定义 constexpr 文字?

c - "Inappropriate file type or format"

programming-languages - 如果不允许 LP 递归,那么可能会出现堆栈溢出的情况?

android - Android Intent使用情况(Call_action和new_task_launch)