I do not know why there is no answer directly addressing the problem. When you want to compile C++ program, it is best to use clang++, instead of using clang. For example, the following works for me:
clang++ -Wall -std=c++11 test.cc -o test
If compiled correctly, it will produce the executable file test, and you can run the file by using ./test.
The whole process is a lot like g++ if you are familiar with g++. See this
post to check which warnings are included with -Wall option. This
Page 显示 Clang 支持的诊断标志列表
A note on using clang -x c++: Kim Gräsman says that you can also use
clang -x c++来编译 CPP 程序,但这可能并不总是可行的。例如,我有一个简单的程序如下:
#include <iostream>
#include <vector>
int main() {
/* std::vector<int> v = {1, 2, 3, 4, 5}; */
std::vector<int> v(10, 5);
int sum = 0;
for (int i = 0; i < v.size(); i++){
sum += v[i]*2;
}
std::cout << "sum is " << sum << std::endl;
return 0;
}
clang++ test.cc -o test will compile successfully, but clang -x c++ will
没有,显示了许多未定义的引用错误。所以我猜它们并不完全等价。在编写 c + + 程序时,最好使用 clang++代替 clang -x c++,以免造成额外的麻烦。