clang and <iostream> on windows - c++

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?

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
Clang 3.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.

Related

CMake uses C-style precompiled headers instead of C++ version of the precompiled header

My project directory is as follows:
<PROJECT-ROOT>:
- build
- MyProject:
- src:
- main.cpp
- hello.h (basic file that contains a simple function that prints something using iostream)
- pch.h (precompiled header)
- CMakeLists.txt
And CMakeLists.txt file is as follows:
cmake_minimum_required(VERSION 3.16)
project(MyProject)
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -std=c++11")
endif()
include_directories(
MyProject
MyProject/src
)
add_executable(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/MyProject/pch.h
${CMAKE_SOURCE_DIR}/MyProject/src/main.cpp
${CMAKE_SOURCE_DIR}/MyProject/src/hello.h)
target_precompile_headers(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/MyProject/pch.h)
As expected, CMake generates the following precompiled header files in build/CMakeFiles/MyProject.dir/ :
cmake_pch.c
cmake_pch.cxx
cmake_pch.cxx.obj
cmake_pch_cxx.pch
cmake_pch.h
cmake_pch.hxx
I am using this CMake project in Visual Studio 2019, and when I try to run main.cpp, Visual Studio gives me a bunch of errors, such as:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(23): error C2061: syntax error: identifier 'noexcept'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(23): error C2059: syntax error: ';'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(23): error C2449: found '{' at file scope (missing function header?)
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(25): error C2059: syntax error: '}'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(31): error C2061: syntax error: identifier 'noexcept'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(31): error C2059: syntax error: ';'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(31): error C2449: found '{' at file scope (missing function header?)
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstdlib(33): error C2059: syntax error: '}'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xtr1common(19): error C2061: syntax error: identifier 'std'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xtr1common(19): error C2059: syntax error: ';'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xtr1common(19): error C2449: found '{' at file scope (missing function header?)
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xtr1common(235): error C2059: syntax error: '}'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\iosfwd(175): warning C4157: pragma was ignored by C compiler
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\iosfwd(180): warning C4157: pragma was ignored by C compiler
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(307): error C4233: nonstandard extension used: '__is_union' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(310): error C4233: nonstandard extension used: '__is_union' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(313): error C4233: nonstandard extension used: '__is_class' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(316): error C4233: nonstandard extension used: '__is_class' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(325): error C4233: nonstandard extension used: '__is_convertible_to' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(330): error C4233: nonstandard extension used: '__is_convertible_to' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(359): error C4233: nonstandard extension used: '__is_enum' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(362): error C4233: nonstandard extension used: '__is_enum' keyword only supported in C++, not C
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\type_traits(499): error C4233: nonstandard extension used: '__is_pod' keyword only supported in C++, not C
When I searched error code C4233 online, I learned that it is because a C code is trying to be compiled as a C++ code. Also, build process from Visual Studio (I guess uses ninja) shows that:
[1/33] Building C object CMakeFiles\MyProject.dir\cmake_pch.c.obj
Hence, I guess it tries to build my precompiled header as a c source, instead it is a C++ source. I also tried to change the pch extension to .hpp but it didn't work either. How can I specify CMake to mark precompiled header as C++ (or I guess CXX) source instead of pure C?
EDIT: When I disable the target_precompile_headers the program runs fine, hence I am certain that this is a precompiled header issue.
Thanks to #drescherjm, adding target_precompile_headers(${PROJECT_NAME} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_SOURCE_DIR}/MyProject/pch.h>" got rid of the errors.

Clang doesn't know PTRDIFF_MAX?

I'm upgrading our build machine (from Windows Server 2008 to Windows 10) and have a build that ran fine on the old machine (using Visual Studio). I had some problems getting it to run with VS Build Tools (I'm trying to avoid needing a VS license) on the new machine, so I'm porting it to use Clang.
The build uses Flatbuffers, which I have upgraded from 1.4.0 to 1.12.0. I'm getting the following error during the build:
[2/70] Building CXX object CMakeFiles\polyphemus.dir\hardwareconfig.cpp.obj
FAILED: CMakeFiles/polyphemus.dir/hardwareconfig.cpp.obj
C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\Llvm\x64\bin\clang-cl.exe /nologo -TP -I"C:\Program Files (x86)\nanomsg\include\nanomsg" -I. -I"C:\Program Files (x86)\FlatBuffers\include" -IE:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\include /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\polyphemus.dir\hardwareconfig.cpp.obj /FdCMakeFiles\polyphemus.dir\polyphemus.pdb -c E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\hardwareconfig.cpp
In file included from E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\hardwareconfig.cpp:3:
In file included from .\hardwareconfig_generated.h:7:
In file included from C:\Program Files (x86)\FlatBuffers\include\flatbuffers/flatbuffers.h:20:
In file included from C:\Program Files (x86)\FlatBuffers\include\flatbuffers/base.h:45:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\algorithm(37,63): error: use of undeclared identifier 'PTRDIFF_MAX'
return static_cast<ptrdiff_t>(_Min_value(static_cast<_CT>(PTRDIFF_MAX), static_cast<_CT>(_Value)));
Not having PTRDIFF_MAX defined is really rather odd, and suggests that Flatbuffers should have done #include <cstdint> in its files, but hasn't. This seems like something that would cause tests to fail, so I can't believe that's an undiagnosed bug. Sure enough, if I add #include <cstdint> to my files, they start building. This really doesn't seem like the correct solution though - I can't believe that Flatbuffers has an undocumented dependency that you have to do that import yourself, which suggests I'm doing something wrong. Does anyone know what, please?
EDIT: Explicitly turning on C++17 in cmake (and from there in clang) results in the same error in a different place that doesn't involve flatbuffers:
In file included from E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\complexseries.cpp:2:
In file included from E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\include\polyphemus/complexseries.h:4:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\complex:12:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\sstream:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\istream:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\ostream:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\ios:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xlocnum:16:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\streambuf:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xiosbase:12:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\system_error:14:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\stdexcept:12:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xstring(1258,47): error: use of undeclared identifier 'PTRDIFF_MAX'
return _Min_value(static_cast<size_t>(PTRDIFF_MAX), static_cast<size_t>(-1) / sizeof(_Elem));
That's in a file where I'd done my own #include of , so I guess that's a red herring (and also not a fix :-/ ).
Turned out that I had in my include path a version of stdint.h that specifies it was for use "only with Microsoft Visual C++ compilers!". Unsurprisingly, it turns out it doesn't work with Clang. You'd expect it to either fail catastrophically or else just work anyway (it does define PTRDIFF_MAX), but I guess it just causes Clang to be very very confused.
Anyway, if I remove it from my include path, things start working.

Using two different math libraries in the same project confuses Visual C++

My project needs to use both Micorsoft Visual C++ math.h and Intel MKL math.h.
Building with verbose details, I get:
1> Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\cmath
1> Note: including file: E:\3rdParty\MKL\2017.1.143\windows\compiler\include\math.h
1> Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\../../vc/include/math.h
1> Note: including file: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\crtdefs.h
1> E:\3rdParty\MKL\2017.1.143\windows\compiler\include\math.h(1577): warning C4005: 'HUGE_VALF' : macro redefinition
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\../../vc/include/math.h(104) : see previous definition of 'HUGE_VALF'
1> E:\3rdParty\MKL\2017.1.143\windows\compiler\include\math.h(1579): warning C4005: 'HUGE_VALL' : macro redefinition
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\../../vc/include/math.h(105) : see previous definition of 'HUGE_VALL'
1> E:\3rdParty\MKL\2017.1.143\windows\compiler\include\math.h(1581): warning C4005: 'HUGE_VAL' : macro redefinition
1> C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\../../vc/include/math.h(96) : see previous definition of 'HUGE_VAL'
The "'HUGE_VALF' : macro redefinition" message is the one that made me be suspicious.
At first I just disabled that warning, but considering that this option would only mask a potential problem, I am looking for an alternative solution.
From lines 1 and 2, you can see that Visual Studio's cmath does not include Visual Studio's math.h, as it should, but MKL's file with the same name.
How can I set my CMakeLists.txt file so that the compiler can pick the right include files?
Just wrap one library.
For example, create header file:
#pragma once
namespace imath {
double sin(double a);
}
And in cpp
#include "Wrapper.h"
#include <intel/math.h>
namespace imath {
double sin(double a) {
return ::sin(a);
}
}
Do this for every symbol you need to use in common source.
And do not include C version of math.h you are using C++ so #include <cmath>.

syntax error in VC xtree internal header with Qt/boost project

I'm using Boost with a Qt project.
I added these lines to my .pro file.
INCLUDEPATH += C:/local/boost_1_62_0/
LIBS += "-LC:/local/boost_1_62_0/lib64-msvc-12.0/"
I'm linking against boost, as I use a the libICP library, that includes boost/multi_array and boost/array.
When I compile, I get a lot of errors :
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree:20: error: C2143: syntax error : missing ',' before '<'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree:21: error: C2518: keyword 'typename' illegal in base class list; ignored
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree:22: error: C2518: keyword 'typename' illegal in base class list; ignored
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xtree:132: error: C2065: '_Iterator_base0' : undeclared identifier
I suspect xtree is used by the implementation of multi_array or array, but I don't really know where to go from here.
Ok, so I found the solution (I was working on this issue since yesterday...)
In case someone else has a similar issue, it was just a case of unbalanced } at the end of the file ICP_Registration.h, that includes libICP.

Visual studio 2015 xlocnum C++ issue

I am using Visual studio version 14.0.25431.01 Update 3. While upgrading an existing project in C++ from VS2k13 to VS2k15, I am getting the following build errors in xlocnum file placed at C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xlocnum.
Error C2912 explicit specialization 'void std::numpunct<char>::_Getvals(void)' is not a specialization of a function template Scripttest C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xlocnum 192
and
Error C2061 syntax error: identifier '_Elem2' InterPTest C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xlocnum 188.
Following code in xlocnum is throwing the error
template<_Elem2>
void _Getvals(_Elem2, const lconv *_Ptr, _Locinfo::_Cvtvec _Cvt)
{ // get values
_Dp = _MAKLOCCHR(_Elem2, _Ptr->decimal_point[0], _Cvt);
_Kseparator = _MAKLOCCHR(_Elem2, _Ptr->thousands_sep[0], _Cvt);
}
After explicitly typenameing template<_Elem2> with template, error resolves but the change has to be made in the windows file xlocnum itself, so its doesn't seem a valid solution. Please suggest a resolution for this issue, Thanks