How to include a dependency in a header without including it in projects that include the header

大憨熊 提交于 2019-12-25 08:16:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!