Getting Clang to work on windows

49,341

Solution 1

There's some instructions for building clang on this page (hidden in the "Clang Development" part of the sidebar...). For MinGW you want the section called "On Unix-like Systems". The only tricky part is step 5 which tells you how to set up the paths for the C++ standard library. These need to be added into the code in clang/lib/Frontend/InitHeaderSearch.cpp. On my machine it wound up looking like this

// FIXME: temporary hack: hard-coded paths.
AddPath("/usr/local/include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++/mingw32", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include/c++/backward", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include", System, true, false, false);
AddPath("c:/msysgit/mingw/bin/../lib/gcc/mingw32/4.4.0/include-fixed", System, true, false, false);

though I'm not sure all these are needed!

Solution 2

Depending on your version of MinGW (and thus the version of gcc ported), the headers might be scattered a bit...

In the file clang/lib/Frontend/InitHeaderSearch.cpp you will find a number of hard-coded paths. The trouble is that each is version specific, so if your version of MinGW is not in there, then feel free to add it (and ask for it to be integrated in Clang's mainline by posting the patch to cfe-commit).

Personally I run it on MinGW/msys with only minor issues (a number of test cases fail because my msys shell mangles the input when there are : in...), I have not tried using it from CodeBlocks though (I'm used to the command line).

If you wish to help, Takumi is watching over MinGW integration, Francois Pichet is leading the work on compatibility with VC++/MFC headers (ie is the main contributor) and @rubenvb is currently trying to push patches on libc++ to have it working on Windows (libc++ does not compile on Windows yet). The 3 areas are pretty much independent and require different skills and knowledge.

Solution 3

I had a similar problem. I used the GCC 4.7's analogs of the paths specified by Mike Dinsdale's answer and specified them with the '-isystem' flag (Clang 3.2 in the mingw64 distribution as built by rubenvb) to all of my future calls to the clang executable (via scripts). As these directories are being specified explicitly as system inclusion directories, all potentially wearisome warnings generated by them are automatically suppressed.

tl;dr: the -isystem flag specifies system inclusion directories without recompilation in Clang

Solution 4

Clang does have hardcoded search locations, as defined in the file clang/lib/Frontend/InitHeaderSearch.cpp, near the comment FIXME: temporary hack: hard-coded paths.

There's a note about it on this page: http://clang.llvm.org/get_started.html

So get the include paths from your other compiler (MingW), and hardcode them into Clang, and it might work. (I'm not sure if Clang's Windows support is 100% there yet)

Solution 5

Windows 10 / VS 2017 / Clang 4.0.0 missing stdlib.h in C code. This is how I solved it:

  • Open x86_x64 Cross Tools Command Prompt for VS 2015/2016/2017. Running clang in here should eliminate the "missing headers" error.
  • If still not working, you are missing the actual headers and/or libs, since Clang leaves these platform-specifics to VS or MinGW. Go to \Program Files (x86)\Windows Kits\10\Include\ and search its subdirectories (if any) for stdlib.h and co. If not found, you will need to install the latest Windows 10 SDK by going to Visual Studio (installer) and clicking Modify to add packages.
Share:
49,341
Luther
Author by

Luther

Updated on July 09, 2022

Comments

  • Luther
    Luther almost 2 years

    I have followed the following step by step guide and I've managed, after a bit of fiddling, to get clang to compile using code:blocks and MinGW. Great, so now I could add the Clang module to eclipse (why have one IDE when you can have four) and start compiling.

    I can compile a simple program that doesn't use the standard library but unfortunately when I try to compile this:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
        return 0;
    }
    

    first of all I get this:

    ..\src\test.cpp:9:10: fatal error: 'iostream' file not found

    so I add the Mingw headers to the include path; then I get this:

    'fatal error: 'bits/c++config.h' file not found'

    which is weird. Why does MingW work if that file isn't in 'bits/'? Is it built in to the compiler?. Never mind, I find an implementation of it and create the file in 'bits/'.

    Then I get a whole storm of errors including strange ones that seem to suggest either clang doesn't implement the preprocessor correctly or else my understanding of the preprocessor is incorrect.

    C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.4.1\include\c++/cwchar:45:26: error: expected value in expression
    #if _GLIBCXX_HAVE_WCHAR_H
    

    and many more like that. Should that be

    #if defined(_GLIBCXX_HAVE_WCHAR_H) 
    

    or

    #ifdef _GLIBCXX_HAVE_WCHAR_H?
    

    If they are then the MinGW standard libraries are wrong.

    I assume I'm incorrect in assuming that clang can be dropped in to replace gcc and that it is not designed to work with the gnu standard libraries. Any confirmation or denial of this, backed up with evidence would be most welcome!

    So, does anybody have a foolproof way to get clang compiling on a Windows PC? There's a dearth of information online regarding clang and especially regarding windows.

    I'm really keen to get clang and LLVM working as they sound great from what I've read. Any help would be appreciated.

    Thanks.

  • Luther
    Luther almost 13 years
    Wow... hard coded search paths. Not so bad if it's also ok to have non hard coded search paths doing the same thing. Do you know if that's the case?
  • Luther
    Luther almost 13 years
    >>(I'm not sure if Clang's Windows support is 100% there yet) I'm pretty sure it's not, given how many hoops are required to get this far. Is anybody actively working on it? Do they need any help?
  • josesuero
    josesuero almost 13 years
    I'm not actively participating in the development, but I've subscribed to their development mailing list for a while. From that, it does seem like a few people are working on Windows support (and it might interest you to know that Linux support can be just as wonky. In particular, the search path issue affects Ubuntu and Fedora too. GCC effectively hardcodes its search paths too, so in order to be a drop-in replacement for GCC, Clang has to do something similar (albeit in a less polished manner). And do the Windows developers need help? From what I've read, they definitely wouldn't say no :)
  • Luther
    Luther almost 13 years
    At the moment, I doubt I'm much help to anybody but if I -can- get this to compile and run in some sort of foolproof way, I'll try submitting my changes.
  • SK-logic
    SK-logic almost 13 years
    @Luther, yes, of course you can use -I. But keep in mind that you'd need libstdc++ headers from gcc-4.2.0, no newer versions are supported.
  • Jean-François Fabre
    Jean-François Fabre over 5 years
    Yes, it requires to download 500 Petabytes of MS crap but at least this works.