Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang and <iostream> on windows

Tags:

c++

clang

According to http://clang.llvm.org/get_started.html I did the ALL_BUILD on Windows with Visual Studio 2010 and added the built stuff to my system path with $PATH=...ClangSourcBuildPath...\build\bin\Release

I now can compile the following file on the console with:

$> clang file_c.c

//file_c.c
#include <stdio.h>

int main() 
{
  printf("hello world\n");
  return 0;
}

But if I do the same for the following file ($> clang file_cpp.cpp):

//file_cpp.cpp
#include <iostream>

int main()
{
    std::cout << "Hello World!";
    return 0;
}

I get the following errors:

In file included from file_cpp.cpp:1:

In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\iostream:9:   
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\istream:9:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\ostream:9:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\ios:9:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\streambuf:9:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xlocnum:13:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xiosbase:9:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xlocale:11:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\stdexcept:10:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring:9:  
In file included from C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xmemory:15:  

C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility:81:10:   
error: missing 'typename' prior to dependent type name '_It::iterator_category' typedef _It::iterator_category iterator_category;

C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility:82:10:  
error: missing 'typename' prior to dependent type name '_It::value_type' typedef _It::value_type value_type;

C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility:83:10:  
error: missing 'typename' prior to dependent type name '_It::distance_type' typedef _It::distance_type distance_type;

C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility:224:32:  
error: template parameter redefines default argument  
 template < class _E, class _Tr = char_traits< _E > >  

...

I also tried:

$> clang++ file_cpp.cpp
$> clang -x c++ file_cpp.cpp

But I still get the same errors.

Can someone explain me what's wrong?

like image 803
grimblegrumble Avatar asked Oct 22 '22 09:10

grimblegrumble


1 Answers

There are certain flags (I think it was -fms-extensions, not quite sure of the exact option) that are required to get clang to parse the VC++ headers.

The next problem you'll encounter is that the resulting executable cannot be linked. This is due to the fact that clang uses a different name mangling than MSVC, and will result in undefined references.

If you want to use Clang on Windows (which is currently possible with MinGW-w64), you can use my prebuilt packages, you'll need

  1. Clang 3.2

  2. GCC 4.6

Extracted to the same directory. GCC is in this case only used to call the linker. Clang can be used to compile everything.

There is currently no way to use clang for C++ with the Visual Studio headers+libs. C should work, but I haven't tested and there may be other ABI problems preventing this from working.

like image 104
rubenvb Avatar answered Oct 30 '22 05:10

rubenvb