PDA

View Full Version : How do I build GLEW for mingw-w64 using cmake on Windows?



InfiniteLoop
07-30-2016, 12:48 AM
I downloaded the source files for GLEW here (https://sourceforge.net/projects/glew/files/glew/2.0.0/glew-2.0.0.zip/download), but have no idea how to build it for mingw-w64 on Windows using the cmd prompt. I've tried cmake ./cmake but it automatically makes files for Visual Studio.

Also tried downloading the the Windows binaries, but from what I read, those libs only work for Visual Studio.

Does anybody know how to correctly build GLEW for mingw-w64 on Win8 using only the cmd prompt?

By the way, this is how I'm building my little game and linking the necessary libs:



g++ src/main.cpp src/core.cpp src/renderer.cpp src/vkdebug.cpp src/debug.cpp src/time.cpp -I include -I include -L lib -lglew32 -lOpenGL32 -lglfw3dll -o Game


At this moment I'm linking against the lib files found in the GLEW Windows 64bit binary zip. It compiles with no errros, but when I run my game, it fails to Init GLEW, I'm guessing because the libs are for Visual Studio.

Thanks and my apologies, I'm still learning.

mhagain
07-30-2016, 04:03 AM
If the libs were incompatible with your build environment it would fail to link.

Here's a useful tip: in at least 99% of cases if you're thinking "it's my build environment", or "it's my libs", or "it's my OS", or "it's my driver", it's actually not: it's your code.

In this case the most likely cause of GLEW failing to initialize is that you're calling glewInit before you've created your OpenGL context. Review your code, check that this isn't the case, and post back.

InfiniteLoop
07-30-2016, 10:58 AM
If the libs were incompatible with your build environment it would fail to link.

Here's a useful tip: in at least 99% of cases if you're thinking "it's my build environment", or "it's my libs", or "it's my OS", or "it's my driver", it's actually not: it's your code.

In this case the most likely cause of GLEW failing to initialize is that you're calling glewInit before you've created your OpenGL context. Review your code, check that this isn't the case, and post back.


Thanks for your response Mhagain, but didn't call glewInit before creating the OpenGL context.



if (m_useOpenGL) {
glfwMakeContextCurrent(m_win);

glewExperimental = true;
GLenum glewinit = glewInit();
if (glewinit != GLEW_OK) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(glewinit)); // Prints:
throw std::runtime_error("**** Failed To Initialize GLEW ****\n");
}
}


I've googled this issue very intensely and a lot of the search results suggested what you mentioned.

I'm definitely doing something wrong, I agree, but specifically due to not knowing how to use cmake to build GLEW. At least that's what I think is going on. I honestly don't have the slightest idea how to use cmake to build GLEW for mingw-w64. You can find tutorials for mingw (32bit), or Visual Studio, but I fail to find one for mingw-w64 using the MS command prompt.

As I mentioned in my opening post, I'm linking against pre-compiled libs that apparently work only for Visual Studio. I have to learn how to compile my own using the source code, and I don't know how to do that for GLEW. I'm very confused. Unless there's another solution, or I'm horrible searching on Google :(

Edit:

GLEW's github page offers a tutorial for MSYS2 (https://github.com/nigels-com/glew), but unfortunately I'd need the system admin's pw to install MSYS2 on to this computer, which is why I'm desperately looking for a simple solution using only cmake and the command prompt.

InfiniteLoop
07-30-2016, 10:50 PM
I solved it! I'll share my solution just in case any fellow newbies are at the verge of going crazy (I almost did). First off, my issue had to do with incorrect libs as I predicted. You do want to make sure you're not calling glewInit before creating a gl context as mhagain suggested, but in my case that wasn't the problem. I was linking my program to pre-compiled lib files that apparently only work for MS Visual Studio.

My setup:

- Windows 8.1
- Vim (Code Editor)
- Mingw-w64 6.1.0 (64bit)
- GLFW 3.2
- GLEW 2.0.0

That's it, nothing fancy.

Step by Step

1) Download the GLEW Source files (https://sourceforge.net/projects/glew/files/glew/2.0.0/glew-2.0.0.zip/download). DO NOT download the 32/64bit Window Binaries unless you're using Visual Studio, and if you are, you wouldn't be reading this.

2) Download (https://msys2.github.io/), install, and update msys2. Unfortunately, I was unable to find a way to compile GLEW (for mingw-w64) without it, but trust me, it's painless, and once you're done, you don't need msys2, we're only using it to compile the GLEW lib files. Follow the update instructions at the official msys2 site (https://msys2.github.io/) before you do anything else. It's only 3 steps long.

Warning: For some strange reason, once you pass the second update step, the msys2 command prompt shortcut stops working on my computer. Just navigate to the c:/msys2 folder and run msys2_shell.cmd if it does.

3) You'll need mingw-w64 for msys2. Some Stack Overflow answers suggest downloading both 64bit and 32bit versions of mingw, but since I'm no expert on the matter, it turned into a pain, so I was better off just downloading the 64bit version which is what I'm using and targeting. Run the following cmd in the msys2 cmd prompt: $ pacman -S gcc make mingw-w64-x86_64-gcc

3-a) You'll need make and gcc for msys2. Run this in the msys2 cmd line: $ pacman -S gcc make

4) Once you're done downloading mingw-w64, gcc, and make. You're ready to compile GLEW. Unzip the source files you downloaded earlier, and put it into you're C:\msys64\home\yourusername folder.

5) Open the msys2 cmd prompt, navigate to the glew2.x.x folder and run the following CMDs in the following order (as soon as each one is done):
5-a) make
5-b) make install
5-c) make install.all

6) You might get some errors and warnings, don't worry. Look in the lib folder within the glew2.x.x folder, and you'll see the lib files you'll need.

7) Copy glew32.dll into the same folder where your programs executable is, and libglew32.a into your program's lib folder.

8) ...and this is how i'm compiling my program (run it in the command prompt or just make a .bat file and run that):

g++ src/main.cpp src/core.cpp src/renderer.cpp src/vkdebug.cpp src/debug.cpp src/time.cpp -I include -I include -L lib -D GLEW_STATIC -lglew32 -lopengl32 -lglfw3dll -o Game


Done! Works perfectly now. Just remember you want to make sure you're creating your GL context appropriately, which could also be the root cause of any compiler or "Missing GL Version" errors. Good luck!

One last thing,
It's a good idea calling any GLEW functions after glewInit :) At least that's what I read in several forums and it definitely makes sense. My first post does the opposite.