由于体系结构的未定义符号 x86_64,GLFW 的最小示例失败
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
我看到类似的问题,例如:
但他们的解决方案并没有真正帮助。
更新: 根据@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 显然安装在标准位置,即您的编译器已经知道在哪里可以找到它。
编辑:我能够重现 linker 错误并将变量名称更改为 GLFW3_LIBRARY
修复了它。
澄清:将您的 link 命令更改为:target_link_libraries(viewer ${GLFW3_LIBRARY})
更新Mac OS:如果您遇到与 OP 相同的问题,可能是因为 glfw3Config.cmake
不 导出我的回答中提到的变量。相反,它创建了一个导入的库 glfw
。在这种情况下,从 link 到 glfw
的正确方法是简单地执行此操作:target_link_libraries(<target> glfw)
.
如果 glfw
是使用 brew
安装的,您应该在 /usr/local/Cellar/glfw/<version>/lib/cmake/glfw3
.
下找到 .cmake
文件
我正在尝试为 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
我看到类似的问题,例如:
但他们的解决方案并没有真正帮助。
更新: 根据@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 显然安装在标准位置,即您的编译器已经知道在哪里可以找到它。
编辑:我能够重现 linker 错误并将变量名称更改为 GLFW3_LIBRARY
修复了它。
澄清:将您的 link 命令更改为:target_link_libraries(viewer ${GLFW3_LIBRARY})
更新Mac OS:如果您遇到与 OP 相同的问题,可能是因为 glfw3Config.cmake
不 导出我的回答中提到的变量。相反,它创建了一个导入的库 glfw
。在这种情况下,从 link 到 glfw
的正确方法是简单地执行此操作:target_link_libraries(<target> glfw)
.
如果 glfw
是使用 brew
安装的,您应该在 /usr/local/Cellar/glfw/<version>/lib/cmake/glfw3
.
.cmake
文件