C++程序在代码块中编译运行,但无法在终端中编译

C++ program compiles and runs in codeblocks, but can't compile it in terminal

我创建了一个包含多个源文件和头文件的 C++ 项目。该程序在代码块中编译和运行良好,但我无法在终端中编译它。

所有文件都在同一个文件夹中。

这是我输入的命令:

clang++ -std=c++11 main.cpp file1.cpp file1.h 

显示:

clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated

还有一大堆错误:

error: use of undeclared identifier 'std' 

在头文件中。

您应该避免编译 header 文件 (.h)。

试试:

clang++ -std=c++11 main.cpp file1.cpp

头文件是预处理器将包含在需要它的cpp文件中的东西(那些编译units 使用 #include 预处理器指令)。

你不应该编译头文件,只编译源文件。在需要引用头文件的源文件中,把#include "file1.h"放在最前面。