为什么我必须重新声明外部函数?

时间:2019-05-29 02:14:23

标签: c

我目前正在阅读Richard Stevens的《 Unix环境中的高级编程》,并且编写了一个基本程序来测试如何创建文件系统链接。此后没有问题,我决定实现一些选项,首先是可以通过命令行指定-s来创建符号链接而不是硬链接的功能。为此,我键入link_fn,将其设置为link,除非用户指定-s,在这种情况下将其设置为symlink

问题是我必须包含extern int symlink(const char* actualpath, const char* sympath);,否则在运行makefile时出现以下错误:

link/link.c: In function ‘main’:
link/link.c:30:18: error: ‘symlink’ undeclared (first use in this function)
   30 |             fn = symlink;
      |                  ^~~~~~~
link/link.c:30:18: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile;31: link.o] Error 1

奇怪的是,我再次检查了书和手册页(man 2 symlink),他们俩都说symlink函数在unistd.h标头中声明。我的标题部分如下所示:

#if defined(__unix__)
    #include <unistd.h>

    #include <dirent.h>
    #include <fcntl.h>

    #include <sys/mman.h>
    #include <sys/random.h>
    #include <sys/stat.h>
    #include <sys/syscall.h>
    #include <sys/sysctl.h>
    #include <sys/sysinfo.h>
    #include <sys/termios.h>
    #include <sys/types.h>
    #include <sys/user.h>
#elif defined(_WIN32)
    ...
#endif // OS-Dependent modules

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
...

我首先确保已定义__unix__,并且symlinklink函数都可以正常工作,但前提是我将symlink声明为外部变量,像这样:

extern int symlink(const char* actualpath, const char* sympath);

然后我使用gcc选项运行-E,以查看是否确实包含了unistd.h,并且可以肯定的是,是否成功包含了symlink函数,就在这里:

extern int symlink (const char *__from, const char *__to)
     __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))) ;

那么,为什么我自己没有声明symlink函数原型时却出现编译器错误?为什么在相同的头文件中以完全相同的方式声明link函数时却没有给我这个问题?

这里是link函数,也来自我在调试时生成的预处理器输出。

extern int link (const char *__from, const char *__to)
     __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))) ;

#if defined(__unix__)
    #include <unistd.h>

    #include <dirent.h>
    #include <fcntl.h>
#elif defined(_WIN32)
    // Removed for brevity
#endif // OS-Dependent modules

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#if !defined(FALSE) || !defined(TRUE)
enum { FALSE, TRUE };
#endif // TRUE || FALSE

// Why does the compiler require this declaration, and only for symlink?
extern int symlink(const char* actualpath, const char* sympath);

typedef int (*link_fn)(const char* path, const char* link_path);

int main(int argc, char *argv[])
{
    if (argc == 2) {
        if (strcmp(argv[1], "--help") == 0) {
            printf("\nUsage: %s <Existing Filename> <New Filename>\n\n", argv[0]);
            printf("Options: \n");
            printf("  -s    Create symbolic link instead of a hard link\n\n");

            return EXIT_SUCCESS;
        }
    }

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <Existing Filename> <New Filename>\n", argv[0]);

        return EXIT_FAILURE;
    }

    link_fn fn = link;

    for (size_t i = 1; i < (size_t) argc - 2; ++i) {
        if (strcmp(argv[i], "-s") == 0) {
            fn = symlink;
        }
    }

    const char* existing_path = argv[argc - 2];
    const char* new_path      = argv[argc - 1];

    errno = 0;

    const int return_code = fn(existing_path, new_path);

    if (return_code == -1) {
        fprintf(stderr, "[Error] %s\n", strerror(errno));

        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

正在使用以下程序编译程序:

CC=gcc
CFLAGS="-std=c17 -Wall -Wextra -Werror -pedantic"

系统信息

gcc version 9.1.0
Manjaro Linux 18.0.4
x86-64

MRE

以下是埃里克·波斯特皮斯切尔(Eric Postpischil)律师的一个最小例子:

#if defined(__unix__)
#include <unistd.h>
#endif

int (*a)(const char*, const char*) = link;
int (*b)(const char*, const char*) = symlink;

int main(void)
{
    //
}

运行make会产生以下输出:

gcc -I include -E -o link.pp link/link.c
gcc -I include -S -masm=intel -o link.asm link/link.c
gcc -std=c17 -Wall -Wextra -Werror -pedantic  -I include -c -o link.o link/link.c
link/link.c:9:38: error: ‘symlink’ undeclared here (not in a function)
    9 | int (*b)(const char*, const char*) = symlink;
      |                                      ^~~~~~~
make: *** [Makefile;31: link.o] Error 1

预处理后的输出如下。

 1  # 1 "link/link.c"
 2  # 1 "<built-in>"
 3  # 1 "<command-line>"
 4  # 31 "<command-line>"
 5  # 1 "/usr/include/stdc-predef.h" 1 3 4
 6  # 32 "<command-line>" 2
 7  # 1 "link/link.c"
 ...
12  # 1 "/usr/include/unistd.h" 1 3 4
13  # 25 "/usr/include/unistd.h" 3 4
14  # 1 "/usr/include/features.h" 1 3 4
15  # 450 "/usr/include/features.h" 3 4
16  # 1 "/usr/include/sys/cdefs.h" 1 3 4
17  # 452 "/usr/include/sys/cdefs.h" 3 4
18  # 1 "/usr/include/bits/wordsize.h" 1 3 4
19  # 453 "/usr/include/sys/cdefs.h" 2 3 4
20  # 1 "/usr/include/bits/long-double.h" 1 3 4
21  # 454 "/usr/include/sys/cdefs.h" 2 3 4
22  # 451 "/usr/include/features.h" 2 3 4
23  # 474 "/usr/include/features.h" 3 4
24  # 1 "/usr/include/gnu/stubs.h" 1 3 4
25  # 10 "/usr/include/gnu/stubs.h" 3 4
26  # 1 "/usr/include/gnu/stubs-64.h" 1 3 4
27  # 11 "/usr/include/gnu/stubs.h" 2 3 4
28  # 475 "/usr/include/features.h" 2 3 4
29  # 26 "/usr/include/unistd.h" 2 3 4
... 
32  # 202 "/usr/include/unistd.h" 3 4
33  # 1 "/usr/include/bits/posix_opt.h" 1 3 4
34  # 203 "/usr/include/unistd.h" 2 3 4
...
38  # 1 "/usr/include/bits/environments.h" 1 3 4
39  # 22 "/usr/include/bits/environments.h" 3 4
40  # 1 "/usr/include/bits/wordsize.h" 1 3 4
41  # 23 "/usr/include/bits/environments.h" 2 3 4
42  # 207 "/usr/include/unistd.h" 2 3 4
43  # 217 "/usr/include/unistd.h" 3 4
44  # 1 "/usr/include/bits/types.h" 1 3 4
45  # 27 "/usr/include/bits/types.h" 3 4
46  # 1 "/usr/include/bits/wordsize.h" 1 3 4
47  # 28 "/usr/include/bits/types.h" 2 3 4
48  # 1 "/usr/include/bits/timesize.h" 1 3 4
49  # 29 "/usr/include/bits/types.h" 2 3 4
...
53  # 31 "/usr/include/bits/types.h" 3 4
54  typedef unsigned char __u_char;

其他函数声明和枚举声明了大约一千行。然后,在第1169行:

  1169  extern int link (const char *__from, const char *__to)
  1170       __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))) ;
  1171  
  1172  
  1173  
  1174  
  1175  extern int linkat (int __fromfd, const char *__from, int __tofd,
  1176       const char *__to, int __flags)
  1177       __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2, 4))) ;
  1178  
  1179  
  1180  
  1181  
  1182  extern int symlink (const char *__from, const char *__to)
  1183       __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))) ;

并跳过几百行类似的声明:

  1416  
  1417  # 6 "link/link.c" 2
  1418  
  1419  
  1420  
  1421  # 8 "link/link.c"
  1422  int (*a)(const char*, const char*) = link;
  1423  int (*b)(const char*, const char*) = symlink;
  1424  
  1425  int main(void)
  1426  {
  1427  
  1428  }

1 个答案:

答案 0 :(得分:3)

要声明symlink,您需要#define正确的feature-test macro,如symlink manpage所示:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       symlink():
           _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
               || /* Glibc versions <= 2.19: */ _BSD_SOURCE

您应该养成使用功能测试宏的习惯,尽管如果使用默认的-std选项或以{{开头的任何-std选项,Gnu标头通常都允许您省略它们1}}。 gnu“标准”定义了gnu功能测试宏,该宏有效地绕过了所有功能测试。某些人可能认为这很方便,但是却导致烦人的可移植性问题。

尽管将定义放在每个源文件的顶部在技术上比较好,但我发现将_GNU_SOURCE放入我的-D_XOPEN_SOURCE=700更为方便,这也保证了该定义位于任何{ {1}}。

另外,请注意,如果要准确地预处理源代码,则需要确保CFLAGS对于预处理命令和对编译命令是相同的。否则,您可能会错过某些符号没有使用更严格的CFLAGS选项进行定义(或预定义)的事实。