Clang ast-dump results in infinite loop - c++

For some reason when I try to get the ast-dump of any C/C++ code with Clang, I get an infinite loop which eventually results in clang: error: linker command failed with exit code 1136 or some similar error code.
I'm running Windows 10 with Clang version 13.0 (Also tried Clang 12.0 with same issues). I run the command clang -Xclang -ast-dump filename.c.
I've tried multiple files, but at this point I'm just trying to get a hello world ast (which compiles and runs fine via clang).
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
I'm sure I'm missing something simple, but I'd appreciate any help, thanks!

It's not an infinite loop if it terminates. :-)
The dumped AST includes stdio.h, which you included, so it's quite big (about 800 lines, when I tried it).
The error message is because the clang compiler (cc1) does not produce an object file when you pass it the -ast-dump option, but the clang driver (clang) doesn't know that. The driver is expecting to be able to link the generated object file into an executable, but it doesn't find the object file so it complains.
Use
clang -Xclang -ast-dump -fsyntax-only filename.c
to terminate the driver after the AST has been dumped.

Related

(Clang Error) compilation error: ld: library not found for -lcrt0.o. Any ideas?

The full terminal output is as follows:
>g++ -std=c++98 -static mainP1.o -o mainP1
>
>ld: library not found for -lcrt0.o
>
>clang: error: linker command failed with exit code 1 (use -v to see invocation)
>
>make: *** [mainP1] Error 1
I'm on a 2020 MacBook Pro with an intel CPU using Visual Studio Code. When I write basic OOP programs in C++ it compiles fine without any clang errors. However, when working with big OOP programs with multiple classes inheriting from a base class I would get this error.
I tried searching online for solutions, but no solution or explanation was found. I double-checked my makefile to ensure I was not linking classes incorrectly.
I thought maybe I should just dual-boot with UBUNTU Linux to avoid this weird XCODE issue I was encountering with clang, but that was also a fruitless endeavor.
The problem was my compiler path in Visual Studio Code.
I changed it to clang++, and now all my code compiles and executes without any problems.
How I changed it:
CMD + SHIFT + P
Typed in: C/C++: Edit Configurations (UI)
Made sure that "Mac" was selected under configuration name.
Changed Compiler Path to: /usr/bin/clang++

intel C++ cannot open "wchar.h" on mac osx

System OSX 10.14.6, macbook pro
Trying to compile this simple C++ program:
#include <iostream>
int main() {
sdt::cout << "hello world" << std::endl;
return 0;
}
Have gcc 9 installed via homebrew, as well as XCode command line tools.
Both of the following work
c++ main.cpp
g++-9 main.cpp
Trying out the latest Intel Parallel Studio, the following fails:
> icpc main.cpp
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd(90),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios(215),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream(38),
from main.cpp(1):
/Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h(119): catastrophic error: cannot open source file "wchar.h"
#include_next <wchar.h>
^
compilation aborted for main.cpp (code 4)
Can someone tell me what I am missing here?
EDIT RESPONSE TO COMMENT ABOUT find
searching from / for wchar.h yields many wchar.h files, including the one from the error:
./usr/local/Cellar/gcc/9.2.0_3/include/c++/9.2.0/tr1/wchar.h
./Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h
./Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/DriverKit19.0.sdk/System/DriverKit/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/wchar.h
./Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/wchar.h
./opt/intel/intelpython3/include/c++/v1/support/solaris/wchar.h
./opt/intel/intelpython3/include/c++/v1/wchar.h
./opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/support/solaris/wchar.h
./opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/DriverKit19.0.sdk/System/DriverKit/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/wchar.h
./dev/fd/3/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/wchar.h
./dev/fd/3/opt/intel/intelpython3/include/c++/v1/support/solaris/wchar.h
./dev/fd/3/opt/intel/intelpython3/include/c++/v1/wchar.h
./dev/fd/3/opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/support/solaris/wchar.h
./dev/fd/3/opt/intel/intelpython3/pkgs/libcxx-4.0.1-hcfea43d_1/include/c++/v1/wchar.h
As per the comments, this solved the problem:
icpc main.cpp -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk
Intel C++ compiler on macOS is highly sensitive to the version of Xcode being used. For example, I receive the same errors as above when using icpc 2020 Initial Release and Xcode 11.3.1, but the code compiles and executes just fine with "icpc main.cpp" when using Xcode 11.2.1.
This has been the situation with Intel C++ compilers for some time on macOS... check your Xcode version and try again.

Clang fails to find iostream. What should I do?

Earlier, I posed a related question.
I have the following program extracted from a large project in my Mac OS
#include <iostream>
int main(){
std::cout<<"hello"<<std::endl;
return 0;
}
Compiling it with Clang fails with the following error:
$ clang test.cpp
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
For information,
A) I have already installed xcode command line tools, using xcodeselect --install. But it seems iostream does not locate in the default search path of clang.
B) Using g++ instead of clang compiles the program. But in my problem, I am not allowed to use other compiler than clang, or to change the source program.
C) I can see workaround techniques, e.g, by tweaking the search path in .bashrc or with some symbolic link, etc. But I feel reluctant to use them, because it seems that I have an installation problem with my Clang and tweaking the path only helps to avoid one of these path issues.
clang and clang++ do different things. If you want to compile C++ code, you need to use clang++
Alternatively you can invoke c++ compiler directly by providing language name explicitely:
clang -x=c++

G++ Link-time optimisation on Mac - compiler/linker bug?

I have a project that uses OpenMP (which doesn't seem to be available in the current OS X default clang setup) but would also like to use LTO. I have sections of the code in SIMD intrinsics using SSE4 and found that G++ was unable to link this code without using the OS X provided clang linker (flag -Wa,-q). G++ 5.10 is installed via Homebrew and compiled with without multilib.
I can compile with LTO using clang but then I lose the parallel 'for's, when I add -flto to G++ I get:
lto1: internal compiler error: in add_symbol_to_partition_1, at lto/lto-partition.c:211
lto1: internal compiler error: Abort trap: 6
g++-5: internal compiler error: Abort trap: 6 (program lto1)
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://github.com/Homebrew/homebrew/issues> for instructions.
lto-wrapper: fatal error: g++-5 returned 4 exit status
compilation terminated.
collect2: fatal error: lto-wrapper returned 1 exit status
compilation terminated.
This also occurs without -Wa,-q if I remove the intrinsics so that it can compile the remaining code.
I have tried a simple two file program that compiles fine with LTO in G++ so I'm not sure what the problem is. Whilst LTO doesn't seem to make a huge amount of difference to this code, in clang I've measured about 7% which is non-negligible as the program takes a while to run.
I know the error says to submit a bug report but I don't want to unless I know it's not just my own stupidity.
Does anyone have experience of this sort of problem, or have I actually run into a compiler bug? (haven't come across one before to my knowledge)
Cheers!
Compilers are never supposed to report ICEs. It's a bug. It may already be a reported bug, but it is most certainly a bug.

C++ 11 Threads, Error Pure virtual function called

here is a very minimal C++11 Thread API code that I am trying to compile
#include<iostream>
#include<thread>
using namespace std;
void threadFunction(void)
{
cout<<"hello from thread:";//<<this_thread::get_id()<<endl;
}
int main()
{
std::thread t(threadFunction);
t.join();
return 0;
}
On Compiling this as
g++ thread1.cpp -pthread -std=c++11
I get the following error
pure virtual method called
terminate called without an active exception
Aborted
What wrong, can someone please help
Note that I am compiling this on Beaglebone Black with ARM A8 processor
This is a bug in either libstdc++ or Clang, depending on who you ask. It should work if you are using a version of Clang released after October 2013. What do you see when you run g++ --version?
As a workaround, you could try using this command line instead. I don't guarantee that it would work; please post a comment with your results.
g++ -pthread -std=c++11 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_{1,2,4} thread1.cpp
Here's the bug report:
http://llvm.org/bugs/show_bug.cgi?id=12730
And here's the official fix to the Clang driver:
https://llvm.org/viewvc/llvm-project?view=revision&revision=191707
I don't know if this was also previously a bug in the GCC driver, and/or whether it's been fixed.
Ran into the same problem on a Raspberry Pi 2 Model B with an ARM Cortex-A7.
Compiled with g++-4.7, turned out the culprit was a compiler flag:
-march=armv7-a
Although clang had a related issue, this is entirely a gcc bug now recorded at: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62100