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

Lad*_*dih 2 c++ terminal codeblocks c++11

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

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

这是我输入的命令:

clang++ -std=c++11 main.cpp file1.cpp file1.h 
Run Code Online (Sandbox Code Playgroud)

表明:

clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated
Run Code Online (Sandbox Code Playgroud)

还有一大堆错误:

error: use of undeclared identifier 'std' 
Run Code Online (Sandbox Code Playgroud)

在头文件中.

Bia*_*sta 10

你应该避免编译文件(.h).

试试:

clang++ -std=c++11 main.cpp file1.cpp
Run Code Online (Sandbox Code Playgroud)

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


Gav*_*vin 6

您不应该编译头文件,只能编译源文件.在需要引用头文件的源文件中,放在#include "file1.h"顶部.