Visual Studio C++ 未检测到项目目录中新创建的头文件

问题描述 投票:0回答:1

好吧,C++ 新手,我有 C#、Java 和 Lua 背景。

1.) 我创建了一个头文件,其中定义了一些结构和对象。 (

Game.h
)

#pragma once
#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#include <GL/GL.h>
#endif

#include <map>
#include <vector>
#include <string>
#include <glm/glm.hpp>


using namespace std;
using namespace glm;


//Tutorial found here; https://learnopengl.com/Model-Loading/Mesh


namespace GSGEngineCore
{
    class Game
    {
        public:
        void OnStart(); //Called when the game is initially created.
        void OnUpdate(float delta); //Called immediately before the frame renders.
        void OnRender(float delta); //Called immediately after the frame is updated.
        void OnFixedUpdate(int updateProcessID, float deltaTime); //Called in fixed intervals.
        void OnDestroy(); //Called when the game is terminated.
    };
}


namespace GSGEngineGraphics
{
    struct Vertex
    {
        public:
        vec3 position;
        vec3 normals;
        vec2 texCoords;
    };

    struct Texture
    {
        public:
        unsigned int id;
        string type;
        string tag;
    };

    struct Mesh
    {
        public:
        vector<unsigned int> indices;
        vector<Texture> textures;
        vector<Vertex> vertices;
        Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures);
        private:
        unsigned int VAO, VBO, EBO;
        void SetupMesh();
    };

    class Graphics
    {
        private:

        public:
        /// @brief Fetches a texture specified by the given tag.
        /// @param key - The string value that represents the texture in the graphics engine.
        /// @return A Texture specified by the tag value.
        Texture GetTexture(string tag);

        /// @brief If the texture specified by the tag exists, it is overwritten by the new texture, otherwise, it is added to the internal map<string, texture> object.
        /// @param tag - The string representation of the given texture.
        /// @param tex - The actual texture that we are accessing.
        /// @return 
        void SetTexture(string tag, Texture tex);
    };
};

2.) 接下来,我创建了关联的 CPP 文件 (

Game.cpp
):

#pragma once
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#include <GL/GL.h>
#endif
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <glm/glm.hpp>

#include <Game.h>

using namespace std;
using namespace glm;
//No code yet, just proof of concept to demonstrate the issue.

3.) 我正在使用项目的默认构建配置(即,我创建了项目,我转到了 Visual Studio 在项目创建时放置在新生成的项目中的“headers”文件夹,并在那里创建了我的标头) .

关于正在发生的事情的一些可能性

1.)有一些代码用于创建我还不熟悉的标头(即是否有

#header
编译器指令或其他东西?)。所以,基本上,这是一个新手错误。

2.) 该项目在某个时候不知何故被损坏了。完全有可能。我对构建系统的概念和实践都不够熟悉,对于 Visual Studio C++ 使用开箱即用的任何构建系统来检查这一点。

3.) Visual Studio 不会自动将

<project>/Header Files/
文件夹定义为标头的位置。这也是可能的,但我不知道如果我必须手动告诉编译器在文件夹中查找标头,为什么 Visual Studio 会浪费精力构建标头文件文件夹。此时,不妨告诉它查找源文件的标头(顺便说一句,这也不起作用;将
Game.h
移动到与
Game.cpp
相同的文件夹中并不能解决问题)。

4.)我必须手动将每个文件添加到默认构建系统(似乎不直观,而 Visual Studio 非常直观,所以这感觉不太可能;如果是这种情况,为什么项目不能自动检测所有标头在主项目目录中?为什么我们必须手动将它们定义给编译器?我们该如何做?)

项目结构(忽略

RGame.h
中的 GUI 图标 - 我创建了另一个标头,并将所有代码从第一个标头移至第二个标头,以测试它是否是专门与
Game.h
文件相关的 Visual Studio bug):

我用来理解标题的来源:

  1. http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/SeparateCompilation.pdf
  2. https://websites.umich.edu/~eecs381/handouts/CppHeaderFileGuidelines.pdf
  3. https://www.geeksforgeeks.org/header-files-in-c-c-with-examples/
  4. https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-170
c++ visual-studio visual-c++
1个回答
0
投票

可以参考drescherjm的方法将头文件目录包含在工程中或者将头文件放入工程中

  1. 右键单击解决方案并选择“在文件资源管理器中打开文件夹”。
  2. 将需要的头文件复制到该文件夹中。
  3. 调用头文件。

关于

#include
,我建议你阅读这个文档,它会帮助你理解。

© www.soinside.com 2019 - 2024. All rights reserved.