C++:如何解决隐式声明编译错误?

C++: How to resolve implicit declaration compile error?

我正在学习 C++。通过这个超级基本示例,我能够执行 PowerShell 命令。

#include <stdio.h>
int main() {
   system("powershell.exe New-Item -ItemType File -Name cpp_test");
   return 0;
}

在 Debian 中编译时,我收到以下(非致命错误?)错误消息:

$ x86_64-w64-mingw32-gcc test.c -o test.exe

test.c: In function ‘main’:
test.c:3:4: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
    system("powershell.exe New-Item -ItemType File -Name cpp_test");
    ^~~~~~

我查看了 错误,但不太明白它如何适用于我的示例。谁能试着向一个五岁的孩子解释一下...

修订, test.cpp:

#include <cstdlib>
int main() {
   system("powershell.exe New-Item -ItemType File -Name cpp_test");
   return 0;
}

C 函数 system 在 C++ header <cstdlib> 中声明(或在 C 中 header <stdlib.h>

因此,如果您的程序是 C++ 程序,则将您的程序中未使用的 header <stdio.h> 替换为

#include <cstdlib>

否则写入

#include <stdlib.h>

支持向后兼容,编译器假定函数在别处声明,无法检查函数调用是否正确。