Search code examples
windowsc++11clang++mingw-w64

Clang on WIndows finds VC headers instead of GCC


When I had Clang 3.7 installed it would find the STL headers from my GCC installation as long as only both those two directories in the path.

Now that I have installed Clang 3.8 the compiler keeps finding the Visual Studio headers despite the fact that it isn't even in the path

My path is as follows:

PATH=whatever;G:\Compilers\LLVM\bin;g:\compilers\Mingw64-64bit\bin

Edit 1

I've know idea what the correct include paths, but I tried a compilation in Codelite which found these:

"-IG:\\Compilers\\Mingw64-64bit\\x86_64-w64-mingw32\\include\\c++"   
"-IG:\\Compilers\\Mingw64-64bit\\x86_64-w64-mingw32\\include\\c++\\x86_64-w64-mingw32"
"-IG:\\Compilers\\Mingw64-64bit\\lib\\gcc\\x86_64-w64-mingw32\\5.1.0\\include"
"-IG:\\Compilers\\Mingw64-64bit\\lib\\gcc\\x86_64-w64-mingw32\\5.1.0\\include-fixed" "-IG:\\Compilers\\Mingw64-64bit\\x86_64-w64-mingw32\\include" 
"-IG:\\Compilers\\Mingw64-64bit\\x86_64-w64-mingw32\\include\\c++\\backward" 

I'm also using the dialect flag --std=c++11

However a very simple program using the C++ library is giving me errors such as

 G:\Compilers\Mingw64-64bit\x86_64-w64-mingw32\include\c++\bits/stringfwd.h:63:33: error: use of undeclared identifier 'char16_t'
   template<> struct char_traits<char16_t>;
                            ^

Edit 2

Now passing x86_64-w64-mingw32 as recommended by Martin, the autodiscovery process seems to work, however the produced executable just freezes.

I found the same when I tried to use the VS2015 Clang toolset

The code example I am using is this

#include <iostream>

int main()
{
   int arr[] = {1, 2, 3, 4, 5};
   for(auto el : arr)
   {
      std::cout << el << std::endl;
   }
   return 0;
}

I am now compiling with:

clang++ -v --target=x86_64-w64-mingw32 hello.cpp -o test.exe -std=c++14

I have looked at the executable produced with Dependency Walker and it is showing as a 64 bit compile and linking to "g:\Compilers\Mingw64-32Bit\bin\libstdc++-6.dll"

I think the next step would be to try compiling a 32 bit version, but I cannot seem to find the correct target name for that

I have just tried clang++ hello.cpp -o main.exe -std=c++14 --target=i686-pc-mingw32 -v

That seems to be producing a 32 bit executable, but again it just freezes

Note I updated the path to point to the 32 bit clang.

Edit 3

What I find confusing is that until I passed x86_64-w64-mingw32 as the target I got very similar errors both from passing the GCC paths, and from the default target of MSVC where both seemed to be related to char16_t and similar types

Seeing this I thought I would try if passing a target might fix the VC compiler errors

Whilst that sort of seems to work it just creates more problems, because it keeps asking for libs to link to and I don't know the correct ones

So I tried:

clang++ hello.cpp -o main-vc.exe -std=c++14 --target=x86_64-pc-windows-msvc19.0.0 -v
 -Xlinker "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64\libcmt.lib" 
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64\libcpmt.lib" 
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64\libvcruntime.lib" 
"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10586.0\ucrt\x64\libucrt.lib"

I have no idea what were the correct directories to pass.

I have a feeling that I need to pass another lib file as I am getting this error

libcmt.lib(thread_safe_statics.obj) : error LNK2019: unresolved external symbol __imp_CloseHandle


Solution

  • Did you change the target? VC is the new default. Use this (example) for x64, mingw:

    clang++ --target=x86_64-w64-mingw32 test.cpp

    Don't forget the linker as wel.