如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:23:19  来源:igfitidea点击:

How do I compile C++ with Clang?

c++clang

提问by pythonic

I have installed Clangby 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++?

回答by adl

The command clangis 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 -xswitch 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 clangand clang++well: Difference between clang and clang++

这个邮件列表线程解释之间的区别clangclang++井:铛铛和区别++

回答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.ccto compile the program. It will produce a default executable file named a.out. Use ./a.outto 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 -Walloption. 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 <...>开始搜索”。