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++?
6 Answers
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
.
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.
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.
A note on using clang -x c++
: Kim Gräsman says that you can also use
clang -x c++
to compile CPP programs, but that may not be always viable. For example, I am having a simple program below:
#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
not, showing a lot of undefined reference errors. So I guess they are not exactly equivalent. It is best to use clang++
instead of clang -x c++
when compiling c++ programs to avoid extra troubles.
- clang version: 11.0.0
- Platform: Ubuntu 16.04

- 24,001
- 18
- 134
- 273
-
12Thanks for actually answering the question. – Curyous Sep 08 '18 at 08:03
-
@jdhao Thanks for the detailed answer. But there is one thing I do not understand. You said "When you want to compile C++ program, it is best to use clang++". Why using `g++` is not recommended? – Mr.Robot Jan 28 '21 at 16:48
-
3@Mr.Robot I mean it is best to use `clang++` for compiling cpp, compared to using `clang`, not compared to using `g++`. – jdhao Jan 29 '21 at 01:53
-
What about on Windows? – Andrew Feb 14 '21 at 03:29
-
@Andrew I do not use clang on Windows. So I am not sure. If you use clang on Windows, it is easy to verify. – jdhao Feb 15 '21 at 03:44
Also, for posterity -- Clang (like GCC) accepts the -x
switch to set the language of the input files, for example,
$ clang -x c++ some_random_file.txt
This mailing list thread explains the difference between clang
and clang++
well: Difference between clang and clang++

- 30,738
- 21
- 105
- 131

- 7,438
- 1
- 28
- 41
-
5I had an linker error with _clang -x c++_ when compiled cpp file with `#include
`. _-lstdc++_ flag solved this problem. – Slav Oct 02 '15 at 16:17 -
1I have the source code of Clang in front of my eyes now. During build (on Windows), it first builds `clang.exe`, and then copies that executable into `clang++.exe`. So it's the same executable, just at runtime it checks its own name to distinguish whether to behave as C or C++ compiler. HTH. – Serge Rogatch Jan 06 '17 at 12:05
-
As a note, the option `-x c++` was very useful to give as an `-extra-arg` to *clang-tidy*, to force it to treat a .h file as containing C++ instead of C. – Ad N Nov 17 '17 at 15:58
-
1This is too limiting to be actually useful unless you build entire thing from source. It's better to just use `clang++` and let it detect what kind of files you supply to it. When a project can contain `.cpp` files, `.ll` files (llvm ir) and `.o`, `.a`, `.so` and what not files from third party libraries, `clang -x c++` will just throw up. – Dec 07 '19 at 11:53
-
I am also seeing the same issue as @Slav. For a very simple program, `clang++` works, but `clang -x c++` shows a lot of undefined reference errors (the other flags are the same). So I guess it is best to use `clang++`. Tested clang version: 11.0.0. – jdhao Nov 11 '20 at 02:05
Solution 1:
clang++ your.cpp
Solution 2:
clang your.cpp -lstdc++
Solution 3:
clang -x c++ your.cpp

- 30,738
- 21
- 105
- 131

- 182
- 1
- 5
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).
It might be worth checking if clang++
can find the correct locations of your C++ libraries:
Compare the results of g++ -v <filename.cpp>
and clang++ -v <filename.cpp>
, under "#include < ... > search starts here:".

- 30,738
- 21
- 105
- 131

- 5,188
- 5
- 32
- 34
Open a Terminal window and navigate to your project directory. Run these sets of commands, depending on which compiler you have installed:
To compile multiple C++ files using clang++:
$ clang++ *.cpp
$ ./a.out
To compile multiple C++ files using g++:
$ g++ -c *.cpp
$ g++ -o temp.exe *.o
$ ./temp.exe

- 274
- 2
- 5