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

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.

Related

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.

GLFW: 'WGL: The driver does not appear to support OpenGL' error when creating window

I am porting my simple lwjgl3 game to C++, and got Error: WGL: The driver does not appear to support OpenGL. The code is almost identical in java, so I'm wondering what's wrong and how to fix it:
int main(int argc, char** argv) {
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
glfwDefaultWindowHints();
//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4);
GLFWwindow* window = glfwCreateWindow(640, 480, "Example", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create window" << std::endl;
glfwTerminate();
return -1;
}
...
I am using msys2 and mingw-w64, and compiled the program in mingw-w64 shell. First I thought the problem was with the glfw3 i built, so I used lwjgl3 copy of glfw instead and got the same error. I have tried the recommended hints, context versions, etc., but to no avail. I am pretty sure that the driver supports OpenGL, and just to make sure I also checked for updates.
While testing stuff to see what works and doesn't, I have confirmed that all glfw and SFML examples are not working (both I have built with mingw-w64). No errors during compilation, no exceptions and/or errors in cmd, no window created as well.
I'm not very familiar with windows development so I'm not sure if this is an issue with the mingw-w64, glfw or wgl.
Using only the standalone mingw-w64 solved it. The same program runs fine now without any errors, not to mention that the glfw and SFML examples now run as well.
I have not yet discovered what could be causing the error, I suppose it has something to do with msys2 but for now the problem is solved.

C++ strange "undefined reference"

I'm relatively new to c++, so bear with a little.
I have a class with the constructor:
Window(int width, int height, const std::string& title);
As defined in the header file.
I then have the code:
#include "window.h"
int main(int argc, char** argv) {
new Window(800, 600, "Elysian Engine");
}
in Main.
When building, I am getting the error "undefined reference to 'Window(int, int, std::string const&)'" Which I do not understand, as I thought I am correctly importing it and everything. I understand this to be a linking error, but I'm not sure why.
Thanks!
--- EDIT ---
The code for window.cpp:
#include "window.h"
#include <SDL2/SDL.h>
#include <SDL/SDL.h>
#include <GL/glew.h>
Window::Window(int width, int height, const std::string& title) :
width(width),
height(height),
title(title),
isCloseRequested(false) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
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);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
}
}
Window::~Window() {
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
}
Because your code does not appear to be causing the issue, I will address the IDE.
When I first started using Code::Blocks, I ran into the "undefined reference" problem quite a bit. There are multiple ways that I solved this.
Exit Code::Blocks and re-open it. This solved my issues more times than I would care to count. It's similar to finally realizing that an executable is acting strangely because you needed to run make clean before compiling; sometimes, you are compiling an old version of code without realizing it.
Delete window.h and window.cpp from the project and re-add them. This is similar to the solution above. Although this has worked for me before, I couldn't figure out exactly why it worked.
Include the full path of window.h. Instead of #include "window.h", try #include "/path/goes/here/window.h". It is possible that your current location (as specified in your #include statement) is incorrect.
Check the compiler that is selected for the project. When I used Code::Blocks, I typically used gcc. However, there was a case where I was receiving all sorts of errors (upon attempting compilation) that I either did not understand or could not resolve, only to learn that I had accidentally selected some gcc variant from the compiler selection list that I did not even have installed.
Create a new project. Move your source files out of the project folder that is created by Code::Blocks, and then remove that project folder. Launch Code::Blocks, create a new project, and add your source files to the new project.
When all else fails and you absolutely must use Code::Blocks, reinstall the full package. I had to do this my first time using Code::Blocks (which was also my first time using an IDE). I realized that I had a bare-bones installation of the IDE, which was limiting what I could do (including compiling). Naturally, I cannot access the Code::Blocks website right now, otherwise I would offer a link here as well. In this case, I think it is safe to say that the full-featured stable version will be the largest (in MB) you can find on Code::Blocks' Downloads page. After you have reinstalled, create a new project and add your files back to it.
I eventually moved to using writing code in an IDE (for the linting!), and taking care of a makefile on my own. It has been awhile since I've used Code::Blocks, but each of the above bullets represents solutions/work-arounds that have helped me for your specific problem (or, in general) while I was using Code::Blocks.

Why cant I use some GLFW methods

I'm using Linux OS and Eclipse. For some reason eclipse does not recognise some of the GLFW commands despite the fact that glfw3.h is included properly. I'm unable to use GLFWwindow, glfwCreateWindow and glfwDestoyWindow. Anyone know why?
Edit
Just to be specific my IDE is underlining these methods as if they are not defined.
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
int main() {
if (!glfwInit()){
return 0;
}
GLFWwindow* window = glfwCreateWindow(680, 480, "test", NULL, NULL);
cin.get();
if (window){
glfwDestroyWindow(window);
}
glfwTerminate();
return 0;
}
If I remember correctly, the Eclipse editor in C/C++ mode uses the compiler to generate its symbol database, so unless you build the editor will not recognize new symbols.
So the solution is to actually build your project, even if the editor tells you there are errors.

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.