How to compile c ++ with clang

I installed clang with apt-get on ubuntu and I can successfully compile C files. However, I do not know how to compile C ++. What do I need to do to compile C ++?

+64
c ++ clang
Feb 05 '12 at 10:26
source share
4 answers

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

+103
Feb 05 '12 at 10:30
source share

In addition, for descendants, clang (e.g. GCC) accepts the -x switch to set the language of the input files, e.g.

 $ clang -x c++ some_random_file.txt 

This mailing chain explains the difference between clang and clang ++: http://clang-developers.42468.n3.nabble.com/Difference-between-clang-and-clang-td3001279.html

+36
Jan 17 '13 at 6:15
source share

I do not know why there is no direct answer to the problem. When you want to compile a program in C ++. It is best to use clang ++, for example, the following works for me:

 clang++ -Wall -std=c++11 test.cc -o test 

if it compiled correctly, it will create the test executable, you can run the file with ./test .

Or you can just use clang++ test.cc to compile the program, it produces a default executable file called a.out . Use ./a.out to run the file.

The whole process is a lot like g ++, if you are familiar with g ++. See this post to check which warnings are included in the -Wall parameter. This page shows a list of diagnostic flags supported by clang.

+4
Jun 14 '17 at 2:37 on
source share

I had a similar problem when creating clang from source code (but not using sudo apt-get install. This may depend on the version of Ubuntu you are running on).

It might be worth checking if clang ++ can find the correct locations for your C ++ libraries: Compare the results of g ++ -v <filename.cpp> and clang ++ -v <filename.cpp> in the "#include <...> section, the search starts here: "

+3
Feb 05 2018-12-12T00:
source share



All Articles