gpt4 book ai didi

macos - 在 OSX 上使用 CMake 将项目与 GLFW3 链接时出现 undefined symbol

转载 作者:行者123 更新时间:2023-12-01 22:09:26 25 4
gpt4 key购买 nike

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

$ 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`

最佳答案

我无法向您解释为什么一个链接到 OpenGL 库而另一个不链接,但以下解决方案适用于我在 OSX(Mountain Lion)上遇到的类似问题。

在“添加可执行文件” block 的末尾,添加 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并以这种方式构建它。这可能是保证跨平台兼容性的最简单方法。

关于macos - 在 OSX 上使用 CMake 将项目与 GLFW3 链接时出现 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21879827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com