Visual Studio can't open any C++ header file - c++

I've just started using Microsoft Visual Studio to code with C++, and even when writing a simple code like the following:
#include <iostream>
using std namespace
int main()
{
cout << "Hello world" << endl;
return 0;
}
I end up getting 300 or something errors.

Seems you have not specified the paths to your include and lib directories.
In VS IDE, select Menu /Project/Property/Configuration Properties/VC++ Directories: ( these paths will be slightly different in your case and later you will know how to allow your IDE to include these paths by default to your projects)
Include directories:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\include
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\atlmfc\include
Lib directories:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\lib\amd64
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\atlmfc\lib\amd64

Related

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

How to find which header is including another header in the include tree?

In the D3D12HelloWindow project from Microsoft DirectX samples there is a reference to IID_PPV_ARGS. When I navigate with F12 in VS2017 enterprise, I reach combaseapi.h, but the output from the compiler with /showincludes does not quote it at all.
In the end, the project compiles and run correctly, but I cannot know which header is including directly or undirectly combaseapi.h ?
How can I find which header is using combaseapi.h ?
The goal/problem is to understand a certain implementation (a certain DirectX 12 aspect) so I can use it to enrich my own (in that case a DirectX 12 WPF asset).
I created very small example on my own. It doesn't work but compiling successfully:
#include "stdafx.h"
#include "Objbase.h"
#include "Unknwn.h"
#include "Propsys.h"
int main()
{
IPropertyStore *pPropertyStore;
CoCreateInstance(IID_IUnknown, NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pPropertyStore));
return 0;
}
In Visual Studio 2017, I see combaseapi.h included through Objbase.h header:
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\Objbase.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\winapifamily.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\winpackagefamily.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\rpc.h
...
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\ole2.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\objbase.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\combaseapi.h
...
Above compiler Output should be read in following way. If the line below has more indent it means that header in above line is including header from below line. I am including #include "Objbase.h" and in my .cpp file and going through increasing indentation I see combaseapi.h (last line in attached part of Output). From this I know that Objbase.h through 5 headers through his way is including combaseapi.h.
I am using Windows 10.
I don't see any remarks in Microsoft documentation
Maybe try to check debugger Output again?

Undeclared identifier in limits header in visual studio 2017

While compiling my project which seems extremely primitive
#include <iostream>
int main() {
std::cout << 1;
return 0;
}
I get many similar errors like
E0020 identifier "FLT_RADIX" is undefined smo c:\Program
Files (x86)\Microsoft Visual
Studio\2017\Community\VC\Tools\MSVC\14.10.24930\include\limits 201
and
E0020 identifier "FLT_MIN" is undefined smo c:\Program Files
(x86)\Microsoft Visual
Studio\2017\Community\VC\Tools\MSVC\14.10.24930\include\limits 1084
All errors refer to the limits file. It might be a problem with references to C libs as was mentioned here
Visual studio 2015 gives me errors upon creating a simple test console program
but defining the path to the libs from Windows Kits explicitly didn't solve the problem.

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.