我创建了一个使用GLEW的库,我有下一段代码:
// ShaderProgram.hpp
#include <GL.hpp>
#include <vector>
namespace LIBRARY {
class LIBRARY_API ShaderProgram {
public:
ShaderProgram() {
// If I uncomment the code will works, but why it doesn't work without because I already initialize GLEW in the main() function
// glewExperimental = GL_TRUE;
// std::cout << (glewInit() == GLEW_OK) << "\n"; // <- Outputs true
std::cout << (glCreateProgram) << "\n" // <- Null function!
m_program = glCreateProgram(); // <- Here crash when trying to call glCreateProgram(), I mean it can't find the function
}
};
}
这是我的库代码的一部分。然后我创建了一个小应用程序来测试它,我使用SFML for window:
int main() {
...
sf::Window window(sf::VideoMode(800, 600, 32), "OpenGL", sf::Style::Titlebar | sf::Style::Close, settings);
window.setFramerateLimit(60);
window.setActive(true);
glewExperimental = GL_TRUE;
std::cout << (glewInit() == GLEW_OK) << "\n"; // <- Outputs true
std::cout << (glCreateProgram) << "\n" // <- Not null function adress, so it's good!
LIBRARY::ShaderProgram shaderProgram; // <- Here crash
...
所以,在main()中glCreateProgram不是null,它是一个可调用的函数,但是在库中是null并且我无法理解为什么因为我在glew init之后调用库代码,因为它可以看到在我构建ShaderProgram的代码中。
Thread 1 received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) bt
#0 0x00000000 in ?? ()
#1 0x68a015a2 in LIBRARY::ShaderProgram::ShaderProgram() ()
from path\to\my\library.dll
#2 0x00401843 in main ()
我不明白为什么在应用程序GL中工作,但在图书馆中它没有。
编辑:编辑代码以更好地解释问题。
答案 0 :(得分:0)
问题在于我是静态链接GLEW。通过动态链接解决。