Questions tagged [clang]

For questions about the clang LLVM compiler front end. For general questions about C, use the C tag.

Usage

This tag should be used for questions specific to clang, an LLVM compiler front end for C-based languages. It should not be used for general questions about C; for those, use the tag.

About

Clang is the LLVM compiler front end for C/C++/Objective-C, which provides fast compiles, useful error and warning messages, an accommodating license and offers an extensible platform for building source level tools.

Why Clang?

The development of a new front-end was started out of a need -- a need for a compiler that allows better diagnostics, better integration with IDEs, a license that is compatible with commercial products, and a nimble compiler that is easy to develop and maintain. All of these were motivations for starting work on a new front-end that could meet these needs.

Current Status

Clang is still under development. Clang is considered to be a production quality C, Objective-C, C++ and Objective-C++ compiler when targeting X86-32, X86-64, and ARM (other targets may have caveats, but are usually easy to fix).

C++ Standards Support

  • C++11 is fully supported in Clang 3.3 and later
  • C++14 is fully supported in Clang 3.4 and later
  • C++17 proposed features are mostly supported in Clang 3.5 and later

Please see the C++ status page for more information.

Related Tags

10121 questions
151
votes
18 answers

How to call clang-format over a cpp project folder?

Is there a way to call something like clang-format --style=Webkit for an entire cpp project folder, rather than running it separately for each file? I am using clang-format.py and vim to do this, but I assume there is a way to apply this once.
user3639557
  • 4,791
  • 6
  • 30
  • 55
150
votes
11 answers

How do I make an infinite empty loop that won’t be optimized away?

The C11 standard appears to imply that iteration statements with constant controlling expressions should not be optimized out. I'm taking my advice from this answer, which specifically quotes section 6.8.5 from the draft standard: An iteration…
nneonneo
  • 171,345
  • 36
  • 312
  • 383
145
votes
5 answers

Where is PATH_MAX defined in Linux?

Which header file should I invoke with #include to be able to use PATH_MAX as an int for sizing a string? I want to be able to declare: char *current_path[PATH_MAX]; But when I do so my compiler (Clang/LLVM on Linux) issues the following…
haziz
  • 12,994
  • 16
  • 54
  • 75
138
votes
4 answers

Clang optimization levels

For gcc, the manual explains what -O3, -Os, etc. translate to in terms of specific optimisation arguments (-funswitch-loops, -fcompare-elim, etc.) I'm looking for the same info for clang. I've looked online and in man clang which only gives general…
Antoine
  • 13,494
  • 6
  • 40
  • 52
135
votes
22 answers

How to fix PCH error?

When I try to build my app in Xcode, I get this error message: PCH file built from a different branch ((clang-425.0.24)) than the compiler ((clang-425.0.27)) It doesn't happen before, but this was the first build after updating Xcode. Other apps…
Macro206
  • 2,143
  • 3
  • 19
  • 25
134
votes
6 answers

How do I compile C++ with Clang?

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++?
pythonic
  • 20,589
  • 43
  • 136
  • 219
134
votes
14 answers

How to list supported target architectures in clang?

Currently I am interested in ARM in general and specifically iPhone/Android targets. But I just want to know more about clang, since it feels to play important role in the years to come. I tried clang -cc1 --help|grep -i list clang -cc1 --help|grep…
exebook
  • 32,014
  • 33
  • 141
  • 226
133
votes
3 answers

Why is a simple loop optimized when the limit is 959 but not 960?

Consider this simple loop: float f(float x[]) { float p = 1.0; for (int i = 0; i < 959; i++) p += 1; return p; } If you compile with gcc 7 (snapshot) or clang (trunk) with -march=core-avx2 -Ofast you get something very similar…
Simd
  • 19,447
  • 42
  • 136
  • 271
131
votes
2 answers

Why does Clang optimize away x * 1.0 but NOT x + 0.0?

Why does Clang optimize away the loop in this code #include #include static size_t const N = 1 << 27; static double arr[N] = { /* initialize to zero */ }; int main() { clock_t const start = clock(); for (int i = 0; i <…
user541686
  • 205,094
  • 128
  • 528
  • 886
118
votes
3 answers

What predefined macro can I use to detect clang?

I'm trying to detect the compiler used to compile my source code. I can easily find predefined macros to check for MSVC or GCC (see https://github.com/cpredef/predef for example), but I cannot find any macro to check for clang. Does someone know if…
Pierre Bourdon
  • 10,521
  • 4
  • 33
  • 27
116
votes
2 answers

Is it intended by the C++ standards committee that in C++11 unordered_map destroys what it inserts?

I've just lost three days of my life tracking down a very strange bug where unordered_map::insert() destroys the variable you insert. This highly non-obvious behaviour occurs in very recent compilers only: I found that clang 3.2-3.4 and GCC 4.8 are…
Niall Douglas
  • 9,212
  • 2
  • 44
  • 54
113
votes
3 answers

How to remove "noise" from GCC/clang assembly output?

I want to inspect the assembly output of applying boost::variant in my code in order to see which intermediate calls are optimized away. When I compile the following example (with GCC 5.3 using g++ -O3 -std=c++14 -S), it seems as if the compiler…
m.s.
  • 16,063
  • 7
  • 53
  • 88
112
votes
4 answers

Confusing Template error

I've been playing with clang a while, and I stumbled upon "test/SemaTemplate/dependent-template-recover.cpp" (in the clang distribution) which is supposed to provide hints to recover from a template error. The whole thing can be easily stripped down…
user350814
110
votes
11 answers

Clang doesn't see basic headers

I've tried to compile simple hello world on Fedora 20 with Clang, and I get the following output: d.cpp:1:10: fatal error: 'iostream' file not found #include I don't have any idea how to resolve it.
sweet_sugar
  • 1,390
  • 3
  • 13
  • 22
108
votes
2 answers

Faster code-completion with clang

I am investigating potential code-completion speedups while using clang's code-completion mechanism. The flow described below is what I found in rtags, by Anders Bakken. Translation units are parsed by a daemon monitoring files for changes. This is…
Pradhan
  • 16,391
  • 3
  • 44
  • 59