Clang doesn't know PTRDIFF_MAX? - c++

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.

Related

error MSB6006: "link.exe" exited with code 1 When run SDV ( Static Driver Verifier) , Visual studio 2019 , SDK 10 build 19041, windows 10 build 19041

We create new KMDF USB project ,
Open visual studio 2019 v 16.11, go to File -> new -> project -> chose Kernel Mode Driver, USB (KMDF).
without any modification, this project have no error, no warning when building.
when we run SDV ( Static Driver Verifier) for this project the smvbuild.log show this error.
C:\Program Files (x86)\Windows Kits\10\TOOLS\SDV\smv\bin\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\tungpvt\source\repos\USB Driver2Release\USBDriver2.sys" /VERSION:"10.0" /INCREMENTAL:NO /NOLOGO /WX /SECTION:"INIT,d" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\BufferOverflowFastFailK.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\ntoskrnl.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\hal.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\wmilib.lib" "C:\Program Files (x86)\Windows Kits\10\lib\wdf\kmdf\x86\1.15\WdfLdr.lib" "C:\Program Files (x86)\Windows Kits\10\lib\wdf\kmdf\x86\1.15\WdfDriverEntry.lib" usbdex.lib ntstrsafe.lib "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\wpprecorder.lib" /NODEFAULTLIB /MANIFEST:NO /DEBUG /PDB:"C:\Users\tungpvt\source\repos\USB Driver2Release\USBDriver2.pdb" /SUBSYSTEM:NATIVE,"10.00" /Driver /OPT:REF /OPT:ICF /ENTRY:"FxDriverEntry#8" /RELEASE /IMPLIB:"C:\Users\tungpvt\source\repos\USB Driver2Release\USBDriver2.lib" /MERGE:"_TEXT=.text;_PAGE=PAGE" /MACHINE:X86 /PROFILE /SAFESEH /guard:cf /kernel /IGNORE:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221 /osversion:10.0 /pdbcompress /debugtype:pdata Release\Device.obj
Release\Driver.obj
Release\Queue.obj
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(1074,5): error MSB6006: "link.exe" exited with code 1. [C:\Users\tungpvt\source\repos\USB Driver2\USB Driver2\USB Driver2.vcxproj]
Done executing task "Link" -- FAILED.
Done building target "Link" in project "USB Driver2.vcxproj" -- FAILED.
Done Building Project "C:\Users\tungpvt\source\repos\USB Driver2\USB Driver2\USB Driver2.vcxproj" (rebuild target(s)) -- FAILED.
In additional, when running the link.exe on command line, Visual studio -> Tools -> Command Line -> Developer Command Prompt with same parameter like above it shows:
Failed to load Intercept.XML:
Could not find file 'c:\program files (x86)\windows kits\10\tools\sdv\smv\bin\%BE%-Intercept.xml'.
we've added the BE and smv variables to Windows system environment
variable name: BE , variable value: MSBuild
variable name: smv , variable value: C:\Program Files (x86)\Windows Kits\10\Tools\sdv\smv
In Linker session in Project properties, we modified Project properties -> Linker -> Input -> Additional Dependency to
%(AdditionalDependencies);$(DDK_LIB_PATH)usbdex.lib;$(DDK_LIB_PATH)ntstrsafe.lib
Then we can pass the command line
C:\Program Files (x86)\Windows Kits\10\TOOLS\SDV\smv\bin\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\tungpvt\source\repos\USB Driver2Release\USBDriver2.sys" /VERSION:"10.0" /INCREMENTAL:NO /NOLOGO /WX /SECTION:"INIT,d" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\BufferOverflowFastFailK.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\ntoskrnl.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\hal.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\wmilib.lib" "C:\Program Files (x86)\Windows Kits\10\lib\wdf\kmdf\x86\1.15\WdfLdr.lib" "C:\Program Files (x86)\Windows Kits\10\lib\wdf\kmdf\x86\1.15\WdfDriverEntry.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\usbdex.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\ntstrsafe.lib" "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\km\x86\wpprecorder.lib" /NODEFAULTLIB /MANIFEST:NO /DEBUG /PDB:"C:\Users\tungpvt\source\repos\USB Driver2Release\USBDriver2.pdb" /SUBSYSTEM:NATIVE,"10.00" /Driver /OPT:REF /OPT:ICF /ENTRY:"FxDriverEntry#8" /RELEASE /IMPLIB:"C:\Users\tungpvt\source\repos\USB Driver2Release\USBDriver2.lib" /MERGE:"_TEXT=.text;_PAGE=PAGE" /MACHINE:X86 /PROFILE /SAFESEH /guard:cf /kernel /IGNORE:4198,4010,4037,4039,4065,4070,4078,4087,4089,4221 /osversion:10.0 /pdbcompress /debugtype:pdata Release\Device.obj
Release\Driver.obj
Release\Queue.obj
But still run the SDV error.
please help me to fix this.
thanks.
===========================================================================
After I update to latest SDK, WDK to version 10.1.22000, and update visual studio 2019 and the error of the template driver gone away, but I facing other problem in my project in
sdv/smvcl.log
c1 : fatal error C1252: Circular or missing dependency between plugins: 'PREfast Drivers Plugin' requires GUID '{EB170136-3433-4C16-AE1D-8998BA5CAB16}'.
can anyone help me?
Hi I had this problem (linker error on static driver verifier build) with WDK 10.0.22000 (with visual studio 2019) and here's the solution I've found: it links when the macro NT_PROCESSOR_GROUPS is defined at compile time (have to rebuild all), so adding this macro to the preprocessor definitions does the trick (at least for my setup).
Just in case you are running across static driver verifier failures now or in the future, here is a useful hint:
Run SDV target with /debug enabled: so
msbuild /t:sdv /p:Inputs="/check:* /debug" path\to\myproject.vcxproj /p:Configuration=Release /p:Platform=x64 /maxCpuCount=8
will create debug logs. The output of the linker is found in the sdv subdirectory
and is named smvlink1.log . There are other useful logfiles in that directory (took me a long time to find them but the output directed me to the solution I proposed above).
You're not alone in encountering problems with SDV in Visual Studio 2019. I ran into this with a mature driver after updating the tools and rebuilding. Fortunately, as is often the case, the good guys at OSR already ran headfirst into this wall themselves, and found a solution:
It seems that if you try to run SDV on a driver project that has “Enable Code Analysis on Build” set to YES, SDV will always fail in the way you see illustrated above. So, the fix is to disable Code Analysis on Build and be sure to save the project to disk. Then run SDV.
The driver I was working on did indeed have "Enable Code Analysis on Build" turned on. Turning it off allowed SDV to build it successfully.

Update visual studio 2017, now getting compile error C7510: 'Callback': use of dependent template name must be prefixed with 'template'

I tried compiling my project as usual after the update (15.8.0). I set showincludes to yes to figure out where the error originates, but it's all system code. Starting with stdafx.cpp, it goes through all the includes and errors out:
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\poppack.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\poppack.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\winrt\wrl\event.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\winrt\eventtoken.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\pshpack8.h
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\winrt\wrl\event.h(316): error C7510: 'Callback': use of dependent template name must be prefixed with 'template'
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\winrt\wrl\event.h(324): error C7510: 'Callback': use of dependent template name must be prefixed with 'template'
Has anyone seen this before? I googled up and down to find an answer to no avail. Short of modifying the windows sdk, not sure what to do.
Edit:
In my installed windows SDK, the error was in the file-
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\winrt\wrl\event.h
Changed line 316: return DelegateHelper::Traits::Callback(Details::Forward(callback));
to: return DelegateHelper::Traits::template Callback(Details::Forward(callback));
and line 324: return DelegateHelper::Traits::Callback(
to return DelegateHelper::Traits::template Callback(Details::Forward(callback));
Since modifying an sdk is not really a solution, Peng Du's solution by selecting non conformance in the configuration window is the way to go.
I have legacy projects and I compared the project settings side by side, finally I successfully built the new project by setting: Configuration Properties > C/C++ > Language > Conformance mode = No
When you use a dependent template name, you have to use a template keyword, for example:
foo.template bar<T>();
Till some moment MSVC was not strict about using typename and template disambiguators, but after an update the rules have changed.
The problem is in the Windows Runtime Library and some change to Visual Studio broke it. I have the same issue on this laptop which is updated to 15.8.2, my machine at home running an earlier version does not do this, since the exact same code compiles on my other machine, it's got to be a bug in VS, or a change required in WRL/event class.
EDIT: Fix to the return values above worked, bug is in the SDK, you should NOT disable /permissive- as this enables protection against Spectre and other security enhancements.

visual studio 2015 doesn't see the header

I'm trying to compile project connected with drivers, but VS doesn't see the header files ntddk.h and vadefs.h. I've found both of them located in:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\km
added them in projects properties:
but it stil doesn't work..
You've got a $ before the path, so it's getting confused and treating it as if you want a macro called $C:\Program which you don't have. Just use this:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\km\;%(AdditionalIncludeDirectories)

clang and <iostream> on windows

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.

Identify which file has included some particular header file

Sometimes with a complex header structure it happens some header is included, but it is hard to tell where from.
Is there some tool (depedency viewer?) or a method how to find the "inclusion stack" (which source / which header / which header / ...) is including one particular header file?
If the header file is included multiple times, finding first inclusion is sufficient, finding all inclusions is a welcome bonus.
Someone has posted about it but I can't find this answer.
So, In VS, go to your project properties. Choose Configuration Properties / C/C++ / Advanced / Show Includes and set "yes".
then compile you cpp file. It looks like this:
cpp file:
#include <stdio.h>
int main()
{
return 0;
}
In the output window after compiling you will see:
1>------ Build started: Project: stlport_project, Configuration: Release Win32 ------
1>Compiling...
1>stlport_project.cpp
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stdio.h
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stl/_prolog.h
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stl/config/features.h
and so on
EDIT: reference to the same question Displaying the #include hierarchy for a C++ file in Visual Studio
The header you are searching for may not be directly included into the source file. You need to 'preprocess_only' the code. This can be done in g++ by using the -E option; I don't know enough about visual C to know what the exact specification is there but if you look in the help for 'preprocess' you may come up with something.
A somewhat hacky approach (but one which should work on any platform/toolchain, without needing a separate dependency analyser) is simply to add a #error at the top of the included header - you will then get a compilation error from the first .cpp file which includes it.
Visual Studio /showIncludes
Directly in the Visual Studio I have found an option called /showIncludes - the output is textual only, but indented in a way which makes reading it quite easy:
Note: including file: /*..path.anonymized..*/\TCMalloc\windows\config.h
Note: including file: /*..path.anonymized..*/\memalloc\tcmalloc\windows/port.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\windows.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\excpt.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\sal.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\vadefs.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\stdarg.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\windef.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\ctype.h
Note: including file: C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
ProFactor Include Manager
There is also a VS add-in called Include Manager which seems to provide the needed functionality in a very nice visual way.