问题
So when you #include a header in C++, the compiler just copies the content of the header and pastes it where the #include was. I'm making a small game engine/framework and I'm including GLEW & GLFW in my Engine.h because I need things like GLFWwindow. But I get a compiler error in my Test Game 1 Project that uses the engine because since the compiler is just copying the code form engine.h and pasting it in test game 1's main.cpp, It's trying to include glew and glfw in my test game 1, which I don't want it to and I haven't linked against the glew and glfw libs in that project. How do I fix it so I can still access glew and glfw in engine.h without including them in test game 1?
回答1:
You have two options. 1. Either you statically link your project with glew and glfw libraries and then call GLFWwindow function 2. Or you load glew and glfw libraries in the c++ code using LoadLibrary() and then call GLFWwindow function using GetProcAddess()
来源:https://stackoverflow.com/questions/39812086/how-to-include-a-dependency-in-a-header-without-including-it-in-projects-that-in