使用 system() 使用 gcc 进行编译

compiling with gcc with system()

我的代码在使用时编译不正确:

system("gcc -o filename temp.c");

我得到:

implicit declaration of function system

我不确定缺少什么,因为它只会在 gcc 调用时引发系统错误。

这是我的代码:

#include <stdio.h>

int main() {
        ...
        system("gcc -o filename temp.c");
        return 0;
}

main() 的顶部添加 #include <stdlib.h>

提示:当您看到 built-in 函数的隐式声明时,您必须搜索该函数(使用 google,例如现在您应该用 "system() C") 搜索,以便找到相应的 header,即声明函数的位置。那么结果之一应该是函数的ref。

在我们的案例中 this link。在那里你可以看到:

SYNOPSIS

#include <stdlib.h>
int system(const char *command);

告诉你必须包含 stdlib header 才能使用 system().

正如 Bright 先生所注意到的,如果您在 inux-like OS 上,man 3 system 也应该可以做到这一点。

示例:

samaras@samaras-A15:~$ man 3 system
SYSTEM(3)                  Linux Programmer's Manual                 SYSTEM(3)

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>  <-- this is what we are looking for!

       int system(const char *command);
...

既然您使用的是 Posix 系统,您应该了解 man 命令,它显示了大多数库调用的文档。在我的系统上,当我键入:

$ man system

我得到:

SYSTEM(3)                  Linux Programmer's Manual                 

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

请注意,在概要中它会告诉您需要使用的包含文件。手册页还包括许多其他文档,例如 return 值。

您没有收到错误,也没有阻止代码的编译,只是警告您没有添加库系统函数,但 gcc 会自动添加。