why does it not recognise include files and folders - c++

everytime i try to compile it to test it doesnt recongnise files
#include <GL\glew.h>
#include <GLFW/glfw3.h>
#ifdef _WIN32
#pragma comment(lib, "winmm.lib")
#endif // _WIN32
int main(void)
{
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);
if(!glewInit())
return -1;
/* 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;
}

When you are using a compiler you need to tell it the directory to find files that you include.
In the line
#include <GL\glew.h>
#include <GLFW/glfw3.h>
This is telling the compiler to include the files glew.h and glfw3.h.
The problem is how is the compiler supposed to know where those files are (it doesn't search your entire Drive for them. c++ compilers have a way for you to tell it where these files are relative to.
In gcc/g++ you use the -I flag and specify where this directory is.
specify the folders where you stored gl\glew.h and glfw\glfw3.hto the gcc using that -I option. If your files are store in C:\Users\John\Projects\dependencies\GLFW\include\glew\glew.h and C:\Users\John\Projects\dependencies\Glew\include\glfw\glfw3.h
then use the the flags like so
gcc -IC:\Users\John\Projects\dependencies\GLFW\include
-IC:\Users\John\Projects\dependencies\Glew\include main.c -o out_file

Related

GLFW WSl2: Display Environment Variable missing

I've installed WSL2 on Windows 10 along with GLFW. When I try to run the default GLFW example program, I run into the following error X11: The DISPLAY environment variable is missing. I build the program using this command-line:
g++ main.cpp -lglfw -lGL -o main
Program:
#include <stdio.h>
#include <iostream>
#include <GLFW/glfw3.h>
static void glfwError(int /*id*/, const char* description)
{
std::cerr << description << '\n';
exit(0);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(&glfwError);
/* 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;
}
How can I fix this issue?

Why do I need to use #define in code blocks but not on visual studio? (GLFW)

I'm starting to learn openGL. I prefer to use codeblocks but the setup for GLFW3 is weird in codeblocks. I copied the example code from the documentation of glfw which was for visual studio. But in code blocks I needed to add an extra line #define GLFW_DLL to make it work?
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
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;
}
so why do I need to add an extra #define line in codeblocks and not in visual studio to make it work?
I followed this instruction
GLFW documentation example code here

opengl initialization problem with glew and glfw

I'm starting to learn the OpenGL stuff, but unfortunately, I can't make the initialization right.
I've added the glfw and glew libraries and these functions give me a strange error,
how can I make it work?
The code:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
std::cout << "GLFW initialization failed.\n";
return -1;
}
if (glewInit()!=GLEW_OK)
{
std::cout << "GLEW initialization failed.\n";
return -1;
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "Wiondow failed.\n";
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;
}
The errors:
To link the GLEW library correctly, proper preprocessor definitions have to be set. See GLEW - Installation:
[...] On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll [...]

c++, Identifier is undefined when part of the code is put in another cpp file

i'm sorry if the question layout is weird or something this is my first time asking a question. I have started learning c++ a week ago and now that i know how to create a window with GLFW I would like to clean up my code and make it nicer. I tried to put the "create window" stuff inside a .cpp file named "window.cpp" and then import it to the main file. but when i take out the GLFWCreateWindow function it doesn't recognize the window name "w_gill" in the swapbuffer, windowshouldclose and destroywindow functions. Can anyone please help me?
Here is the Main.cpp file:
#include <iostream>
#include <Windows.h>
#include "window.cpp"
int main(){
do{
createWindow();
glfwSwapBuffers(w_gill);
glfwPollEvents();
} while (!glfwWindowShouldClose(w_gill));
glfwDestroyWindow(w_gill);
glfwTerminate();
return 0;
}
And the Window.cpp file:
#include <GL\glew.h>
#include <GLFW\glfw3.h>
int windowWidth = 1920 / 2;
int windowHeight = 1080 / 2;
int createWindow(){
if (!glfwInit())
{
glfwTerminate();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow* w_gill;
w_gill = glfwCreateWindow(windowWidth, windowHeight, "Gillow", NULL, NULL);
if (!w_gill)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(w_gill);
glfwSetInputMode(w_gill, GLFW_STICKY_KEYS, GL_TRUE);
}
Nevermind I got it, I had to put
GLFWwindow* w_gill;
int createWindow();
inside of the main.cpp file so they are linked. Thanks for responding anyway.
Your problem has to do with the "scope" or lifetime that a variable has in C. The scope of w_gill is local to its containing function, and as such, there is no visibility to that variable outside of the function. In addition to moving the createWindow() function to its own .CPP file, you must have made some other changes, as even if it was still in main.cpp you would still have the same issue.

crazy flashing window OpenGL GLFW

I have completed the following video
https://www.youtube.com/watch?v=shpdt6hCsT4
however, my hello world window looked like this:
http://s1303.photobucket.com/user/eskimo___/media/screenshot_124_zps890ae561.jpg.html
any ideas where I've gone wrong? Im using osx yosemite with the latest GLFW
cheers
as requested:
my project folder is comprised of 3 files which were made as part of the process using the terminal:
main.cpp(C++ source code)
Makefile(txt)
test(Unix Executable File)
i've set up the glfw3 library on my mac using homebrew.
Main.cpp, which is what is run to produce the undesired effect in the window pictured, is comprised of the example code at GLFW's documentation part of the website:
include <GLFW/glfw3.h>
int main(void)
{
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 */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Insert glClear(GL_COLOR_BUFFER_BIT) prior to glfwSwapBuffers - it's basically updating from uninitialized 'framebuffer' memory, which the GL implementation has probably used for other purposes, like backing store, textures, etc., in the Quartz compositor.
Think of it as like a call to malloc; the memory isn't required to be initialized or zeroed.