简体   繁体   English

Visual Studio C ++能够编译时出现编译错误(红色下划线)

[英]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: 这里是一个编辑过的环境截图:

能够使用编译错误构建

On the LEFT is my Visual Studio Solution Directory list with the "Show All Files" off. 在左侧是我的Visual Studio解决方案目录列表,关闭“显示所有文件”。

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). 例如,学生A和B都是AST和PARSER文件夹(我们称之为子项目)。 We have an API for each sub-project so other sub-projects know what to call. 我们为每个子项目都有一个API,以便其他子项目知道要调用什么。

At the TOP-CENTER, we have my Source File for a class QueryProcessor . 在TOP-CENTER,我们有一个QueryProcessor类的源文件。 (just the first few lines) Below it, is the Output for the Build Success . (只是前几行)在它下面,是Build Success的输出。

The red lines are all over all the classes, mainly cause the #include "stdafx.h" cannot be opened by the environment. 红线遍布所有类,主要是因为#include "stdafx.h"无法被环境打开。

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 在右边,这是stdafx.h ,我们包含所有不同的子项目,所以我们省去了每个项目有不同stdafx.h的麻烦

However, I am able to build the project. 但是,我能够构建项目。 I am pretty sure I am doing this directory/linking wrongly. 我很确定我正在做这个目录/链接错误。

This should work 这应该工作

  1. Right click on the solution file 右键单击解决方案文件
  2. Click Open in Windows Explorer 单击Windows资源管理器中的打开
  3. Find file stdfx.h in explorer and copy the path of the folder 在资源管理器中查找文件stdfx.h并复制该文件夹的路径
  4. In visual studio solution explorer, Right click on the project file 在visual studio解决方案资源管理器中,右键单击项目文件
  5. Click properties-> C/C++ -> General 单击属性 - > C / C ++ - >常规
  6. In the Additional Include Directories paste the path 在“附加包含目录”中粘贴路径
  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. 在VC中组合文件夹和虚拟文件夹是我的观点,因为虚拟文件夹指示所有文件都在一个目录中,并且在硬盘驱动器上创建的文件夹显然表明所有文件都在不同的目录中。 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. 我假设您错过了解stdafx.h的目的。此头文件的目的不是将所有标题文件放入其中,然后将其包含在所有其他文件中。 Here is a SO question about this Purpose of stdafx.h 这是关于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 清理stdafx.h文件后,在.cpp文件中包含尽可能多的头文件,如果头文件中需要这些文件,则只将这些包含在头文件中

  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. 打开显示所有文件,现在您将使用实际文件夹,并且您可以确定如果您在“PKB”之类的文件夹中找到该文件夹​​确实存在,因为您可以在左侧解决方案资源管理器中看到它。

  5. If you use using namespace std; 如果使用namespace std; for example make sure you also include the required header files. 例如,确保您还包含所需的头文件。 You might think "hey I already included eg 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. 您可能会认为“嘿,我已经将iostream包含在另一个头文件中,我现在将其包含在此头文件中,因此我不需要它”当您使用更大的项目时,这将真正摧毁您。

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. 哦,关于stdafx.h包含问题,只要你切换到显示所有文件我假设你会发现stdafx与你使用include的文件不同。 Maybe something like #include "..\\stdafx.h" is required (depending on your structure). 也许需要#include "..\\stdafx.h"类的东西(取决于你的结构)。

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. 我认为这是显而易见的,但是如果你包含一个头文件,那么include就是相对于包含另一个头文件的文件。

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. stdafx.h通常用于创建预编译头,它实质上是编译时优化,因此编译器不会为每个编译单元连续编译这些头。

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. 实际上,最好只使用它来包含标准标题和第三方标题(如boost库和类似标题),这些标题你永远不会改变。

You may decide that some of your own libraries are "set in stone" and can also be included. 您可能会认为您自己的某些库是“一成不变的”,也可以包含在内。

Every project, ie 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 每个项目,即构建到单独单元(DLL或.exe)中的项目的每个部分都应该有自己的预编译头和它自己的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. 项目只应包含自己的.stdafx而不包含其他项目的项目,因此此头文件也可用于定义dllexport宏。

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. 在安排项目标题时,您应该注意:1。外部包含哪些标题2.哪些标题仅包含在内部,甚至不包含在外部。

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. 后一种类型应该包括您的stdafx.h文件,理想情况下不应该与项目外部包含的那些头文件位于同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM