16

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.

Harry Wagstaff
  • 343
  • 2
  • 10
  • Interesting, I didn't even know Clang could target windows :d – Antti Haapala -- Слава Україні Jul 23 '19 at 14:32
  • 2
    A different environment, but I couldn't reproduce the problem in an MSYS2 Mingw-w64 Win64 build environment. – Ian Abbott Jul 23 '19 at 15:46
  • lib\clang\8.0.0\include\float.h:45 is including a ming header file x86_64-w64-mingw32\include\float.h. what is line 45 of lib\clang\8.0.0\include\float.h? – effbiae Jul 24 '19 at 02:00
  • 1
    @IanAbbott This seems to work for a C example, but clang++ doesn't seem to recognize the msys gcc include paths so no C++ headers are found. – Harry Wagstaff Jul 24 '19 at 09:50
  • 1
    @effbiae #include_next – Harry Wagstaff Jul 24 '19 at 09:51
  • Doesn't work neither with `clang.exe` nor `clang++.exe`. I use `mingw-w64` as well. `In file included from C:\LLVM\Prebuilt\lib\clang\8.0.0\include\float.h:45:`, `C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\float.h:28:15: fatal error: 'float.h' file not found`, `#include_next ` – Vlad Faust Jul 30 '19 at 14:46
  • Are you using MSYS, or MSYS2? (Or something else?) – Ian Abbott Aug 02 '19 at 15:45
  • Sorry for delay. If the MSYS question was directed to me, then the answer is I use neither. It's just "MingW-W64-builds" from the official site, downloads section; and then the online installer. – Vlad Faust Aug 05 '19 at 08:37
  • @HarryWagstaff Have you tried to add `-std=c99` parameter for clang call? "float.h" is C99 feature and may not be available for previous standards – Alexander Ushakov Aug 05 '19 at 09:28
  • @AlexanderUshakov neither `c99` nor `gnu17` helps – Vlad Faust Aug 05 '19 at 10:36
  • @HarryWagstaff. I had to try to solve this problem with a project I was working on. At the moment the only two solutions are either to use MSYS2 with the latest MinGW-w64 or build MinGW-w64 from source. It could be quite a while before there is any official release. Check out my answer as I've had to edit it as I've found out more details. – dawlane Aug 08 '19 at 05:22

2 Answers2

5

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.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • Using this release I receive a linker error (cannot find -lgcc_eh). I assume that this is libgcc with SEH support - I've tried recompiling with -fno-exceptions but that also produces the error. – Harry Wagstaff Aug 05 '19 at 08:20
  • Any chance it got merged into the "online" installer? I'm confused that I can't select neither exception handling nor threading model. Also that "ada" thing... – Vlad Faust Aug 05 '19 at 08:34
  • @VladFaust Ada's just another language that GCC supports. However, it requires an Ada compiler to build itself. I personally don't use it because the whole concept is less than ideal. I don't think the online installer has been updated in more than a year. – S.S. Anne Aug 05 '19 at 13:30
  • @HarryWagstaff Usually, that's the default on i686... not x86_64. – S.S. Anne Aug 05 '19 at 13:34
  • @JL2210 I also get the `cannot find -lgcc_eh` error. I'm on x86-64 – Vlad Faust Aug 05 '19 at 16:28
3

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.

dawlane
  • 149
  • 2
  • 1
    This is just as bad of a solution as patching a compiler to do something else for a specific problem... –  Aug 04 '19 at 12:37
  • 1
    It's not the best solution, but unfortunately it's a question of which MinGW-w64 release is being used. The latest MinGW sources show the patch to line 27 in MinGW's float.h. While the current MinGW-w64 binary downloads do not have this patch. – dawlane Aug 04 '19 at 14:13
  • Thanks for your effort, @dawlane! Unfortunately, building from source is not an option I can rely on. I hope this question would make MinGW maintainers to update the mainstream version ASAP. – Vlad Faust Aug 05 '19 at 08:36
  • @VladFaust: With the above -lgcc_seh issue. The reason why you are more than likely seeing this would be down to the default exception of the above binaries. See https://stackoverflow.com/a/17968530/11879567 on how to check. One of the reasons of why I wouldn't rely on third party downloads. – dawlane Aug 06 '19 at 06:48
  • Suggesting to use MSYS2 based MinGW-w64 is a good suggestion if you don't have specific requirements w.r.t. toolchain versions and just want something that works as best it can. – rubenvb Aug 06 '19 at 09:00