could you remove stdafx.h in visual studio when coding in c++? - 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.

Related

Missing headers file in C++ Windows project

I have inherited some ancient Windows C++ code and have created a project for it in Visual Studio 2010. When I build, I get missing header file errors for system files such as "precom.h" and "Classes.hpp". They do not exist on my machine. Is anyone familiar with these files and where they come from? Thanks.
They're not standard Windows files. My guess is that whoever you inherited the code from forgot to check the files into source control.
Go find that person, and glare at them until they find them!
The name precom.h is suggestive of a precompiled header. If that's true, then it may be a matter of getting the project set up to generate the precompiled header.

Visual Studio plugin to find out which files are being included implicitly

A particular header file may only be including "Foo.h" but Foo.h is including other headers which implicitly include many other headers. I would like to know all the headers that a particular file is including.
In case anybody is wondering why a plugin for Visual Studio; simply because of the way the include directories are set-up in the project. If an external tool does the job and allows me to specify the locations where it can search for the header files, that will do as well.
I don't know of any plugins that do this, but you could whip up a homebrew solution by turning on the showIncludes flag in your projects settings, then doing a full build and parsing the output. The indentation changes based on the nesting of the includes.
Try out Boost.Wave. It is straight forward to dump all the headers included. They also have an example named list_includes that does exactly that.

Visual Studio 2010, Intellisense and PCH: what are the alternatives to ugly stdafx.h?

I recently switched to Visual Studio 2010 and for Intellisense not to take half a minute to show up when using boost libraries, Microsoft's suggestion seems to use precompiled headers.
Except that I never used them before (except when forced to by Ugly ATL Wizards (TM)), so I searched around to figure out how they work.
Basically, the Big Centralized stdafx.h approach seems plain wrong. I never want to include (even cheaply) a whole bunch of header files in all my sources. Since I don't use windows libraries (I make C++/CLI higher level wrappers, then use .NET for talking to the outside world), I don't have "a whole truckload of non-changing enormous headers". Just boost and standard library headers scattered around.
There is an interesting approach to this problem, but I can't quite figure out how to make this work. It seems that each source file must be compiled twice (please correct me if I'm wrong): once with /Yc and once with /Yu. This adds burden on the developper which must manually tweak the build system.
I was hoping to find some "automatically generate one precompiled header for each source file" trick, or at least some "best practices", but most people seem happy with including the world into stdafx.h.
What are the options available to me to use precompiled headers on a per source file basis ? I don't really care about build times (as long as they don't skyrocket), I just want intellisense to work fast.
For starters, you are reading the article wrong. Every file is NOT compiled twice. The file stdafx.cpp gets compiled once with /Yc (c, for create) before anything else and then every other file in your project gets compiled once with /Yu (u, for use) and imports the result of the previously created saved state from stdafx.cpp.
Secondly, the article is 7 years old and is talking about VC++ 6, so you should start off distrusting it. But even assuming the information in it still applies to VC++ 2008 or 2010, it seems like bad advice. The approach it recommends using /pragma hdrstop is solution looking for a problem. If you have headers that contain things you don't want in every file, then they simply shouldn't go in your pre-compiled header.
Your problem basically seems to be that Intellisense is slow for Boost in VS2010? I don't have a direct solution for this problem, but could Visual Assist X be an option for you? I have used it in various versions of Visual Studio now and with great pleasure. Not a direct solution, but it might work for you.
Precompiled headers aren't too bad if you use them properly.
Don't use them as a replacement for proper and precise #includes, but as a way to speed things up. Achieve this by making the precompiled header do nothing in release builds, only speeding stuff up in debug.
You are wrong, each file is only compiled once. You have one .cpp file that is compiled with /Yc and the rest are compiled with /Yu. The file with /Yc, which is stdafx.cpp by default, contains one line, #include "myMainHeader.h" (changed the name from the default) All other .cpp files must start with #include "myMainHeader.h" When your /Yc file is compiled, the entire internal state of the compiler is saved. That file is loaded when each of your other files is compiled. That is why you must start with including the PCH, so that the /Yu option doesn't change the result of compilation, only the time. Xcode does not make this requirement and will use a PCH regardless of if your .cpp file starts with the right include directive. I have used libraries that relied on this and could not be built without PCH.

C++ VS Express 2010 Intellisense

I'm trying to use Visual Studio Express 2010 to write an openGL program, so It's a win32 console application. And I can't get Intellisense to show up for anything, openGL or otherwise.
What am I doing wrong, and how can I force Intellisense to show? (Like you can force the Xcode equivalent with Esc)
From MSDN:
IntelliSense can stop working under certain conditions. Use the following steps to help determine why IntelliSense does not work for C++ projects.
To investigate IntelliSense failure in C++ projects
Make sure that the Visual C++ project contains no compilation errors.
1) If the project is a Makefile project, see How to: Enable IntelliSense for Makefile Projects.
2) Make sure that stdafx.h is on the include path. For more information about include paths in Visual C++ projects, see #include Directive (C/C++) and /I (Additional Include Directories).
Also :*General, All Languages, Text Editor, Options Dialog Box
Provides information about how to turn off IntelliSense.
NB.
IntelliSense LimitationsIntelliSense does not work in C++ projects under the following circumstances: ( there more then this one below )
IntelliSense is not fully supported when you reference a header file multiple times by using the #include directive, and the meaning of that header file changes because of various macro states that are defined through the #define directive. In other words, when you include a header file several times and the header usage changes under different macro states, IntelliSense does not always work.
Please find *.sdf file in project directory and delete it relaunch the solution file. It will show your intellisense

do i need to add this head file "stdafx.h" to run an c++ program in windows

i am new to windows c++ i have created an application in visual studio 2008 and i have created new win32 console and i have compiled an sample c++ program it asked me to add the stdafx header and is it is compulsory to add this header ??? i dont need any windows library....
No, just create an EMPTY project, and add a main .cpp. This will be fine for any very small win32 projects. stdafx is absolutely unnessacery, and to be honest, I don't use it or even know what it is (I have only about a month experience with MSVC).
However, to make win32 applications, you need #include <Windows.h>, which is the windows header file.
To be able to remove the file you have to disable Precompiled headers in project settings. This might increase compilation time, so I'd recommend you just familiarize yourself with the mechanism. Its described on Wikipedia and at MSDN
The wizard will assume that you want something called Precompiled Headers and generate your project based on this assumption. In the new project wizard, make sure that you click "next" rather than "finish" (or similar) and ask for an empty project. With that done, add a new .cpp file and write your code in it. You should no longer need to include stdafx.h in every source file.
Precompiled headers are quite useful in large C++ projects, in that they can significantly reduce compile time. However, for most small to mid-sized projects they are mostly an annoyance. I personally tend to create empty projects -- if I want PCH, I'll add them myself.
This file is not mandatory.
It is created by the Visual Studio Wizard, and has for purpose to be the general include file, that you'll include in all your cpp files, and that'll include all the header files you need. It will serve as being the precompiled header source.
You don't need it, but compilation might break if you selected "use precompiled header" when you created your project and you don't turn off the corresponding "create/use precompiled header" compiler option.
A precompiled header is one that, as the name suggests, is partially pre-processed or compiled, so as to speed up the rest of compilation. Typically, if you were to use this feature, you'd put a bunch of #include statements in stdafx.h, for system headers that (virtually) all of the files in your project use. Things like <windows.h> are commonly put here, as are MFC or ATL GUI libraries. You can also put your own headers in there, but they should be ones that change only quite rarely.