How-to find issues concerning includes - visualize inclusion tree [duplicate] - c++

This question already has answers here:
Displaying the #include hierarchy for a C++ file in Visual Studio
(9 answers)
Closed 9 years ago.
Scenario
From time to time I get compiler errors in system headers. Currently for example:
c:\Programme\Microsoft Visual Studio 9.0\VC\ce\include\streambuf(55) : error C2143: syntax error : missing ')' before 'string'
As always, this is a false error message and of course there is a problem in my code. However, the compiler is not capable to tell me where it hurts. So I was looking for the usual suspects, e.g.
"using namespace XXX" in some header files or something like that.
My problem is, I do not even know via which way the file streambuf was included into my code. At least I do not include it directly.
Concrete question
Is there a way to get some kind of "inclusion tree"?
Something like
myClass.cpp
+ myClass.h
+ ios
...
+ streambuf
so I would be able to determine the error location a bit better.

I typically use
cl /c /P /d1PP file.cpp
This creates a file.i. This is the preprocessed file - it contains all the headers as processed i.e. if a particular part of the header is under ifdef something & you haven't defined that something, it will not contain that block. The /d1PP (undocumented, I think) also show you where the macros are actually defined. You also will see who included streambuf in your code and at what point.
I then compile file.i as
cl /c /Tp file.i (or cl /c /Tc file.i - if it's C and not C++)
For visualisation, try this - http://www.codeproject.com/Articles/3478/Include-File-Hierarchy-Viewer

Related

In Visual Studio C++, How to quickly find necessary header files?

I know a fair amount of Java and Eclipse IDE, but am new to Visual Studio and C++. In Eclipse/Java, if you use a predefined class, Eclipse helpfully suggests the appropriate header file to include for the code to compile. Wondering if Visual Studio has similar functionality.
For example every time I use a code sample from the web, I spend a lot of time Googling which header files to include so the code will compile. My current challenge: I'm writing a small utility that reads filenames in a directory into an array for batch renaming. For this, I'm using following code fragment:
DIR* dir;
struct dirent* dirEntry;
dirEntry = readdir(dir);
Visual Studio is giving the error message: "DIR" is unidentified. "readdir" is unidentified.Is there an efficient way to locate the appropriate header files for C++ code fragments to resolve error messages like these? Thanks.
I tested this feature in vs2019 community 16.3.6 and it works. When you hover the mouse at the location of an error, you can see an error light bulb. And click the drop-down arrow next to the error bulb to add missing #include.
You can also press Alt+Enter.
As mentioned in some answers above, Visual Studio has started offering some suggestions for header files. But as of this writing, some VS suggestions lead to other error messages. E.g. I just used getline(). VS gave error message: Identifier "getline" is unidentified. It suggested I add using namespace std::basic_istream; to my code. But this was not applicable to my code and produced additional error messages.
After stumbling around, I found a very simple solution: Visit the C++ reference website. There I searched for getline and found the header information at the following link: getline(). To fix the error, I needed to #include <string>.

(MSVC 2017 /WALL & /WX) Including Iostream Produces 800 Warnings

Reviewing material for an optimized C++ course next quarter. The professor for this course is enforcing /WALL and /WX for our project properties. The problem I'm having is that including the Iostream library produces over 800 warnings. Here's the code I'm attempting to run:
#include "pch.h"
#include <iostream>
int main() {
std::cout << "Hello World";
return(0);
}
A few of the warnings that I'm receiving includes:
C4514 'abs': unreferenced inline function has been removed
C4774 'sprintf_s': format string in argument 3 is not a string literal
C4820 'std::basic_ios ...': '7' bytes of padding added after...
Before asking Stack I emailed the Prof to ask about the warnings and was told:
You should be including iostream
If you get 100 warnings you included a header that's not needed
Is there something I'm missing? I know I wouldn't be able to edit source files for iostream as that's not portable coding. I looked around to see if I could explicitly include functions such as cout, cin, etc. Yet, I don't feel like this is the correct solution.
Edit:
A user requested an example of a more explicit warning message in case there was something missing in there. Here are a few:
C415 'abs': referenced inline function has been removed (Project: Hello World) (File: stdlib.h)
C4710 'int sprintf_s(char *const....: function not inlined. (Project: Hello World) (File: stdio.h)
The professor is using GCC through Visual Studio and our settings are pulled from a repository as premade projects.
Your professor is, quite simply, wrong.
This has nothing to do with "including a header that's not needed" (why would that generate warnings?), but with using /WALL, which reveals some flaws in the stdlib implementation there!
This switch is not recommended; quoting James McNellis who gets it bang-on under the above referenced question:
/Wall enables many warnings that, while potentially useful sometimes, are not useful most of the time. /Wall in Visual C++ does not mean the same thing as -Wall on g++ (really, g++ "has /Wall wrong," since it doesn't actually enable all warnings). In any case, in Visual C++, all of the commonly important and useful warnings are enabled by /W4.
I would use /W4 in Visual Studio (and -Wall -Wextra in GCC).
Obviously I can't help you to persuade your professor of this, other than to suggest saying something along the lines of "I asked on Stack Overflow and found out that this is due to /Wall being too strict and generating warnings on Visual Studio's own headers. They suggest we use /W4 instead. . What do you think?"
It is true that you need to #include <iostream>, and it is true that you should never modify the provided standard headers. Also, don't forget to stream a '\n' to end your output line!

What's the cause of a D8049 error in visual studio?

I'm creating a project with openframeworks (the full source is here: https://github.com/morphogencc/ofxAsio/tree/master/example-udpreceiver), and the empty project seems to compile fine.
I added the ASIO library, and a few header classes, and now the project seems to be give me the following error:
1>------ Build started: Project: example-udpreceiver, Configuration: Debug x64 ------
1> main.cpp
1>cl : Command line error D8049: cannot execute 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\c1xx.dll': command line is too long to fit in debug record
1>cl : Command line error D8040: error creating or communicating with child process
I couldn't find any examples of error D8049 on stackoverflow or even on Microsoft's pages, and google turned up painfully few results. The only remotely useful one was this github issue:
https://github.com/deplinenoise/tundra/issues/270
But I'm still not sure what's causing the problem. Is anyone familiar with this error, and can recommend a method for troubleshooting what's causing it?
thanks in advance!
For me, working with UE4, this was an intermittent error.
I added "bLegacyPublicIncludePaths = false;" to the innermost block of project.Build.cs and recompiled without errors.
Then I removed that line and compiled again w/o errors.
The error message suggested adding "DefaultBuildSettings = BuildSettingsVersion.V2;" to project.Target.cs which worked.
This is a bit of a weird sounding error, as it is from essentially internally generated data. However, you do have control over that. Taking the error message at face value, you probably have many/lots of defined symbols passed in on the command line (or the the ones you do have have lengthy definitions), or you may have some lengthy file paths.
If you look under the project properties, one of the selections under the C++ section is "Command Line", which will show you exactly what gets passed to the compiler. When you view that you can see where you have many or lengthy parameters, and then make changes to shorten them.
Too many defines? Put them in a header (possibly stdafx.h) and include them that way.
Long file paths? Shorten the paths, put the files somewhere else, or set up file system aliases to your real directories that use shorter paths.

C++.NET, Link can't find file Debug/.obj (notice strange file name)

I have added some information to this question, below the line of xxxx.
I am attempting to move a large ActiveX (legacy) control from Visual C++ version 6 to Visual Studio 2010. The build went just fine under VSC++6. The automated conversion (within VS2010) seemed to run smoothly. There were a number of minor errors which had to be corrected in the code, such as having to declare variables to be integer rather than letting them default. Upon fixing the last error and building, the link step gave this error:
1>LINK : fatal error LNK1104: cannot open file '.\Debug\/.obj'
I have searched and found a couple people had asked about this problem, but the solutions were very project specific. I could find nothing that applied to my situation.
There were two projects in the original solution. To attempt to solve the problem, I separated them into two separate solutions, one of which produces a .dll and works and the other (to produce the activeX component) which fails with the above error.
I really don't know where to look.
I have no .cpp nor .h files with Debug in the filename.
There must be something telling the linker to look for this file... but where? How would I even recognize it. (I'm not even sure what the forward slash in the filename represents. Is it escaping the period... or does the pair of characters (backslash forwardslash, \ /) escape the forward slash meaning it is part of the filename?)
Where do I even start?
I think that I have heard that Visual Studio generates command lines which actually do the compilation and linking. Is there any way to see the generated command line to do the Link?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I just located 3 files with names and content as shown. They seem to be
related to the problem. The 3rd file shows the bad file name (twice). Can anyone tell me where in the Visual Studio GUI they are set up? Or what may cause the erroneous entries in the last file?
custombuild.command.1.tlog -------------------
^C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\QGETL32.ASM
..\Assembler\ml /Fo.\Debug\Qgetl32.obj /coff /I. /Zi /c /Cx /Ta Qgetl32.asm
^C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\ROT32.ASM
..\Assembler\ml /Fo.\Debug\Rot32.obj /coff /I. /Zi /c /Cx /Ta Rot32.asm
custombuild.read.1.tlog ----------------------
^C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\QGETL32.ASM
^C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\ROT32.ASM
custombuild.write.1.tlog ---------------------
^C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\QGETL32.ASM
C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\DEBUG\.OBJ
^C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\ROT32.ASM
C:\DOCUMENTS AND SETTINGS\USER1\DESKTOP\QUILT00\DEBUG\.OBJ

C++ Compiler Error C2371 - Redefinition of WCHAR

I am getting C++ Compiler error C2371 when I include a header file that itself includes odbcss.h. My project is set to MBCS.
C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\odbcss.h(430) :
error C2371: 'WCHAR' : redefinition; different basic types 1>
C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winnt.h(289) :
see declaration of 'WCHAR'
I don't see any defines in odbcss.h that I could set to avoid this. Has anyone else seen this?
This is a known bug - see the Microsoft Connect website:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98699
The error doesn't occur if you compile your app as Unicode instead of MBCS.
There are a half-dozen posts on various forums around the web about this - it seems to potentially be an issue when odbcss.h is used in the presence of MFC. Most of the answers involve changing the order of included headers (voodoo debugging). The header that includes odbcss.h compiles fine in it's native project, but when it is included in a different project, it gives this error. We even put it in the latter project's stdafx.h, right after the base include for MFC, and still no joy. We finally worked around it by moving it into a cpp file in the original project, which does not use MFC (which should have been done anyway - but it wasn't our code). So we've got a work-around, but no real solution.
This error happens when you redeclare a variable of the same name as a variable that has already been declared. Have you looked to see if odbcss.h has declared a variable you already have?
does this help?
http://bytes.com/forum/thread602063.html
Content from the thread:
Bruno van Dooren [MVP VC++] but i know the solution of this problem.
it solves by changing project setting of "Treat wchar_t as Built-in
Type" value "No (/Zc:wchar_t-)". But I am using "Xtreme Toolkit
Professional Edition" for making good look & Feel of an application,
when i fix the above problem by changing project settings a new
linking errors come from Xtreme Toolkit Library. So what i do to fix
this problem, in project setting "Treat wchar_t as Built-in Type"
value "yes" and i wrote following statements where i included wab.h
header file. You can change that setting on a per-codefile basis so
that only specific files are compiled with that particular setting. If
you can solve your problems that way it would be the cleanest
solution.
#define WIN16
#include "wab.h"
#undef WIN16
and after that my project is working fine and all the things related to WAB is also working fine. any one guide me, is that the right way
to solve this problem??? and, will this have any effect on the rest of
project?? I wouldn't worry about it. whatever the definition, it is a
16 bit variable in both cases. I agree that it isn't the best looking
solution, but it should work IF WIN16 has no other impact inside the
wab.h file.
--
Kind regards, Bruno van Dooren bruno_nos_pam_van_dooren#hotmail.com
Remove only "_nos_pam"