"program_invocation_name "没有在MacOS下编译。

问题描述 投票:0回答:2

试图用 程序_调用_名称

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

int main(int argc, char *argv[])
{
  extern char *program_invocation_name;
  printf("%s \n", program_invocation_name);

  exit(EXIT_SUCCESS);
}

使用命令 gcc filename.c -o filename以上代码在Linux下可以工作,但在MacOS下无法编译。

gcc filename.c -o filename
Undefined symbols for architecture x86_64:
  "_program_invocation_name", referenced from:
  _main in filename-4acad8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
c linux gcc
2个回答
0
投票

program_invocation_name 并不是任何标准的一部分,它是由 glibc 和其他一些系统提供的扩展,MacOS 并不是其中之一。像你在这里做的那样使用它是无偿的非可移植性和无意义的。只要使用 argv[0].

唯一的一点是 program_invocation_name 是针对那些不属于 main 并没有任何合同 main 拟通过 argv[0] 的(初始)值,或以其他方式使其可以获得。argv[0].


0
投票

Glibc功能替代物 在...之下 12.14 Glibc对errno.h的扩展。:

12.14.1 program_invocation_name

Gnulib没有解决的可移植性问题。

在某些平台上没有这个变量。Mac OS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF1 5.1, Solaris 11.4, Cygwin 1.7.7, mingw, MSVC 14, Interix 3.5, Android 9.0。

© www.soinside.com 2019 - 2024. All rights reserved.