Use a library and header files in Gnome Builder

993 Views Asked by At

UPDATE I switched to the meson build system. Everything is working fine now!

I am very new to using C++, OpenGl, and Gnome Builder. I have a very very basic foundation with C++ and I know how to link header files and libraries in CodeLite, however after messing around Gnome Builder I want to make the switch. I haven't found any beginner friendly tutorials on using Builder. I am just lost as to how I should link external libraries in Builder. Do I just manually edit the Makefile or is there a setting somewhere that will automate the makefile process with automake? Am I wrong in assuming that this is a makefile problem? Apologies if this is a very novice question.

I am using Ubuntu. I am getting the error "undefined reference to ..." for all the glfw and glew variables and headers. After installing libraries with apt, I have my libraries installed in usr/lib/x86-64-linux-gnu, headers in usr/include.

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>


int main ()
{
  glewExperimental = true;
  if (!glfwInit() )
  {
    fprintf(stderr, "Failed to initialize GLFW \n");
    return -1;
  }
  glfwWindowHint(GLFW_SAMPLES, 4);
  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);

  GLFWwindow* window;
  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. \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);

  do {
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
    glfwPollEvents();
  }
  while ( glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);

 return 0;
}

When trying to build I get this error output~

g++ -o practice -Wall -ggdb -fno-omit-frame-pointer -O2 practice.cpp /usr/bin/ld: /tmp/ccLx11Ky.o: in function main': /home/joe/Projects/practice/practice.cpp:30: undefined reference toglewExperimental' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:31: undefined reference to glfwInit' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:36: undefined reference toglfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:37: undefined reference to glfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:38: undefined reference toglfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:39: undefined reference to glfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:40: undefined reference toglfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:43: undefined reference to glfwCreateWindow' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:50: undefined reference toglfwMakeContextCurrent' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:51: undefined reference to glewExperimental' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:52: undefined reference toglewInit' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:58: undefined reference to glfwSetInputMode' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwWindowShouldClose' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:61: undefined reference to glClear' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:62: undefined reference toglfwSwapBuffers' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:63: undefined reference to glfwPollEvents' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwGetKey' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:47: undefined reference to `glfwTerminate' collect2: error: ld returned 1 exit status make: *** [Makefile:8: practice] Error 1

My default Makefile looks as follows~

all: practice

WARNINGS = -Wall
DEBUG = -ggdb -fno-omit-frame-pointer
OPTIMIZE = -O2

practice: Makefile practice.cpp
    $(CXX) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) practice.cpp

clean:
    rm -f practice

# Builder will call this to install the application before running.
install:
    echo "Installing is not supported"

# Builder uses this target to run your application.
run:
    ./practice
1

There are 1 best solutions below

1
On

You need to customize the build command (practice) to include the required libraries. Conside using pkg-config --libs glew (or pkg-config --libs -static glew) to find which libraries are needed, and pkg-config --cflags for command line flags, if any.

Most likely:

  • Add '-lglfw3' to the 'practice' build command,
  • If you use static libs, add '-DGLEW_STATIC'

See glfw Errors with glfwWindowHint, and GLEW Linker Errors (undefined reference to `__glewBindVertexArray')