如何用Clang编译c++ ?

时间:2021-06-21 03:17:18

I have installed Clang by using apt-get in Ubuntu, and I can successfully compile C files using it. However, I have no idea how to compile C++ through it. What do I need to do to compile C++?

我已经在Ubuntu中使用apt-get安装了Clang,我可以使用它成功编译C文件。但是,我不知道如何通过它编译c++。我需要做什么来编译c++ ?

4 个解决方案

#1


112  

The command clang is for C, and the command clang++ is for C++.

命令clang是C的,命令clang++是c++的。

#2


41  

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++ well之间的区别:clang和clang++之间的区别

#3


12  

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运行该文件。

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的默认可执行文件。使用。/。出去运行文件。

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 shows a list of diagnostic flags supported by Clang.

如果你熟悉g++,整个过程很像g++。请参阅本文以检查在-Wall选项中包含哪些警告。这个页面显示了Clang支持的诊断标志的列表。

#4


3  

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安装)。这可能取决于你正在运行的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 <文件名的结果。cpp> 和clang+ -v <文件名。cpp> ,在"#include <…">搜索从这里开始:”。

#1


112  

The command clang is for C, and the command clang++ is for C++.

命令clang是C的,命令clang++是c++的。

#2


41  

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++ well之间的区别:clang和clang++之间的区别

#3


12  

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运行该文件。

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的默认可执行文件。使用。/。出去运行文件。

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 shows a list of diagnostic flags supported by Clang.

如果你熟悉g++,整个过程很像g++。请参阅本文以检查在-Wall选项中包含哪些警告。这个页面显示了Clang支持的诊断标志的列表。

#4


3  

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安装)。这可能取决于你正在运行的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 <文件名的结果。cpp> 和clang+ -v <文件名。cpp> ,在"#include <…">搜索从这里开始:”。