0

I am developing hobby opengl3 engine and I decided to make a new .dll build of it. It uses GLEW to get opengl3 functions from GPU. I've successfully builded it about 5 months ago, but now I've changed a lot and it doesn't want to get working.

So the .dll builds perfectly (windows7, mingw). I've made a simple program and it crashes on first call to glCreateProgram which is runned by code from mylib.dll.

in pseudocode:

#include "mylib.hpp"
int main(){
   std::cout << (void*)glCreateProgram << "\n";  
   // displays 0 as glCreateProgram haven't been loaded yet

   myspace::Window* = new SDL2BasedWindow(...);
   //this constructor is from my .dll and calls glewInit() which returns GLEW_OK
   std::cout << (void*)glCreateProgram << "\n";
   //this displays proper address 
   int testGLEW= glCreateProgram();
   std::cout << "glCreateProgram from main: " << testGLEW<< "\n";
   //this displays 1 which means valid call to glCreateProgram
   window->runApplication(new Application(...));
   //Application is user-defined class, it further creates myspace::ShaderProgram 
   //which is imported from my .dll (part of my engine) which then calls 
   //glCreateProgram in it's initialisation 
   //(it is first call to any function which should be initialized by GLEW if we count only code imported from mylib.dll)


}

    //in ShaderProgram constructor:
    std::cout << "trying to call glCreateProgram, address: ";
    std::cout << (void*)glCreateProgram << "\n";  //this displays 0 (!)
    int id = glCreateProgram();                   //this ends execution with SIGSEGV
    printf("created program: %d\n", id);          //so this one is never called

So my question is, why GLEW works only in code which is not imported from my .dll and how can I fix it?

Btw I've checked nm mylib.dll ant it contains glCreateProgram and other glew dependent functions, I also use #define GLEW_STATIC both in .dll and program that uses this .dll

Thanks for your help!

user7428910
  • 61
  • 1
  • 7

1 Answers1

0

#define GLEW_STATIC is used to build a static library or executable.

To build a .dll use #define GLEW_BUILD instead.

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • I've changed GLEW_STATIC to GLEW_BUILD when compiling .dll and it still the same – user7428910 Jul 03 '17 at 16:49
  • Ok, I've moved the code from place to place a bit and it works now, thanks for your time, unfortunatelly I dont have reputation to rate your comment – user7428910 Jul 03 '17 at 17:22
  • It looks like initializing it twice: firstly in main and secondly in .dll works Do you have any better idea? – user7428910 Jul 03 '17 at 17:38
  • Perhaps you need some advise with `dllexport/dllimport` matters. Read [this](https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport). Glew must be initialized just once, likely right after the context creation. – Ripi2 Jul 03 '17 at 18:01