Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get GLFW to link

Tags:

c++

clang

glfw

I am trying to compile the below test program:

#include <GL/glfw.h>

int main(int argc, char** argv) {
    if(!glfwInit()) {
        return -1;
    }

    if(!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW)) {
        return -1;
    }

    while(glfwGetWindowParam(GLFW_OPENED)) {
        glfwSwapBuffers();
    }

    return 0;
}

but I always get undefined reference errors in regards to the GLFW functions.

Below is my makefile:

CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDFLAGS = -lglfw

OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include

SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello

all: init $(OBJECTS) $(EXECUTABLE)

$(EXECUTABLE):
    $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS)

$(OBJ_DIR)/%.o: %.cpp
    $(CXX) $(INC_DIR) -c $< -o $@

init:
    @mkdir -p "$(OBJ_DIR)"

clean:
    @rm -rf $(OBJ_DIR) $(EXECUTABLE)

I definitely have glfw.h and libglfw.a/.so as when I run locate glfw I get:

:~$ locate glfw
/usr/include/GL/glfw.h
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/libglfw.so.2
/usr/lib/libglfw.so.2.6

The output of nm /usr/lib/libglfw.a | grep glfwInit:

:~$ nm /usr/lib/libglfw.a | grep glfwInit
                 U _glfwInitialized
                 U _glfwInitialized
                 U _glfwInitialized
                 U _glfwInitialized
0000000000000000 B _glfwInitialized
0000000000000000 T glfwInit
                 U _glfwInitialized
                 U _glfwInitialized
                 U _glfwInitialized
                 U _glfwInitialized
                 U _glfwInitialized
                 U _glfwInitJoysticks
                 U _glfwInitTimer
00000000000000c0 T _glfwInitJoysticks
0000000000000000 T _glfwInitTimer

and the verbose message from clang:

clang++ -I/usr/include -c main.cpp -o bin/main.o
clang++ -Wall -std=c++0x -Wl --verbose -lglfw -lGL -lGLU -L/usr/lib -o hello bin/main.o
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang: warning: argument unused during compilation: '-std=c++0x'
 "/usr/bin/ld" -z relro --hash-style=gnu --as-needed --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. -L/lib/x86_64-linux-gnu -L/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib -lglfw -lGL -lGLU bin/main.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
bin/main.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `glfwInit'
main.cpp:(.text+0x76): undefined reference to `glfwOpenWindow'
main.cpp:(.text+0x97): undefined reference to `glfwGetWindowParam'
main.cpp:(.text+0xa7): undefined reference to `glfwSwapBuffers'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1

It seems to not be finding the library?

like image 840
Lerp Avatar asked May 12 '13 02:05

Lerp


People also ask

Can I use GLFW with C++?

3.1 - What compilers are supported by GLFW? Currently, GLFW releases are tested with MinGW, MinGW-w64 and Visual C++ 2010, 2012, 2013 and 2015, but it should work with any compiler that supports C99 (C89 on Windows). Very old development environments may require updated system headers.

Does GLFW include OpenGL?

GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan development on the desktop. It provides a simple API for creating windows, contexts and surfaces, receiving input and events. GLFW is written in C and supports Windows, macOS, X11 and Wayland.

How do I add GLFW to Xcode?

With Xcode on macOSIf you are using the dynamic library version of GLFW, add it to the project dependencies. If you are using the static library version of GLFW, add it and the Cocoa, OpenGL and IOKit frameworks to the project as dependencies. They can all be found in /System/Library/Frameworks .


1 Answers

The problem is that the glfw libraries are being specified to the linker before the object file that depends on them. ld searches libraries to resolve dependencies only for the dependencies that it knows about at the point in list of files it's processing. So when ld is searching libglfw.a it doesn't know about the glfwInit dependency in main.o yet. ld (by default) doesn't go back an search the library again.

Try:

$(EXECUTABLE):
    $(CXX) $(CXXFLAGS) $(LIB_DIR) -o $@ $(OBJECTS)  $(LDFLAGS)

Also the libraries should probably be specified in a LDLIBS (or LIBS) variable - LDFLAGS is conventionally use for linker options:

CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDLIBS = -lglfw -lGL -lGLU

OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include

SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello

all: init $(OBJECTS) $(EXECUTABLE)

$(EXECUTABLE):
    $(CXX) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDLIBS)

$(OBJ_DIR)/%.o: %.cpp
    $(CXX) $(INC_DIR) -c $< -o $@

init:
    @mkdir -p "$(OBJ_DIR)"

clean:
    @rm -rf $(OBJ_DIR) $(EXECUTABLE)
like image 56
Michael Burr Avatar answered Nov 10 '22 00:11

Michael Burr