Clang: Difference between revisions
Jump to navigation
Jump to search
Enable all warnings:
(→Tips) |
|||
Line 45: | Line 45: | ||
=== Enable all warnings: <code>-Weverything</code> === |
=== Enable all warnings: <code>-Weverything</code> === |
||
<code>-Weverything</code> means enable all current and future warnings. See [http://amattn.com/p/better_apps_clang_weverything_or_wall_is_a_lie.html Better Apps with Clang's Weverything or Wall is a Lie!] |
<code>-Weverything</code> means enable all current and future warnings. See [http://amattn.com/p/better_apps_clang_weverything_or_wall_is_a_lie.html Better Apps with Clang's Weverything or Wall is a Lie!] |
||
== Troubleshoot == |
|||
=== iostream not found === |
|||
This is a funny one. Simple Hello, World program that fails with an |
|||
<source lang="c"> |
|||
#include <iostream> |
|||
using namespace std; |
|||
int main() { |
|||
cout << "Hello, World" << endl; |
|||
return 0; |
|||
} |
|||
</source> |
|||
<source lang="bash"> |
|||
clang++ main.cpp |
|||
# main.cpp:1:10: fatal error: 'iostream' file not found |
|||
# #include <iostream> |
|||
# ^~~~~~~~~~ |
|||
# 1 error generated. |
|||
</source> |
|||
Luckily [https://stackoverflow.com/questions/54521402/locating-iostream-in-clang-fatal-error-iostream-file-not-found StackOverflow] comes to the rescue. |
|||
The problem is that we have several gcc/g++ installation, and the one selected by clang doesn't have the c++ headers. |
|||
<source lang="bash"> |
|||
$ clang++ -v main.cpp |
|||
clang version 7.0.1-8 (tags/RELEASE_701/final) |
|||
Target: x86_64-pc-linux-gnu |
|||
Thread model: posix |
|||
InstalledDir: /usr/bin |
|||
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/10 |
|||
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/8 |
|||
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10 |
|||
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8 |
|||
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9 |
|||
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/10 |
|||
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8 |
|||
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10 |
|||
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8 |
|||
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9 |
|||
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9 |
|||
Candidate multilib: .;@m64 |
|||
Selected multilib: .;@m64 |
|||
"/usr/lib/llvm-7/bin/clang" -cc1 [...] |
|||
clang -cc1 version 7.0.1 based upon LLVM 7.0.1 default target x86_64-pc-linux-gnu |
|||
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/x86_64-linux-gnu" |
|||
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/backward" |
|||
ignoring nonexistent directory "/include" |
|||
ignoring duplicate directory "/usr/include/clang/7.0.1/include" |
|||
#include "..." search starts here: |
|||
#include <...> search starts here: |
|||
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++ |
|||
/usr/include/clang/7.0.1/include |
|||
/usr/local/include |
|||
/usr/include/x86_64-linux-gnu |
|||
/usr/include |
|||
End of search list. |
|||
main.cpp:1:10: fatal error: 'iostream' file not found |
|||
#include <iostream> |
|||
^~~~~~~~~~ |
|||
1 error generated. |
|||
</source> |
|||
The simplest for us was to uninstall gcc-9. Another solution is to install g++-9. Also it seems that clang uses some kind of prioritization, so in fact reinstalling gcc-8/g++-8 may work as well. |
Revision as of 11:26, 28 May 2020
Links
Clang on Windows
LLVM-MinGW
- Get packages from https://github.com/mstorsjo/llvm-mingw
The best port of LLVM/Clang on Windows I tried.
- No dependency on Visual Studio.
- Support 32-bit and 64-bit targets.
- Support sanitizers (ASAN, UBSAN).
MSYS2
MSYS2 now provides clang 9.0 packages.
- No dependency on Visual Studio.
- Support 32-bit and 64-bit targets.
- But does not support sanitizers (ASAN, UBSAN).
Official LLVM
Packages from the official LLVM at http://releases.llvm.org/download.html.
- DEPEND on Visual Studio for linking
- Support 32-bit and 64-bit targets.
- (Maybe) support sanitizers (ASAN, UBSAN).
Tips
Get Stack Pointer
Use this macro [1]:
#define current_stack_pointer ({ \
register unsigned long esp asm("esp"); \
asm("" : "=r"(esp)); \
esp; \
})
Turn errors into warning
-Wno-error=foo Turn warning “foo” into an warning even if -Werror is specified.
Enable all warnings: -Weverything
-Weverything
means enable all current and future warnings. See Better Apps with Clang's Weverything or Wall is a Lie!
Troubleshoot
iostream not found
This is a funny one. Simple Hello, World program that fails with an
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World" << endl;
return 0;
}
clang++ main.cpp
# main.cpp:1:10: fatal error: 'iostream' file not found
# #include <iostream>
# ^~~~~~~~~~
# 1 error generated.
Luckily StackOverflow comes to the rescue.
The problem is that we have several gcc/g++ installation, and the one selected by clang doesn't have the c++ headers.
$ clang++ -v main.cpp
clang version 7.0.1-8 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
"/usr/lib/llvm-7/bin/clang" -cc1 [...]
clang -cc1 version 7.0.1 based upon LLVM 7.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/backward"
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/include/clang/7.0.1/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++
/usr/include/clang/7.0.1/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
The simplest for us was to uninstall gcc-9. Another solution is to install g++-9. Also it seems that clang uses some kind of prioritization, so in fact reinstalling gcc-8/g++-8 may work as well.