OpenGL C++ undefined reference to symbol 'glViewport' [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Linking GLEW with CMake
(6 answers)
Closed last month.
When I try to compile it, I get a strange error:
[domon#archlinux TileVox-engine]$ g++ -c engine_main.cpp
[domon#archlinux TileVox-engine]$ g++ engine_main.o -o game -L -lGL -lglfw -lGLEW -lglut -lGLU
/usr/bin/ld: engine_main.o: undefined reference to symbol 'glViewport'
/usr/bin/ld: /usr/lib/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Code:
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
int window_width = 1280;
int window_height = 720;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(window_width, window_height, "TileVox-engine: Main scene", nullptr, nullptr);
if (window == nullptr){
cout << "TileVox-engine Error: failed to create OpenGL profile";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK){
cout << "OpenGL Error: starting failed";
}
glViewport(0,0, window_width, window_height);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
But there are no errors here, and I compiled and added the libraries correctly, what could be the problem?

Related

Unable to compile OpenGL program in Ubuntu, due to GLAD errors

I installed GLWF succesfully. I can prove that since this program compiles:
#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 */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Then i used these commands in order to install glad:
git clone https://github.com/Dav1dde/glad.git
cd glad
cmake ./
make
sudo cp -a include /usr/local/
When i tried to execute this program:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// ------------------------------------
// vertex shader
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(shaderProgram);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
I got these errors:
/usr/bin/ld: /tmp/ccDMX5H7.o: in function `main':
triangle.c:(.text+0xe9): undefined reference to `gladLoadGLLoader'
/usr/bin/ld: triangle.c:(.text+0x12b): undefined reference to `glad_glCreateShader'
/usr/bin/ld: triangle.c:(.text+0x13f): undefined reference to `glad_glShaderSource'
/usr/bin/ld: triangle.c:(.text+0x162): undefined reference to `glad_glCompileShader'
/usr/bin/ld: triangle.c:(.text+0x173): undefined reference to `glad_glGetShaderiv'
/usr/bin/ld: triangle.c:(.text+0x19a): undefined reference to `glad_glGetShaderInfoLog'
/usr/bin/ld: triangle.c:(.text+0x1fd): undefined reference to `glad_glCreateShader'
/usr/bin/ld: triangle.c:(.text+0x211): undefined reference to `glad_glShaderSource'
/usr/bin/ld: triangle.c:(.text+0x234): undefined reference to `glad_glCompileShader'
/usr/bin/ld: triangle.c:(.text+0x245): undefined reference to `glad_glGetShaderiv'
/usr/bin/ld: triangle.c:(.text+0x26c): undefined reference to `glad_glGetShaderInfoLog'
/usr/bin/ld: triangle.c:(.text+0x2cf): undefined reference to `glad_glCreateProgram'
/usr/bin/ld: triangle.c:(.text+0x2de): undefined reference to `glad_glAttachShader'
/usr/bin/ld: triangle.c:(.text+0x2f7): undefined reference to `glad_glAttachShader'
/usr/bin/ld: triangle.c:(.text+0x310): undefined reference to `glad_glLinkProgram'
/usr/bin/ld: triangle.c:(.text+0x321): undefined reference to `glad_glGetProgramiv'
/usr/bin/ld: triangle.c:(.text+0x348): undefined reference to `glad_glGetProgramInfoLog'
/usr/bin/ld: triangle.c:(.text+0x3ab): undefined reference to `glad_glDeleteShader'
/usr/bin/ld: triangle.c:(.text+0x3bc): undefined reference to `glad_glDeleteShader'
/usr/bin/ld: triangle.c:(.text+0x4b9): undefined reference to `glad_glGenVertexArrays'
/usr/bin/ld: triangle.c:(.text+0x4d1): undefined reference to `glad_glGenBuffers'
/usr/bin/ld: triangle.c:(.text+0x4e9): undefined reference to `glad_glGenBuffers'
/usr/bin/ld: triangle.c:(.text+0x501): undefined reference to `glad_glBindVertexArray'
/usr/bin/ld: triangle.c:(.text+0x512): undefined reference to `glad_glBindBuffer'
/usr/bin/ld: triangle.c:(.text+0x528): undefined reference to `glad_glBufferData'
/usr/bin/ld: triangle.c:(.text+0x54b): undefined reference to `glad_glBindBuffer'
/usr/bin/ld: triangle.c:(.text+0x561): undefined reference to `glad_glBufferData'
/usr/bin/ld: triangle.c:(.text+0x584): undefined reference to `glad_glVertexAttribPointer'
/usr/bin/ld: triangle.c:(.text+0x5ad): undefined reference to `glad_glEnableVertexAttribArray'
/usr/bin/ld: triangle.c:(.text+0x5bb): undefined reference to `glad_glBindBuffer'
/usr/bin/ld: triangle.c:(.text+0x5ce): undefined reference to `glad_glBindVertexArray'
/usr/bin/ld: triangle.c:(.text+0x607): undefined reference to `glad_glClearColor'
/usr/bin/ld: triangle.c:(.text+0x630): undefined reference to `glad_glClear'
/usr/bin/ld: triangle.c:(.text+0x63e): undefined reference to `glad_glUseProgram'
/usr/bin/ld: triangle.c:(.text+0x64f): undefined reference to `glad_glBindVertexArray'
/usr/bin/ld: triangle.c:(.text+0x660): undefined reference to `glad_glDrawElements'
/usr/bin/ld: triangle.c:(.text+0x696): undefined reference to `glad_glDeleteVertexArrays'
/usr/bin/ld: triangle.c:(.text+0x6ae): undefined reference to `glad_glDeleteBuffers'
/usr/bin/ld: triangle.c:(.text+0x6c6): undefined reference to `glad_glDeleteBuffers'
/usr/bin/ld: triangle.c:(.text+0x6de): undefined reference to `glad_glDeleteProgram'
/usr/bin/ld: /tmp/ccDMX5H7.o: in function `framebuffer_size_callback(GLFWwindow*, int, int)':
triangle.c:(.text+0x764): undefined reference to `glad_glViewport'
collect2: error: ld returned 1 exit status
I compiled the program with this command:
g++ triangle.c -o triangle -Wall -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor
EDIT:
I used these two resources to perform the installation of the libraries:
https://www.youtube.com/watch?v=ZwaHQ6c-ma0
https://shnoh171.github.io/gpu%20and%20gpu%20programming/2019/08/26/installing-glfw-on-ubuntu.html
Glad consists of multiple files:
A glad.h header file, which you have to include into your files
A glad.c source file which you have to compile together with your own source files.
Judging from your error message, you forgot to also compile the glad.c source file. You can adapt your command line to something like:
g++ triangle.c <GLADPATH>/glad.c -o triangle -Wall -lGL -lGLU -lglut -lGLEW -lglfw -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor
Replace <GLADPATH> with the path to the glad directory
There are two essential components while using gpp compiler, that allow to run OpenGL programs:
LINKER FILE (for example glad.c)
THE FILE THAT HAS TO BE COMPILED APART FROM YOUR MAIN PROGRAM.
The easiest way to handle the linker file is to move it to the folder where your main.cpp is located. Then compile the program in a following way:
g++ main.cpp glad.c -lglfw -lGL -ldl
HEADER FILE (for example glad.h)
A file which is defined in the curly brackets <> of the #include directive. The path that is placed in curly brackets <> needs to be continuation of the directory that is placed in one of the following paths:
$ cpp -v
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/9/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
For example, when #include <glad/glad.h> is specified in your code, glad.h should be located in:
/usr/include/glad/glad.h
Apart from that, you don't need to use the libraries like :
-lGLU -lglut -lGLEW -lX11 -lXxf86vm -lXrandr -lpthread -lXi -lXinerama -lXcursor. Use the minimum amount of "-l's" and once you compile linker file with main file, there's no need to define glad in the form of static library (-lglad).
Thus, the answer is:
g++ triangle.c glad.c -lglfw -lGL -ldl
and you should get orange square on the green background (if you don't forget about header and linker files located in the right folders)

I cant get GLEW to work on netbeans in ubuntu 20.04 (C++)

I managed to get GLFW3 to work and properly display a window, however when i add glewInit() to my project it tells me undefined reference to glewInit'. I am using g++ as my compiler with -lglfw3 and lGl as flags. Below is a simple project to demonstrate my issue. I am on linux. Any help is much appreicated.
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <iostream>
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// here is the error, my glew is not intializing :(
if(glewInit() != GLEW_OK) {
return 0;
}
// render loop
while (!glfwWindowShouldClose(window))
{
// render
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
glfwTerminate();
return 0;
}
There is a standard way on Linux to get a correct set of libraries to link with your program - please look at man pkg-config. In your case you need to link everything, which is needed by both GLFW3 and GLEW - so you should call the pkg-config two times:
hekto#ubuntu:~$ pkg-config --libs glfw3
-lglfw
hekto#ubuntu:~$ pkg-config --libs glew
-lGLEW -lGLU -lGL
Therefore, you need to use -lglfw instead of -lglfw3 and add two more libraries - -lGLEW and -lGLU. These calls to the pkg-config tool can be directly added to your Makefile as well.

Why would the window created with this OpenGL context open as transparent?

Upon execution, the background should be rendered as a darker-blue, but I have clearly missed something. The window is instead rendered with a background that is identical to the image immediately behind it (e.g. other open windows or the desktop, etc.). I cannot identify the problem.
Currently I am not using any -std during compilation and I am utilizing the following linkages with the output executable:
-lGL -lGLU -lGLEW -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXcursor -lXinerama
Here are the contents of my .cpp file:
#include <stdio.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
//Initialize GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failes to intialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a Window and create it's OpenGL context
window = glfwCreateWindow( 1024, 768, "playground", NULL, NULL);
if( window == NULL) {
fprintf( stderr, "Failed to open GLFW window.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
glfwTerminate();
return 0;
}
I am an OpenGL newcomer. Feel free to remark on the over-all structure or if anything should/must be rewritten entirely. Thanks!
You just forgot to clear your window in your render loop:
do{
glClear(GL_COLOR_BUFFER_BIT); // Clear background with clear color.
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
glClear is often one of the first call of every render loop using OpenGL, giving you a new fresh frame to work on.

OpenGL with GLFW and GLEW - compiling with gcc on windows

I'm trying to run an OpenGL program that uses GLFW and GLEW libraries I built myself. The starter code I use is
#include <iostream>
// GLEW
#define GLEW_STATIC
#include <glew.h>
// GLFW
#include <glfw3.h>
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", 0, 0);
glfwMakeContextCurrent(window);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
Then I type
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32
Which yields a number of 'undefined reference to' errors.
The code should be OK, as I previously run it successfully in Visual Studio.
This command:
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32
will not be enough in Windows. You will need to link additional system libraries. For example, every project in Visual Studio 2012 have these attached by default:
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
That is why it compiled fine in VS.
kernel32.lib and user32.lib should be always linked. gdi32.lib is required, when you do any graphic operations.
First solution:
Link these libraries manually:
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 -lkernel32 -luser32 -lgdi32 -lws2_32
If I remember correctly, ws2_32.a is the name of WinSock2 library supplied with MinGW.
Second solution:
If you use MinGW, you can use -mwindows flag:
g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 -mwindows
This will link, among few others, gdi32.a, kernel32.a, user32.a and ws2_32.a.

Compiling error lnk 2019 under visual studio

I do not know what to with this error. I searched for some solutions, but nothing helps.
Linker Error:
"error LNK2019: unresolved external symbol __ imp__glDrawArrays"
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
int main()
{
if(glfwInit()==false){
//Did not succeed
fprintf(stderr, "GLFW faild ");
return -1;
}
glfwWindowHint(GLFW_SAMPLES,4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(640,480,"Hallo Welt",NULL,NULL);
if(!window)
{
fprintf(stderr, "Window failed");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if(glewInit() != GLEW_OK)
{
fprintf(stderr, "glew init failed");
glfwTerminate();
return -1;
}
GLuint vaoID; //Vertex array erzeugt
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID); // wir verwenden das VA
static const GLfloat verts[] = {
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f,
0.0f,1.0f,0.0f};
//Generate VBO
GLuint vboID;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER,vboID);
glBufferData(GL_ARRAY_BUFFER,sizeof(verts),verts,GL_STATIC_DRAW);
do{
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,vboID);
glVertexAttribPointer(0, 3,GL_FLOAT,GL_FALSE,0,(void*)0);
glDrawArrays(GL_TRIANGLES,0,3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
} while(glfwWindowShouldClose(window)==false);
return 0;
}
I set my directories for my include and lib files.
Under Linker->Input i have glew32.lib, glew32s.lib, glfw3dll.lib
If I delete the function glDrawArrays the code works and i get the black window. But I want to draw a white triangle in the window.
You have a couple of problems here:
You are linking to glew32.lib (dynamic --> DLL) and glew32s.lib (static)
Pick only one, and I would suggest glew32s.lib - but make sure you add GLEW_STATIC to pre-processor definitions when you use glew32s.lib.
You are only linking to support libraries, not the actual OpenGL run-time that ships with Windows
Fix this by adding OpenGL32.lib to your libraries.