gl3w - Static Linking Fails

Viewed 12

I'm trying to build gl3w from source and link it automatically in my C++ project using CMake. I've added gl3w as a git submodule and have the following in CMakeLists.txt:

# GL3W
set(gl3w_dir "${CMAKE_CURRENT_SOURCE_DIR}/gl3w")
set(gl3w_inc "${gl3w_dir}/include")
set(gl3w_src "${gl3w_dir}/src/gl3w.c")
if (EXISTS ${gl3w_src})
    message(STATUS "Found gl3w source files")
else()
    execute_process(COMMAND python3 gl3w_gen.py WORKING_DIRECTORY "${gl3w_dir}") # use python or python3
    if (NOT EXISTS ${gl3w_src})
        message(FATAL_ERROR "Could not generate gl3w source files")
    endif()
endif()
add_library(gl3w STATIC "${gl3w_src}")
target_include_directories(gl3w PUBLIC "${gl3w_inc}")
target_link_libraries(main gl3w)

However, this doesn't seem to be working. I get the following error upon building:

[build] Undefined symbols for architecture x86_64:
[build]   "_gl3wProcs", referenced from:
[build]       _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64

If I instead link gl3w dynamically rather than statically in CMakeLists.txt, I somehow don't get this error - but running the compiled executable results in a segfault.

1 Answers

It turns out that I wasn't calling gl3wInit(); in main, causing my code to segfault whenever it came across calls to OpenGL functions. Now static linking works just fine!

Related