使用OSX上的CMake将项目与GLFW3链接时未定义的符号

时间:2014-02-19 11:59:18

标签: macos cmake glfw

关于如何使用GLFW3和OSake 10.9.1上的CMake构建项目,我正在关注this guide,我遇到了一些麻烦。当我构建实际项目时,我得到以下错误:

$ 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

我的CMakeLists.txt看起来像:

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)

然而,使用

可以很好地构建项目
cc `pkg-config --cflags glfw3` -o graphics graphics.cpp \
    `pkg-config --static --libs glfw3`

3 个答案:

答案 0 :(得分:3)

我无法向您解释为什么一个链接与OpenGL库和另一个没有链接,但以下解决方案对我有效,OSX(Mountain Lion)上存在类似问题。

在“添加可执行文件”的末尾。阻止,添加Apple特定条件以链接到OpenGL框架:

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

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

我认为GLFW CMake配置中必须有一些东西负责与OpenGL库链接,所以我查看了GLFW 3.0.4 CMakeLists.txt,发现' -framework'线。

这只会帮助您使用OSX - 您需要提供其他平台特定的链接方式,以使其跨平台。

您可能已经考虑过了,但同一指南也提供了a way to include the GLFW source with your app并以此方式构建。这可能是保证跨平台兼容性的最简单方法。

答案 1 :(得分:0)

Brett Hale的评论是正确的。

有问题的未定义引用是不推荐使用的函数,因此不在OpenGL核心配置文件中。

最佳解决方案是删除对已弃用函数的引用,并仅使用核心配置文件中的函数替换它们。

答案 2 :(得分:0)

我必须这样做。

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")

请注意,我使用" glfw3"而不是target_link_library中的$ {GLFW_STATIC_LIBRARIES}。否则对我不起作用。

我正在使用cmake 3.6.1。