How to prevent Visual Studio from marking identifiers as undefined in C++? - c++

I have cloned a repository and Visual Studio is showing hundreds of errors in all of the header files due to them not #include-ing all of their used dependencies, even though the solution builds just fine. From what I understand, this is because header files do not need to include everything they use as long as they themselves are included after their dependencies in the files where they are used.
When I asked the repository owner about this they said that this is a bug that was not present in previous Visual Studio editions, therefore meaning that there should be a way to make Visual Studio treat all the identifiers not included in a header as actually included.
I found that main.cpp includes every header and in the correct order, ensuring that even if a given header doesn't include all that it uses it will still compile just fine. Is there a way to make Visual Studio keep track of all the previous includes from the frame of reference of main when in a given header, therefore preventing all the identifier ... undefined errors inside the headers?
EDIT: It seems that the files aren't getting scanned by Visual Studio for some reason, as when I manually open files and pin them, the errors start going away. Is there some way to force Visual Studio to automatically consider the entire solution at once instead of having to open every single header?

Related

Is there a way to link depend C++ files to the main file in Visual Studio Code?

I am having trouble dealing with multiple C++ files in Visual Studio Code (I am a Linux user). Everything is fine, but when I include a header (class definition) file (which includes another .cpp with the class methods and constructors), it gives a strange error "undefined reference to Class_name::Class_name()".
Running the exact same code in Code::Blocks in an empty project is working perfectly fine. If I am using separate files it isn't working, so I guess the problem is how to link the files in VS Code? Is there a way this can be solved?

VC++ C2011 redefinition errors - unused header files

I am working with a large console application. The solution contains 5 projects.
I am adding a 6th project to replace one of the existing projects to allow for the use of a GUI.
I am getting many compiler C2011 errors regarding type redefinition. In particular, they are 'struct' type redefinitions or '[function]: redefinition; different linkage. They come from the header files ws2def.h and winsock2.h.
I've been searching the entire project and the entire solution for where these are included, but I don't see any #include <ws2def.h> or #include <winsock2.h> statements, nor anything to indicate they are used.
However, there are External Dependencies folders/filters included in both the project I'm replacing, and my new project. Both ws2def.h and WinSock2.h exist in those filters. I wouldn't think that having the same file included in separate projects under one solution would cause these issues. Also, I'm getting these errors when building just my new project, meaning it shouldn't see the old project anyway.
Based on the information I've given, are you able to see where my problem may lie? Is it the case that these header files must be #included somewhere within the project and I'm just not seeing it? I've considered deleting both the header files from the External Dependencies filter because it seems I don't need them. Is there a different, common header file that also includes these header files so that they wouldn't show up in a CTRL-Find?
Thank you.
The issue seems to have mostly been based on the horrible file structure of the original large project.
Trying to create a new project within the solution to replicate another project as a GUI instead of Console project meant that the compilation of the project was doubling up on many files.
I found another post (can't find it right now) that stated that when a Visual Studio solution compiles, all of the project files are combined, much like all the #include statements in a .c/.cpp/.h file really just copy all of those files into the file which #included them.
My solution was to create a copy of the large project (for backup purposes), and in the copied project, remove the original Console project and only include the UI Project. Files are not longer doubling up because they don't exist in both projects anymore, just the one still existing in the solution.

could you remove stdafx.h in visual studio when coding in c++?

i just started learning c++ lately and i am using visual studio 2015. But i have to manually remove the #include stdafx.h in my cpp file before upload it to the test server for my class, or it just sends back an error like this:the error from test server
I would like to know why this issue is happening. My guess is that this thing is exclusive to windows and x86 based OS?
Any help would be appreciated. :D
You'll need to upload all your sources. stdafx.h isn't special in that regard. Of course, if your course restricts you to one .cpp upload, then you can't use stdafx.h for that course
stdafx.h is the default name of a precompiled header for a Visual Studio project. However, you can certainly have a file named this on other environments. Note that the Visual Studio compiler processes this header slightly differently than it does other headers, if it is specified as the precompiled header in your project's property pages (C/C++ -> Precompiled Headers - see here for more details). For example, multiple includes of this file with different preprocessor defines will be ignored when it is the precompiled header (which isn't that common, but something to be aware of).
If you want to make portability to non-Visual Studio environment easier, it's usually easier to disable precompiled headers in your Visual Studio project. If you continue to use the stdafx.h file in your code, you will obviously need to upload it to your test compilation server, and may need to modify it and/or its inclusion into your code.

Visual Studio resource change

when we have resources in our project, Visual Studio creates two files: "resources.rc" and "resources.h". Now, every change in rc file (in visual editor), changes header file also.
Sometimes I need to add something manually to resource.h, but after any change in visual editor, my header changes may be even deleted.
I've noticed that VS uses some kind of comments that steers its behaviour. So maybe there is a way to say to VS: "Hey, don't touch this part of file" ?

Problems creating a static C++ library

Greetings fellow programmers! I am trying to create a C++ library to use in other projects. This library is to be a static library that will be linked to other projects.
Problem: The compiler doesn't seem to catch errors that it otherwise would if it were configured as an executable program under Project->Properties>General. In fact I purposely made a syntax error and the compiler
gave me a success regardless.
Is there a way to force the compiler to check everything while building a static lib?
Build Environment: Visual Studio 2010
EDIT: As it turns out, it was a rather simple, yet incredibly subtle mistake. It turns out that Visual studio was treating some of my .cpp files as header files. Because of this, the compiler was not running through the code at all (no one includes .cpp files). When adding files to the library, I must have accidentally selected header instead of C++ file. I thought Visual Studio would only rename the header file. I guess I was wrong!
This is nonsensical. It cannot happen. What is probably happening is that you think that the compiler is compiling your source file, (the one with the syntax error,) but it does not. Perhaps you have not added your source file to your project.
(Then again, C++ has a pretty quirky syntax; I hope you are sure that what you made was in fact a syntax error, and not a syntactically valid language construct.)
If you have more than one project in the workspace (solution) like exe and lib, make sure you click on the exact one to build or set it as the startup project (showing up in bold letters) if you hit the F7 button.