GLFW的最小示例由于架构X86_64的未定义符号而失败
Minimal example with GLFW fails because of undefined symbols for architecture x86_64
我正在尝试编译GLFW
的以下最小示例:
#include <GLFW/glfw3.h>
#include <thread>
int main() {
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
return 0;
}
我的CMakeLists.txt
文件是:
cmake_minimum_required(VERSION 3.9)
project(viewer)
set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
add_executable(viewer main.cpp)
target_link_libraries(viewer ${GLFW_LIBRARIES})
如果我尝试编译代码,则会失败,以下错误消息:
[ 50%] Linking CXX executable visualiser
Undefined symbols for architecture x86_64:
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.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[3]: *** [visualiser] Error 1
make[2]: *** [CMakeFiles/visualiser.dir/all] Error 2
make[1]: *** [CMakeFiles/visualiser.dir/rule] Error 2
make: *** [visualiser] Error 2
我看到了类似的问题,例如:
- 麻烦编译GLFW,未定义的符号
- http://dudandan.com/2017/02/15/setup-glfw-and-glew/
但是他们的解决方案并没有真正帮助。
upd:按照@thomas_f的评论,我修改了我的 CMakeLists.txt
文件如下:
cmake_minimum_required(VERSION 3.9)
project(viewer)
set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW3_INCLUDE_DIR})
add_executable(viewer main.cpp)
target_link_libraries(viewer ${GLFW3_LIBRARY})
message(GLFW LIB: ${GLFW3_LIBRARY})
我还确保我的构建目录中没有CMakeCache.txt
:
$ ls -a
. .. .idea CMakeLists.txt cmake-build-debug main.cpp
但是,我仍然收到相同的错误消息。看来,
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" ........./viewer
GLFWLIB:
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/denis/Documents/projects/theia/code/visualiser/cmake-build-debug
[Finished]
所以看来${GLFW3_LIBRARY}
未定义。
来自 glfw3Config.cmake
:
# - Config file for the glfw3 package
# It defines the following variables
# GLFW3_INCLUDE_DIR, the path where GLFW headers are located
# GLFW3_LIBRARY_DIR, folder in which the GLFW library is located
# GLFW3_LIBRARY, library to link against to use GLFW
看来您是在引用错误的变量。另外,在这种情况下,无需调用include_directories()
,因为GLFW显然安装在标准位置,即您的编译器已经知道在哪里可以找到它。
编辑:我能够重现链接器错误并将变量名称更改为GLFW3_LIBRARY
修复了它。
澄清:将您的链接命令更改为:target_link_libraries(viewer ${GLFW3_LIBRARY})
更新如果您在Mac OS 上:如果您遇到的问题与OP相同,则可能是因为glfw3Config.cmake
dim not 导出我中提到的变量回答。相反,它创建了导入的库glfw
。在这种情况下,链接到glfw
的正确方法就是简单地执行此操作:target_link_libraries(<target> glfw)
。
如果使用brew
安装了glfw
,则应找到.cmake
-FILES下:/usr/local/Cellar/glfw/<version>/lib/cmake/glfw3
。
相关文章:
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何在 Linux 中构建共享库时使未定义引用的链接器失败
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- Android opencv 3.4.1 Build 命令失败.未定义的引用
- 针对 QNX 交叉编译后,boost 文件系统 (1.63) 失败并出现未定义的引用错误
- Gtest 在catkin_make run_tests时失败.未定义的引用
- GLFW的最小示例由于架构X86_64的未定义符号而失败
- LLVM-5.0 MakeFile未定义的参考失败
- 提升 Python 导入失败,未定义包装类的符号
- Linux Flex gcc 编译失败,并显示"未定义对 'TclReError'的引用"
- 在 RPI 上编译 ROS 在 opencv 时失败,未定义对'header'的引用
- 体系结构x86_64的未定义符号,链接器命令失败
- 体系结构x86_64的未定义符号,链接器命令失败,退出代码为 1
- Xcode:为什么重命名为 .mm 失败,仅在静态库中"___gxx_personality_sj0"未定义的符号?
- 链接c++失败[未定义引用boost::filesystem3 ..]].
- 由于未定义的引用,AOSP构建失败
- c++链接失败,未定义对libboost_thread的引用
- 使用libvpx x86 android构建项目失败,出现未定义的引用错误
- libavcodec.so的星号模块app_transcoder失败(未定义的符号avacodec_init)