Undefined Symbols when linking project with GLFW3 using CMake on OSX
Asked Answered
K

3

8

I am following this guide on how to build a project using GLFW3 with CMake on OSX 10.9.1, and I've run into some trouble. When I get to building the actual project I get the following errors:

$ make
Scanning dependencies of target Graphics
[100%] Building CXX object CMakeFiles/Graphics.dir/graphics.cpp.o
Linking CXX executable Graphics
Undefined symbols for architecture x86_64:
  "_glBegin", referenced from:
      _main in graphics.cpp.o
  "_glClear", referenced from:
      _main in graphics.cpp.o
  "_glColor3f", referenced from:
      _main in graphics.cpp.o
  "_glEnd", referenced from:
      _main in graphics.cpp.o
  "_glLoadIdentity", referenced from:
      _main in graphics.cpp.o
  "_glMatrixMode", referenced from:
      _main in graphics.cpp.o
  "_glOrtho", referenced from:
      _main in graphics.cpp.o
  "_glRotatef", referenced from:
      _main in graphics.cpp.o
  "_glVertex3f", referenced from:
      _main in graphics.cpp.o
  "_glViewport", referenced from:
      _main in graphics.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Graphics] Error 1
make[1]: *** [CMakeFiles/Graphics.dir/all] Error 2
make: *** [all] Error 2

My CMakeLists.txt looks like:

cmake_minimum_required (VERSION 2.8)
project (Graphics)

# The version number.
set (Graphics_VERSION_MAJOR 1)
set (Graphics_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/GraphicsConfig.h.in"
  "${PROJECT_BINARY_DIR}/GraphicsConfig.h"
  )

# add the binary tree to the search path for include files
# so that we will find GraphicsConfig.h
include_directories("${PROJECT_BINARY_DIR}")

find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})

# add the executable
add_executable (Graphics graphics.cpp)
target_link_libraries(Graphics ${GLFW_STATIC_LIBRARIES})

# add the install targets
install (TARGETS Graphics DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/GraphicsConfig.h"        
         DESTINATION include)

However can build the project just fine using

cc `pkg-config --cflags glfw3` -o graphics graphics.cpp \
    `pkg-config --static --libs glfw3`
Klina answered 19/2, 2014 at 11:59 Comment(1)
These are not core profile functions. You will have to request a _FORWARD_COMPAT = GL_FALSE and _PROFILE = _COMPAT_PROFILE in context creation. Better yet - upgrade the code to core profile functionality and shaders when ready.Neutralism
P
7

I can't offer you an explanation for why one links with the OpenGL libs and the other doesn't, but the following solution worked for me with a similar problem on OSX (Mountain Lion).

At the end of the 'add executable' block, add an Apple-specific condition to link with the OpenGL framework:

# add the executable
add_executable (Graphics graphics.cpp)
target_link_libraries(Graphics ${GLFW_STATIC_LIBRARIES})

if (APPLE)
  target_link_libraries(Graphics "-framework OpenGL")
endif()

I figured there must be something in the GLFW CMake config that was responsible for linking with the OpenGL libs so I had a look at the GLFW 3.0.4 CMakeLists.txt and found that '-framework' line.

This will only help you on OSX - you'd need to supply other platform specific ways of linking to make it cross platform.

You may have already considered it, but the same guide also provides a way to include the GLFW source with your app and build it that way. That's probably the easiest way to guarantee cross-platform compatibility.

Preservative answered 19/4, 2014 at 20:15 Comment(0)
T
1

I had to do this.

elseif(APPLE)
  # GLFW
  find_package(glfw3 REQUIRED)
  include_directories(${GLFW_INCLUDE_DIRS})

  find_package(GLEW REQUIRED)
  include_directories(${GLEW_INCLUDE_DIRS})

  # app_framework executable
  add_executable(app_framework ${SOURCE_FILES})
  target_link_libraries(app_framework app_framework_library ${GLEW_LIBRARY} glfw3)
  target_link_libraries(app_framework "-framework OpenGL")

Note that I use "glfw3" instead of ${GLFW_STATIC_LIBRARIES} in target_link_library. Did not work for me otherwise.

I'm using cmake 3.6.1.

Tenorite answered 31/8, 2016 at 8:18 Comment(0)
D
0

Add this to your CMakeFile,

add_executable(${LOCAL_PROJECT_NAME} )
#openGL Specific
target_link_libraries(${LOCAL_PROJECT_NAME} ${GLFW_STATIC_LIBRARIES})

find_package(glfw3 3.3 REQUIRED)
target_link_libraries(${LOCAL_PROJECT_NAME} glfw)
find_package(OpenGL REQUIRED)
target_link_libraries(${LOCAL_PROJECT_NAME} OpenGL::GL)

also, make sure compiler options are set accordingly.

Diapause answered 25/10, 2022 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.