如何使用 Clang 编译 C++?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9148488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I compile C++ with Clang?
提问by pythonic
回答by adl
The command clang
is for C, and the command clang++
is for C++.
命令clang
用于 C,命令clang++
用于 C++。
回答by Kim Gr?sman
Also, for posterity -- Clang (like GCC) accepts the -x
switch to set the language of the input files, for example,
此外,对于后代 - Clang(如 GCC)接受-x
开关来设置输入文件的语言,例如,
$ clang -x c++ some_random_file.txt
This mailing list thread explains the difference between clang
and clang++
well: Difference between clang and clang++
这个邮件列表线程解释之间的区别clang
和clang++
井:铛铛和区别++
回答by jdhao
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++
. For example, the following works for me:
我不知道为什么没有直接解决问题的答案。当你想编译 C++ 程序时,最好使用clang++
. 例如,以下对我有用:
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
.
如果编译正确,它将生成可执行文件test
,您可以使用./test
.
Or you can just use clang++ test.cc
to compile the program. It will produce a default executable file named a.out
. Use ./a.out
to run the file.
或者你可以只用它clang++ test.cc
来编译程序。它将生成一个名为a.out
. 使用./a.out
运行该文件。
The whole process is a lot like g++ if you are familiar with g++. See this postto check which warnings are included with -Wall
option. This pageshows a list of diagnostic flags supported by Clang.
如果你熟悉 g++,整个过程很像 g++。请参阅此帖子以检查-Wall
选项中包含哪些警告。此页面显示了 Clang 支持的诊断标志列表。
回答by Guy Adini
I've had a similar problem when building Clang from source (but not with sudo apt-get install
. This might depend on the version of Ubuntu which you're running).
我在从源代码构建 Clang 时遇到了类似的问题(但不是使用sudo apt-get install
. 这可能取决于您正在运行的 Ubuntu 版本)。
It might be worth checking if clang++
can find the correct locations of your C++ libraries:
可能值得检查是否clang++
可以找到 C++ 库的正确位置:
Compare the results of g++ -v <filename.cpp>
and clang++ -v <filename.cpp>
, under "#include < ... > search starts here:".
比较的结果g++ -v <filename.cpp>
和clang++ -v <filename.cpp>
,在“这里的#include <...>开始搜索”。