ali
ali

Reputation: 881

Visual Studio C++ able to compile with compile errors (red underlines)

I am having a problem of getting compile errors (red underlines) like:

Error: cannot open source file "stdafx.h"

Here an edited screenshot of the environment:

Able to build with Compile Errors

On the LEFT is my Visual Studio Solution Directory list with the "Show All Files" off.

I am working on a school project, and each Folder are the source files of different parts of the project with different people who are in-charge of them.

For example, Student A and B are incharge of AST and PARSER folders (we will call them sub-projects). We have an API for each sub-project so other sub-projects know what to call.

At the TOP-CENTER, we have my Source File for a class QueryProcessor. (just the first few lines) Below it, is the Output for the Build Success.

The red lines are all over all the classes, mainly cause the #include "stdafx.h" cannot be opened by the environment.

On the RIGHT, that is the stdafx.h where we include all the different sub-projects so we save the trouble of each project having a different stdafx.h

However, I am able to build the project. I am pretty sure I am doing this directory/linking wrongly.

Upvotes: 4

Views: 3186

Answers (3)

CashCow
CashCow

Reputation: 31445

stdafx.h is commonly used for creating a precompiled-header, which essentially is a compile-time optimisation such that the compiler will not continually compile these headers for every compilation unit.

If any of these headers changes, you will need to do a full system rebuild.

In reality it is preferable only to use it to include standard headers plus third-party headers (like boost libraries and similar) that you are not ever going to change.

You may decide that some of your own libraries are "set in stone" and can also be included.

Every project, i.e. every part of the project that is built into a separate unit (DLL or .exe) should have its own precompiled header and its own version of stdafx.h

Projects should only ever include their own .stdafx and not those of other projects, therefore this header file can also be used to define your dllexport macro.

When arranging your project headers you should be aware of: 1. Which headers are included externally 2. Which headers are only included internally, and are not even included indirectly externally.

The latter sort should include your stdafx.h file and should ideally not be in the same directory as those headers included from outside your project.

Upvotes: 1

user238801
user238801

Reputation:

  1. Combining folders and virtual folders in VC is from my point of view messy because the virtual folders indicate that all files are in one directory and the folders created on the harddrive obviously indicate that all files are in different directories. You can combine it if you know what's going on but in your case I would not recommend it.

  2. I assume you missunderstand the purpose of stdafx.h The purpose of this header file is NOT to put all header filles into it and then just include it to all other files. Here is a SO question about this Purpose of stdafx.h

  3. After cleaning up your stdafx.h file include as many header files into your .cpp files and only put these includes in your header files if they are required in the header file

  4. Turn on show all files, now you will work with actual folders and you can be sure that if you adress a folder like "PKB" that this folder really exists since you can see it in the left solution explorer.

  5. If you use using namespace std; for example make sure you also include the required header files. You might think "hey I already included e.g. iostream in another header file which I now include in this header file so I don't need it" That will really destroy you when you work with bigger projects.

Oh and regarding the stdafx.h include problem as soon as you switch to show all files I assume you will realise that stdafx is in a different file than the file where you use the include. Maybe something like #include "..\stdafx.h" is required (depending on your structure).

I think it's obivious but if you include a header file the include is allway relative to the file which is including the other header file.

Upvotes: 1

yohannist
yohannist

Reputation: 4204

This should work

  1. Right click on the solution file
  2. Click Open in Windows Explorer
  3. Find file stdfx.h in explorer and copy the path of the folder
  4. In visual studio solution explorer, Right click on the project file
  5. Click properties-> C/C++ -> General
  6. In the Additional Include Directories paste the path

Upvotes: 1

Related Questions