Search code examples
macosopenglglfwglew

Linking GLEW and GLFW3 on mac on command line


So basically, i'm on mac.

I installed glew and glfw3 on command line via homebrew.

Now these libraries with the .h files are inside /usr/local/include and i can include them without error (intellisense even finds methods and stuff).

The thing is, unlike stdio.h string.h iostream ecc. including them is apparently not enough.

As soon as i build the program, this message appears on the console:

Undefined symbols for architecture x86_64:
  "_glewInit", referenced from:
      _main in es-97eafd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I don't know what to do, do you know how to properly use these libraries?


Solution

  • These are the steps you need to do to make glew and glfw3 work.

    I assume you have already installed these with brew as such:

    brew install --universal glew
    brew install --universal glfw3
    

    Next when you compile your code, you need to include the headers with -I flag and link to the above libraries with the -l flag as such:

    -I/usr/local/include  -lGLEW -lglfw
    

    A sample makefile should look something like one shown below, assuming we are developing an OpenGL app in main.c with these libraries.

    BIN = hellot
    CC = clang++
    FLAGS = -Wall -pedantic -mmacosx-version-min=10.9 -arch x86_64 -fmessage-length=0 -UGLFW_CDECL -fprofile-arcs -ftest-coverage
    INC = -I/usr/local/include
    LOC_LIB = -lGLEW -lglfw
    FRAMEWORKS = -framework Cocoa -framework OpenGL -framework IOKit
    SRC = main.c
    
    all:
        ${CC} ${FLAGS} ${FRAMEWORKS} -o ${BIN} ${SRC} ${INC} ${LOC_LIB}
    

    I have included a sample project on Github as well which demonstrates the above by rendering a 3D triangle, the hello world of graphics. See the same Makefile listed above here.