用gcc编译系统()

Jos*_*nan 7 c gcc compiler-errors

我使用时,我的代码无法正确编译:

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;
}
Run Code Online (Sandbox Code Playgroud)

gsa*_*ras 10

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

提示:当您看到内置函数的隐式声明时,您必须搜索该函数(使用google,例如现在您应该搜索"system()C"),以便找到相应的标题,即声明函数的位置.然后其中一个结果应该是函数的ref.

在我们的例子中这个链接.在那里你可以看到:

概要

#include <stdlib.h>
int system(const char *command);
Run Code Online (Sandbox Code Playgroud)

它告诉您必须包含stdlib要使用的标头system().

正如布莱特先生所注意到的那样,如果您使用类似inux的操作系统,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);
...
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用的是unixy操作系统,那么`man 3 system`也应该可以解决问题. (2认同)
  • 是@JoshBuchanan,不是任何标题,当时需要包含该特定函数的标题.现在`system()`需要包含`stdlib.h`.不错的问题,你得到了我的+1.希望你保留我的小费,这将有助于我相信! (2认同)