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++?
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:
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 true. 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 undefined references 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
-
6
-
@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 at 16:48 -
1@Mr.Robot I mean it is best to use
clang++
for compiling cpp, compared to usingclang
, not compared to usingg++
. – jdhao Jan 29 at 1:53 -
-
@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 at 3: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++
-
5I had an linker error with clang -x c++ when compiled cpp file with
#include <iostream>
. -lstdc++ flag solved this problem. – Slav Oct 2 '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 intoclang++.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 6 '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 -
This 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. – Sahsahae Dec 7 '19 at 11:53 -
I am also seeing the same issue as @Slav. For a very simple program,
clang++
works, butclang -x c++
shows a lot of undefined reference errors (the other flags are the same). So I guess it is best to useclang++
. Tested clang version: 11.0.0. – jdhao Nov 11 '20 at 2:05
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:".
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
Solution 1:
clang++ your.cpp
Solution 2:
clang your.cpp -lstdc++
Solution 3:
clang -x c++ your.cpp