Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile code using Clang with the MinGW C/C++ Library? (Particular issue with float.h)

Tags:

c

gcc

mingw

llvm

clang

I have a simple program which I can successfully compile with clang, using MinGW's C/C++ Library:

#include <stdio.h>
int main(int argc, char **argv) { printf("Hello world!\n"); return 0; }

I am able to compile this with mingw-gcc successfully:

 $ gcc test.c -o test
 $ ./test
 Hello world!

I am also able to compile it successfully using clang+mingw:

 $ clang test.c -o test -target
 $ ./test
 Hello world!

However, if I make a small change to my program (include float.h), it continues to compile with gcc but no longer compiles with clang:

#include <stdio.h>
#include <float.h>
int main(int argc, char **argv) { printf("Hello world!\n"); return 0; }
 $ gcc test.c -o test
 $ ./test
 Hello world!

 $ clang test.c -o test -target x86_64-pc-windows-gnu
 In file included from test.c:2:
 In file included from C:\llvm\built\lib\clang\8.0.0\include\float.h:45:
 C:\mingw64-8.1.0\x86_64-w64-mingw32\include\float.h:28:15: fatal error: 'float.h' file not found
 #include_next <float.h>
               ^~~~~~~~~
 1 error generated.

Is there some configuration issue with clang or some missing command line argument? Googling around a bit, it appears that the order of paths when including float.h is important, but this is all supposed to be handled internally by the clang driver.

like image 825
Harry Wagstaff Avatar asked Jul 23 '19 14:07

Harry Wagstaff


People also ask

How do you compile with Clang in terminal?

2.4. To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.

Can Clang ++ compile C?

Clang compiles only C-like languages, such as C, C++, Objective-C, and Objective-C++.


2 Answers

The older binary releases of MinGW-w64 have an incompatibility with Clang 8.0+'s float.h. To fix this, copy this specific revision of float.h into the correct location and use it.

like image 189
S.S. Anne Avatar answered Oct 16 '22 13:10

S.S. Anne


I think that it would be advisable to pass this issue on to one of the clang developers.

Comparing the previous release 7.1.0's float.h to the one in 8.0.0 shows only a few differences. The first one I myself would be asking about, is why they change to the header guard from __FLOAT_H to __CLANG_FLOAT_H.

Have a play around changing the 8.0.0 header guard and see what happens.

Edit: Did a bit more searching. The MinGW-w64 developers know of this change since August 2018. Add or adapt the patch from https://sourceforge.net/p/mingw-w64/mailman/message/36386405/ to your MinGW install may sort it out.

Edit 2: Something that I have not used for a while is my MSYS2 install of MinGW. It shows g++.exe (Rev1, Built by MSYS2 project) 8.2.1 20181214. This has the applied patch to line 27 of float.h.

#if !defined(_FLOAT_H___) && !defined(__FLOAT_H) && !defined(__CLANG_FLOAT_H)

While the source forge download of MinGW-w64 8.1.0 has it shown as #if !defined(_FLOAT_H___) && !defined(__FLOAT_H)

Note: I'm also sure that MSYS2 uses a rolling release update, but I would have to check on that. It's not something that I use on a regular basis.

Edit3: MSYS2 looks like it's a rolling release. Latest versionis 9.1.0.

My opinion unless you need a stand alone MinGW, then go with MSYS2 with the latest updates. Just trying to patch one of the old versions may work, but there could be other issues that my show themselves. If you do need a stand alone version, then I think that the only option would be to build MinGW-w64 directly from source.

NOTE: I would have added comments to the above discussion, but being new I'm not allowed yet.

EDIT 4: NOTE: The third party multilib toolchains are more than likely built with sjlj exceptions as default. See https://stackoverflow.com/a/17968530/11879567 on how to check.

Edit 5: Hopefully this is the last edit I will be making. Checking out the MinGW-w64 forums to see if anyone had asked when the next official release was due. I came across someone who did ask about when 8.2 was to be released. I got the impression that you could be in for a very long wait for any new MinGW-w64 release. https://sourceforge.net/p/mingw-w64/discussion/723797/thread/ea9a5b00fb/

SIDE NOTE: As I have found out when dealing with Clang, you are always going to have one issue or another with it, either with MinGW or Visual Studio.

like image 3
dawlane Avatar answered Oct 16 '22 14:10

dawlane