OpenGL multi-architecture setup - c++

I've spent over 8 hours on this today.
Here's what I'm trying to do:
I want to create a project that uses GLFW, GLEW and nanogui to build an application.
I haven't started yet with nanogui, but I can already tell it's going to to be a blast.
Anyway, my primary goal is to setup four (Debug x86, Release x86, Debug x64, Release x64) environments with openGL and have them all work. This is what I've got:
Solution directory
Project directory
include
GL
eglew.h
glew.h
glxew.h
wglew.h
GLFW
glfw3.h
glfw3native.h
lib
Win32
glew32.lib
glew32s.lib
glfw3.dll
glfw3.lib
glfw3dll.lib
x64
glew32.lib
glew32s.lib
glfw3.dll
glfw3.lib
glfw3dll.lib
The Win32 and x64 directories have x86 and x64 files respectively.
Moving on to configuration:
(All configurations, all platforms)
Project properties -> C/C++ -> Additional Include Directories -> $(SolutionDir)include
Project properties -> Linker -> Inputs -> Additional Dependencies ->
opengl32.lib
glew32s.lib
glfw3.lib
(All configurations, Win32)
Project properties -> Linker -> Additional Library Directories -> $(SolutionDir)lib\Win32
(All configurations, x64)
Project properties -> Linker -> Additional Library Directories -> $(SolutionDir)lib\x64
I have also copied glew32.dll to project folder (x86 version) and to all of the configuration outputs (x86 for x86 and x64 for x64). I expected for x86 version to work at least.
This is the only file in project:
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char *argv[]) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(600, 800, "Name", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (window = nullptr) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
return -2;
}
glViewport(0, 0, screenWidth, screenHeight);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
glfwTerminate();
return 0;
}
Aaaaand it doesn't work. The program returns code -2, meaning glewInit() failed.
Even if all this worked, it's not really what I want. I don't want to have to package glew32.dll with my program. In the end, I want my program to be a single .exe file.
So, what did I do wrong here and how can I change it to make it work?
Better yet, how can I make it work so I don't have to provide glew32.dll with the .exe?

Related

Unable to include GLFW/glfw.h on Visual Studio 2019

I am setting up Visual Studio for C++ OpenGL. I tried following a tutorial on youtube (https://www.youtube.com/watch?v=OR4fNpBjmq8) but it still keep on getting this error: "cannot open source file "GLFW/glfw3.h"
#include <GLFW/glfw3.h>
int main()
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
It is exact same as the demo on the official GLFW website but it does not work. Any way to fix this?
It is exact same as the demo on the official GLFW website but it does
not work. Any way to fix this?
Thanks for your sharing the tutorial with me and l have followed this guidance and this works in my side and builds without any errors. So please follow my steps to troubleshoot your issue:
1) add the path of glfw3.h into Additional Include Directories(Right-click on your project-->Configuration Properties-->C/C++-->General-->Additional Include Directories).
2) add glfw3.lib into Additional Dependencies(Right-click on the project-->Configuration Properties-->Linker-->Input-->Additional Dependencies) and do not forget to add opengl32.lib into it
3) close,reopen VS and then you will rebuild your project without any errors.
In addition, if you want to enter into glfw3.h file to see its function when you debug it, you should add the path into Library Directories(Right-click on the project-->Configuration Properties-->VC++ Directories-->Library Directories).
Hope it could help you.

Visual studio community 2017 LNK2019 unresolved external symbol

I've been looking around for the whole day and I find no solution. Im using opengl extensions glew, glfw and glm. Visual Studio Community 2017 throws up the exception: Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp__glewInit#0 referenced in function _main Name Directory.
My project is located: C:\Users\timbucktoo\Desktop\Projects\Scape\Scape
My extensions are located: C:\Users\timbucktoo\Desktop\Projects\Scape\Scape\OpenGL Exetensions Binaries\unzipped
I have the c++ include directories added: C:\Users\timbucktoo\Desktop\Projects\Scape\Scape\OpenGL Exetensions Binaries\unzipped\glm-0.9.8.4 %281%29\glm;
C:\Users\timbucktoo\Desktop\Projects\Scape\Scape\OpenGL Exetensions Binaries\unzipped\glfw-3.2.1.bin.WIN64\glfw-3.2.1.bin.WIN64\include;
C:\Users\timbucktoo\Desktop\Projects\Scape\Scape\OpenGL Exetensions Binaries\unzipped\glew-2.0.0-win32\glew-2.0.0\include;
I have the Linker general library directories added:
C:\Users\timbucktoo\Desktop\Projects\Scape\Scape\OpenGL Exetensions Binaries\unzipped\glew-2.0.0-win32\glew-2.0.0\lib\Release\x64;
Visual studio has told me I have a x86 machine, I am not fully aware of what this means but from what I have assumed this machine is running 64-bit windows.
I have tried mixing up all the options like the different add directories/libraries option and I can come up with no solution. This may have been asked before but not on the visual studio community 2017. Please help. THanks. :)
Oh and here is the code which I also tried using the pragma comment with glew32.lib.
#include <stdlib.h>
#include <stdio.h>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
int main()
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do {
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
}
LNK2019 unresolved external symbol __imp__glewInit#0 referenced in function _main
This tells you that it can't find the external symbol. Which is because you aren't linking glew32.lib.
As you're using MSVC / Visual Studio, try adding glew32.lib to your project directory. Then add glew32.lib under Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies. Since you're using MSVC you can also (as you already mentioned) add the following:
#pragma comment(lib, "glew32.lib")
If you have glew32.lib in your project directory and added the line above to your .cpp file. Then that linker error shouldn't come.
Now (given your example) just add something like glClear() and you then get something akin to:
LNK2019 unresolved external symbol __imp__glClear#4 referenced in function _main
Then that's because you also need to add opengl32.lib to your linker.
#pragma comment(lib, "opengl32.lib")
If you get a linker error like this:
LNK4272: library machine type 'X86' conflicts with target machine type 'x64'
It means that you're building for x64 but some of your libraries are built for x86. In other words you might be using the x64 version of glew32.lib, while you actually need to use the x86 version of glew32.lib.
If your code compiles but when the application is run you get something like:
The program can't start because glew32.dll is missing from your computer.
Then add the glew32.dll to your project directory.
Again version matter, so if the application crashes saying something like:
The application was unable to start correctly (0xc000007b)
Then like before your application might be built for x86, but the glew32.dll is the one built for x64.
Verify everything above, and it should compile and run.
Last but not least. Instead of using GLEW I highly recommend switching to GLAD instead. GLEW is old and broken and as far as I can tell not really supported anymore.
Additionally GLAD is way easier to setup and use. You go to the generator, select the version you want to target, press generate and download an implementation (.c) file and a few headers (.h). That's it. No libs to link against, no dll's to carry around or anything.
I have a x86 machine, I am not fully aware of what this means
x86 is an instruction set architecture. However x86 is more commonly used to refer to 32-bit. Whereas x64 is used to refer to 64-bit. The architecture is the same, it's just 64-bit instead of 32-bit. The 64-bit version of the x86 architecture is also called
x86-64 or AMD64.
This also means that a 64-bit CPU can execute 32-bit instructions. But a 32-bit CPU can't execute 64-bit instructions. Which is why applications usually target 32-bit computers.

Undefined reference to 'glewExperimental', 'glewInit#0', '__glewGenBuffers'

I'm sorry if this is a duplicate question but I've looked through StackOverflow and Google and haven't come up with an answer yet.
I'm using Code::Blocks and SDL and trying to get GLEW working with openGL.
The program code is:
#include <Windows.h>
#include <SDL.h>
#define GLEW_STATIC
#include <glew.h>
int main(int argc, char *argv[])
{
//initialize SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
SDL_GL_DeleteContext(context);
SDL_Quit();
return 0;
}
When I build it, it returns:
undefined reference to 'glewExperimental'
undefined reference to 'glewInit#0'
undefined reference to '__glewGenBuffers'
It's not returning an error finding glew.h, so I assume it's a problem with the linker finding the .lib files.
I've added the folder where the .lib files are under Project > Build Options > Search Directories > Linker and added 'glew32s', 'glew32' (I've tried switching the precedence on glew32s and glew32), and 'opengl32' under Linker Settings > Link Libraries, and added '-lglew32s', '-lglew32' (again tried switching the precedence and removing one or the other), and '-lopengl32' under Linker Settings > Other linker options.
I even tried adding the .lib files, .h files, and .dll files to the project folder and adding search directories to the project folder. It still returns the same error.
I'm using the most recent version of glew (1.13.0) on a 64-bit OS. I've tried linking to the 64-bit and 32-bit versions (because I'd read most compilers compile in 32-bit by default for compatibility). Am I linking to the wrong files?
Thanks for reading.

Unhandled exception at 0x5401FA96 (sfml-system-d-2.dll)

I am trying to make a simple program with SFML. The source code is the same provided in this example
Given that I downloaded SFML from the site using the precompiled version from Visual Studio 12.
I configured the project normally, and added the correct libraries for debug and release.
Note:
There were no compiler errors, and not linker errors as well. I also added the .dll libraries in the .exe directory.
When I run the program, here is the output (just crashes):
Exception thrown at 0x50FAFA96 (sfml-system-d-2.dll) in TestSFML.exe: 0xC0000005: Access violation reading location 0xCCCCCCD8.
If there is a handler for this exception, the program may be safely continued.
From what I looked on the internet, this error seems to be related to mixing debug and release libraries, but I think I did it right.
Note that I am using Visual Studio Community 2015, but I don't think this could cause the problem.
The configuration inside the project
Release and Debug (All Configurations):
Added the same directory for the include path (release and debug) and library path (release and debug).
Release
On Linker -> Input I added:
sfml-window.lib
sfml-system.lib
Debug
On Linker->Input I added:
sfml-system-d.lib
sfml-window-d.lib
Since I am using dynamic libraries, I also added the libraries on the directory of the executable.
EDIT
Main.cpp
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
int main()
{
// create the window
sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
// load resources, initialize the OpenGL states, ...
// run the main loop
bool running = true;
while (running)
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw...
// end the current frame (internally swaps the front and back buffers)
window.display();
}
// release resources...
return 0;
}
Aditional dependencies on the project. They were set on Active(Debug)
opengl32.lib
sfml-system-d.lib
sfml-window-d.lib
The compilation
1>------ Build started: Project: TestSFML, Configuration: Debug x64 ------
1> main.cpp
1> TestSFML.vcxproj -> C:\Users\Leonardo\documents\visual studio 2015\Projects\TestSFML\x64\Debug\TestSFML.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
I got the same error when trying to compile a SFML program with SFML for VS2013 when using VS2015. Downloading (and obviously using) the correct version solved the problem.

compiled program cannot find freeglut.dll

I'm new to this site, and relatively new to programming.
I've been doing some C++ programming for a while using Visual Studio 2010, and I wanted to get into OpenGL, so I bought the OpenGL Superbible to get started. I've gotten stuck on the second chapter's "simple" project.
After hours of research I've been able to download all the necessary files to use freeGLUT and GLtools. I've made sure that everything is in the right place for the program to work.
Now, it appears as though everything has been worked out... except for one odd problem.
I was instructed that I needed to place freeglut.dll into Windows\System32, so I did. The project will build now, but when I go to run it, it tells me
"The program can't start because freeglut.dll is missing from your computer. Try
reinstalling the program to fix this problem."
Now, I am certain that freeglut.dll is in Windows\System32 as it should be, so what's the problem? how do I solve it?
Here's the original code from the book:
#include "../../shared/gltools.h" //OpenGL toolkit
//////////////////////////////////////////////
//called to draw scene
void RenderScene(void)
{
// clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
//Flush drawing commands
glFlush();
}
////////////////////////////////////////////////////////
//set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f , 0.0f, 1.0f, 1.0f );
}
///////////////////////////////////////////////////////////
//main program entry point
void main(void)
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
This is the code that actually compiled, but would not run (it's a bit of a mess from all the conflicting data I got from different resources):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GLTools.h>
#include <gl/GLUT.h>
//called to draw scene
void RenderScene(void)
{
// clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
//set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f , 0.0f, 1.0f, 1.0f );
}
//void main(void)
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
Try putting the DLL in the same folder as your exe file. The advice to put it in Windows\System32 predates a lot of newer Windows security restrictions.
#ScottMcP-MVP 's answer solved my problem, but I thought I'd add some detail that doesn't really fit in the comment.
My solution:
Add the following subdirectory structure to your solution folder:
In x86, put the 32-bit versions of GLut, GLew and anything else you need.
In x64, put the 64-bit versions of same.
I went ahead and put all .dll, .lib, and .h in the corresponding folders here rather than placing them in the Windows SDK (Or, in Win7+, the "Windows Kits" folder) to ensure that my projects in SVN had the correct version, and checking out on another machine would retrieve all dependencies. This required adding the include and target-specific lib folders to the project properties:
Set your Include Directories field to
$(SolutionDir)ThirdParty\Include\;$(IncludePath)
And your Library Directories field to
$(SolutionDir)\ThirdParty\$(PlatformTarget)\lib\;$(LibraryPath)
Note that all of these should be applied to the "All Platforms" build configuration. The $(PlatformTarget) macro will make sure that the correct lib's and dll's are used. The include folder is target-agnostic, so I've placed it in the root of my ThirdParty folder.
To get the required files into your output folder, add the following post-build event to your project configuration (under "All platforms"):
xcopy $(SolutionDir)ThirdParty\$(PlatformTarget)\*.dll $(OutputPath) /Y
That will copy the correct version of the DLLs to your output folder on build. This keeps you from having to manually put the DLLs in our output folder, and is more compatible with source control where you typically don't want to include your output, bin or debug folders.
Once I had all of this configured, I created a VC OpenGL project template since getting everything configured took 30 minutes of my life I'd rather have back.