I've been trying to set up a build environment for OpenGL using glfw3 and GLAD. I'm currently using WSL2 Ubuntu with an X Server for compilation and a makefile.
However, when I run my make I receive the following error:
src/glad.c:25:10: fatal error: glad/glad.h: No such file or directory
25 | #include <glad/glad.h>
This is odd to me because it seems that the makefile is able to compile the main.cpp file and create a main.o despite also including "glad/glad.h"
File structure:
-HelloTriangle
--include
---glad
----glad.h
---KHR
----khrplatform.h
--src
---glad.c
---main.cpp
--makefile
This is my make file:
BASE_OBJS = main.o glad.o
SRC_PATH = src
OBJS = $(addprefix $(SRC_PATH)/, $(BASE_OBJS))
CXX = g++
CXXFLAGS = -g -Iinclude
LDFLAGS =
LDLIBS = -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl
HelloTriangle: $(OBJS)
$(CXX) -o $# $(LDFLAGS) $^ $(LDLIBS)
clean:
rm $(OBJS)
This is my main.cpp:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
/*
Function to handle window resizing
*/
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main() {
/*
Initialize GLFW
Sets version to Core profile 3.3
*/
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/*
Initialize a window context for OpenGL
Defines the windows width, height, and title
*/
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Triangle", NULL, NULL);
if(window == NULL) {
std::cout << "Failed to create GLFW window" <<std::endl;
glfwTerminate();
return -1;
}
/*
Initialize GLAD
Handles OS-specific function pointers
*/
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
/*
Handle window resizing
*/
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
/*
Render loop
*/
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
You seem to set the CXXFLAGS (for the C++ compiler), but your glad.c is compiled with the C-compiler (which checks CFLAGS)
Related
I am attempting to set up SDL2 for creating a C++ game for windows. When I run the below make file with just a single source file, I get no errors, and the program can run. When I attempt to run the make file with multiple source files, the compilation goes smoothly, but when I attempt to run the program, i get the error seen in the title. It appears to only happen when I attempt to create an instance of a class in the second source file, and try to access either members or instance functions.
I have tried redownloading and setting up my environment, updating mingw (didnt hurt to try) and triple checking my code. I have included the makefile, as well as the source files I am using. Thank you for any help.
EDIT: It seems that the delete call in the main function causes this error... Not sure if that is a coincidence or if the cause does lie somewhere there...
#OBJS specifies which files to compile as part of the project
OBJS = src/*.cpp src/*.hpp
#CC specifies which compiler we're using
CC = g++
#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Iinclude/SDL2
#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -Llib
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS =
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = demoGame
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
#include <SDL.h>
#include <stdio.h>
#include "grid.hpp"
#include <iostream>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
}
}
Grid* grid = new Grid(1, 2);
// These two lines seem to cause an issue.
std::cout << grid->getRows() << std::endl;
delete grid;
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
class Grid {
public:
Grid(int rows, int columns) {
this->rows = rows;
this->columns = columns;
}
int getRows() {
return this->rows;
}
private:
int rows;
int columns;
};
I've been struggling with getting SDL and SDL_ttf to work together. My minimal reproducible case (down below) works in VC++ but for the life of me I can't get it to work in a MinGW environment.
SDL version: 2.0.10 (latest at moment of writing)
SDL_ttf version: 2.0.15 (latest at moment of writing)
My set up looks like this:
Copied SDL and SDL_ttf lib and include folders to my project root.
Set up my link and include in my Makefile like so:
SRC_DIR=./app/src
INC_DIR=./app/inc
OBJ_DIR=./.obj
BUILD_DIR=./build
NAME=$(BUILD_DIR)/out
all: $(NAME)
CC=gcc
CXX=g++
CXXFLAGS=-Wall -Wextra -pedantic --std=c++17 -g
# Objects to build
STNAMES=\
main \
OBJECTS=$(patsubst %,$(OBJ_DIR)/%.o, $(STNAMES))
# Windows specific settings
ifeq ($(OS), Windows_NT)
LINK=\
-L./SDL2/lib \
-L./SDL2_ttf/lib \
-lmingw32 \
-lSDL2main \
-lSDL2 \
-lSDL2_ttf \
-lstdc++ \
INCLUDE=\
-I$(INC_DIR) \
-I./SDL2/include \
-I./SDL2_ttf/include \
define mkdir =
if not exist "$(1)" mkdir "$(1)"
endef
define rmdir =
if exist "$(1)" del /S/Q "$(1)"
endef
EXECUTABLE=$(NAME).exe
# macOS specific settings
# UNTESTED, assumes dependencies are installed with brew
else
LINK=$(shell sdl2-config --libs) -lc++
INCLUDE=\
-I$(shell brew --prefix)/include/SDL2 \
define mkdir
mkdir -p $(1)
endef
define rmdir =
rm -rf $(1)
endef
EXECUTABLE=$(NAME)
endif
$(NAME): $(OBJECTS)
#$(call mkdir,$(BUILD_DIR))
$(CC) -o $# $(OBJECTS) $(LINK)
#$(EXECUTABLE)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
#$(call mkdir,$(OBJ_DIR))
#$(CXX) -o $# -c $< $(CXXFLAGS) $(INCLUDE)
re:
#$(call rmdir,$(OBJ_DIR))
make
Added the dlls to MinGW's bin folder.
Here's a minimal reproducible test case which works for me on Visual Studio, but not on MinGW:
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
void drawText(SDL_Renderer* renderer, TTF_Font* font, int x, int y, const char* text) {
auto surface = TTF_RenderText_Solid(font, text, SDL_Color{ 255, 255, 255, 255 });
auto texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dstRect = { x, y, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, NULL, &dstRect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("some title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1920, 1080, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
TTF_Init();
bool running = true;
SDL_Event event;
auto font = TTF_OpenFont("assets/courier_prime.ttf", 36);
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
running = false;
}
SDL_SetRenderDrawColor(renderer, 25, 25, 25, 255);
SDL_RenderClear(renderer);
drawText(renderer, font, 10, 10, "Test string");
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255 );
auto rect = SDL_Rect{10, 100, 110, 200};
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}
Regardless of where I place that SDL_RenderFillRect (before or after the drawText), the only thing I see is the text. The Rectangle is no longer visible.
I'm currently dealing with a mixed OS dev environment (mac and windows), hence why I'd like it to build on both using make.
Seems like it was an issue with SDL_RENDERER_ACCELERATED and openGL not being linked. Linking openGL did not fix the issue, however, swapping to the software renderer (SDL_RENDERER_SOFTWARE) did fix the rendering bug. Very strange that it does work on VS though. I guess that'll remain a mystery...
I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a
I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.
The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.
Please see these screenshots: http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD
C++.cpp
#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"
int main(int argv, char** args){
display x(800,600,"something");
while(!x.isClosed()){
glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
x.Update();
}
return 0;
}
display.cpp
#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){
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_DOUBLEBUFFER,1);
m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);
m_glContext = SDL_GL_CreateContext(m_window);
GLenum status = glewInit(); //NO ERRORS OCCUR
if(status != GLEW_OK){
std::cerr << "glew failed to initialize" << std::endl;
}
m_isClosed = false;
}
display::~display(){
SDL_GL_DeleteContext(m_glContext);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
bool display::isClosed(){
return m_isClosed;
}
void display::Update(){
SDL_GL_SwapWindow(m_window);
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
m_isClosed = true;
}
}
}
display.h
#ifndef DISPLAY_H_
#define DISPLAY_H_
#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/
class display{
public:
display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~display();
private:
display(const display& other){}
display& operator=(const display& other){}
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};
#endif /* DISPLAY_H_ */
To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).
Create OpenGL context and window
A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).
Initilize SDL
If you are using SDL you have to create the window and you have to create the OpenGL context.
SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
SDL_GLContext glContext = SDL_GL_CreateContext( window );
Note, you should check for errors with SDL_GetError.
The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.
SDL_GL_MakeCurrent( window, glContext );
Initilize GLUT
To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.
glutInit(&argc, argv);
glutCreateWindow("OGL window");
Initilize GLFW
Note, glfwInit returns GLFW_TRUE if succeded:
if ( glfwInit() != GLFW_TRUE )
return;
GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
return;
}
glfwMakeContextCurrent( wnd );
After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.
Set up GLEW
Note that glewInit returns GLEW_OK if succeded:
if ( glewInit() != GLEW_OK )
return;
To link the GLEW library correctly you have to set up proper preprocessor definitions:
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
See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')
So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
and finnally:
gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)
Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test
Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.
I am working on an GLFW desktop application that uses openGL ES 2.0 instead of the normal openGL!
The code that i wrote compiles great but when i run the application i hear some weird sound coming from my laptop and after a few seconds the window of the application goes unresponsive when i close the window the sound stops!
Is it a hardware/software problem or i did something wrong?
this my main.cpp:
#ifndef GLFW_INCLUDE_ES2
#define GLFW_INCLUDE_ES2
#endif
#include "game.h"
#include <GLFW/glfw3.h>
int init_gl();
void shutdown_gl();
void set_main_loop();
GLFWwindow* window;
int main()
{
if (init_gl() == GL_TRUE) {
on_surface_created();
on_surface_changed();
set_main_loop();
}
shutdown_gl();
return 0;
}
void shutdown_gl()
{
glfwDestroyWindow(window);
glfwTerminate();
}
int init_gl()
{
const int width = 480,
height = 800;
if (glfwInit() != GL_TRUE) {
return GL_FALSE;
}
window = glfwCreateWindow(width, height, "Simple example", NULL, NULL);
if (!window) {
return GL_FALSE;
}
glfwMakeContextCurrent(window);
return GL_TRUE;
}
void set_main_loop()
{
while (!glfwWindowShouldClose(window))
{
on_draw_frame();
glfwSwapBuffers(window);
}
}
tell me if u need the code from game.cpp!
The code is compiled on Ubuntu 14.04 using g++ with the commands:
g++ -I. -I../common -c main.cpp ../common/game.cpp
g++ main.o game.o -o main.exec -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread
You need to call glfwPollEvents() in your loop, however I'm not 100% sure as the sound could be caused by the application not being vsync-d.
Reference:
http://www.glfw.org/docs/latest/quick.html
I am trying to get the GLFW3 working on my workstation. I did install GLFW3 on my Ubuntu 12.04 from http://www.glfw.org/download.html
Here is the program I am trying. Got it from GLFW official site
http://www.glfw.org/documentation.html
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
{
std::cout<< "xxx\n";
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
std::cout<< "yyy\n";
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;
}
Followed this stackoverflow question How to build & install GLFW 3 and use it in a Linux project
compiled it as -
g++ -I/usr/local/include glfw.cpp -L/usr/local/lib -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lrt
It could successfully compile. However when I launch the executable, I it prints
yyy
Which means the window was not created. What could have been wrong, how can I fix it?