7

Summary

Compiler MinGW OS Windows 7 x64

glViewport() function gives Undefined Reference error

I have tried

  1. adding -lGL resulted cannot find -lGL <-- How do I locate OpenGL dll files ?
  2. checked -Lpath -lglew32 -lglfw3 as if not last linker argument

I may have miscompiled GLEW and/or miscopied GLEW libs as I am not sure if I need libglew32.a and/or libglew32.dll.a files.

I have a folder structure like that

  • OpenGL
    • bins
      • glew32.dll
      • glfw3.dll
    • includes
      • GL
        • glew.h
        • glewx.h
        • wglew.h
      • GLFW
        • glfw3.h
        • glfw3native.h
    • libs
      • glfw3.lib

Long Story

Hello experienced programmers. Your humble questioner returns. Today me and my friend decided to start/learn OpenGL. As we follow this tutorial, we have stuck at glViewport as it gives Undefined reference error. We are working on NetBeans 8.0 C/C++ version. I have double checked the Makefile as some sites mentioned -Lpath -lglew32 -lglfw3 had to be last parameter when compiling. I have tried to ad -lGL as linker option, but sadly it won't work.


Code

#include <cstdlib>
//GLEW
#include "GL/glew.h"
//GLFW
#include "GLFW/glfw3.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    glewExperimental = GL_TRUE;
    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(800, 600, "LearnOpenGL", NULL , NULL);
    glfwMakeContextCurrent(window);
    glViewport(0, 0, 800, 600);

    return 0;
}

As a side note this is my first time using libraries other than standards. Well Summary looking much longer than Long Story..

Edit: I do not think this is a duplicate question as I'm asking linking of a specific library, not how can I link something in C++. This is like saying all coding questions are same as all of them involves writing code. Checked the said topic and cannot find any direction about linking opengl32 library.

Yakup Türkan
  • 576
  • 2
  • 6
  • 21
  • Can you get the verbose output from the 'make' command? If compiling via command line, adding VERBOSE=1 to the command will do it – Xiao Jul 20 '14 at 06:05
  • Unfortunately I am currently using NetBeans generated makefile. But now I am trying to find a way to use VERBOSE=1 in NetBeans makefile. – Yakup Türkan Jul 20 '14 at 06:12
  • Wait, what compiler are you using? I've been assuming GCC on linux but I just noticed you're using .dll's – Xiao Jul 20 '14 at 06:14
  • I am using MinGW as compiler. – Yakup Türkan Jul 20 '14 at 06:16
  • Ah good. Well adding a `-Wl,--verbose` to the command options should give more information then. That passes `--verbose` to the linker – Xiao Jul 20 '14 at 06:17
  • It gave me a really huge [output](http://pastebin.com/fnxY6W0z) – Yakup Türkan Jul 20 '14 at 06:22
  • Hmm, nothing obvious to me. Have you downloaded the guy's sample source and tried compiling that? It doesn't have the glViewport call, strangely enough – Xiao Jul 20 '14 at 06:41
  • I havent noticed that. Even without glViewport() there are errors. - -`D:\OpenGL\OpenGLTutorial/sample.cpp:21: undefined reference to glClearColor@16'` -`D:\OpenGL\OpenGLTutorial/sample.cpp:22: undefined reference to glClear@4'` – Yakup Türkan Jul 20 '14 at 06:48

1 Answers1

10

In Windows the OpenGL API interface library is called opengl32 not GL, hence you must link with

-lopengl32

(note that it's always …32, even on 64 bit systems).

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • If adding this does not fix the issue, keep it (you need it either way) and also build `glad.c`. It has these stuff defined. – am.rez Dec 30 '22 at 15:58
  • 1
    @am.rez: `glad.c` is just one of many different OpenGL loader libraries. I'd strongly recommend against mindlessly pulling in and adding 3rd party sources, without actually understanding, what's happening under the hood. – datenwolf Dec 30 '22 at 20:15
  • totally agree. I was using `glad.c` and forgot to add its object in the link process. So I came to help future viewers, but did not notice the question did not mention _glad_. – am.rez Dec 30 '22 at 20:38