0

Okay, so new to C++, I have background in C#, Java, and Lua.

1.) I created a header file with some structs and objects defined in it. (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.) Next, I created the associated CPP file (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.) I am using the default build configuration for the project (i.e. I created the project, I went to the "headers" folder that placed in the newly generated project by Visual Studio on project creation, and I created my header there).

Some possibilities as to what's going on:

1.) There's some code that goes into creating headers that I'm not familiar with yet (i.e. is there a #header compiler directive or something?). So, basically, a newbie fuck-up.

2.) The project somehow got corrupted at some point. Entirely possible. I'm not familiar enough with build systems conceptually and practically with whatever build system Visual Studio C++ uses out of the box to check for this.

3.) Visual Studio doesn't automatically define the <project>/Header Files/ folder as the location of headers. This is also possible, but I don't know why Visual Studio would waste energy building the Header Files folder if I have to manually tell the compiler to look for headers in the folder anyway. At that point, might as well just tell it to look for headers with the source files (which also doesn't work, by the way; moving Game.h into the same folder as Game.cpp does nothing to resolve the problem).

4.) I have to manually add every file to the default build-system (seems unintuitive, and Visual Studio is pretty intuitive, so this feels unlikely; if this is the case, why can't the project just automatically detect all headers in the main project directory? Why do we have to manually define them to the compiler? And how do we go about doing this?)

Project structure (ignore the GUI icon in RGame.h - I created another header and moved all the code from the first one to the second one to test if it was a Visual Studio bug specifically relating to the Game.h file):

enter image description here

Sources I used to understand headers:

  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
Aamir
  • 1,974
  • 1
  • 14
  • 18
Jax
  • 402
  • 7
  • 24
  • 2
    #include says look in the system location first. Did you add the folder that contains Game.h to your "Additional Include Directories" location? – drescherjm Jan 12 '23 at 18:09
  • @drescherjm How do I include header files in the project directory? – Jax Jan 12 '23 at 18:10
  • VS lets you include other files in a project folder that are not part of the build, so the answer is "4.) I have to manually add every file to the default ". Go to Solution Explorer, Click on headers or cpp, then right-click, Add, Existing Item. – Dave S Jan 12 '23 at 18:11
  • 1
    `#include "Game.h"` looks in the location that is relative to the individual cpp file being compiled. So if `Game.h` is in the same folder as your cpp file it should work. – drescherjm Jan 12 '23 at 18:11
  • @DaveS Is there any way to tell Visual Studio to include all header files found in the extended project directory? – Jax Jan 12 '23 at 18:12
  • 1
    Set the `c/c++->General->Additional Include Directories` project property setting for each configuration you use: Debug, Release ... – drescherjm Jan 12 '23 at 18:13
  • BTW, you should not be `using namespace std` in a header file. Unimportant now, but may save you trouble in larger projects. – Friedrich Jan 13 '23 at 10:54
  • @Friedrich Why? As long as I can ensure the global namespace doesn't have any function or type conflicts, it seems easier to just import the namespace than manually typing `std::` every time I want to use them - what is gained from those extra keystrokes? – Jax Jan 17 '23 at 21:20
  • @Jax See https://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file You can do whatever you like in your implementation files, though. – Friedrich Jan 18 '23 at 06:01
  • @Friedrich So, wait, that adds another question. If I require a file in a header (`#include`), and if I use the `using` directive to require that namespace in the header, that `using` directive gets propagated to every implementation file that requires that given header? – Jax Jan 19 '23 at 14:19
  • @Jax yes. The preprocessor will replace `#include "foo.h"` with the contents of foo.h. So it's as if you wrote your directive into every implementation file. – Friedrich Jan 19 '23 at 14:35
  • @Friedrich How does the compiler know which implementation of a header file is the "correct" one, in the case of multiple implementations? – Jax Jan 23 '23 at 19:25
  • @Friedrich To clarify, I'm asking, say we have `a.cpp` and `b.cpp` and both `include` a header file (`c.h`) with defined types and functions, but only one of them actually has an implementation for the header file defined. How does the compiler order the code such that the type and function definitions that happen in `c.h` and the implementation that happens in `b.cpp` are put before the code in `a.cpp` (that uses the functions and types but doesn't implement them)? – Jax Jan 23 '23 at 19:28
  • @Jax you mean the functions are _declared_ in `c.h` and _defined_ in `b.cpp`. And the answer is: the compiler doesn't. The linker does. In fact, that's what the linker is there for. This is really getting off topic and while I appreciate your curiosity I won't answer any more questions here. – Friedrich Jan 24 '23 at 06:06

1 Answers1

0

You could refer to drescherjm's method to include the header file directory in the project or put the header file into the project.

  1. Right-click the solution and choose Open Folder in File Explorer. enter image description here
  2. Copy the header files you need into this folder. enter image description here
  3. Call the header file. enter image description here

About #include, I suggest you read this document, it will help you understand it.

Yujian Yao - MSFT
  • 945
  • 1
  • 3
  • 9