C++ GLFW的最小示例失败,因为架构x86_64的符号未定义

C++ GLFW的最小示例失败,因为架构x86_64的符号未定义,c++,macos,opengl,cmake,clion,C++,Macos,Opengl,Cmake,Clion,我试图为GLFW编译以下最小示例: #include <GLFW/glfw3.h> #include <thread> int main() { glfwInit(); std::this_thread::sleep_for(std::chrono::seconds(1)); glfwTerminate(); return 0; } 如果我尝试编译代码,它将失败,并显示以下错误消息: [ 50%] Linking CXX executa

我试图为
GLFW
编译以下最小示例:

#include <GLFW/glfw3.h>
#include <thread>

int main() {
    glfwInit();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    glfwTerminate();
    return 0;
}
如果我尝试编译代码,它将失败,并显示以下错误消息:

[ 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
我看到了类似的问题,例如:

但他们的解决方案并没有真正起到作用

UPD:在@thomas\u 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})
我还确保在我的构建目录中没有
cmakcache.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_库
修复了它

澄清:将链接命令更改为:
目标链接库(查看器${GLFW3\u库})

在Mac OS上更新:如果您遇到与OP相同的问题,可能是因为
glfw3Config.cmake
没有导出我回答中提到的变量。相反,它创建了一个导入的库,
glfw
。在这种情况下,链接到
glfw
的正确方法就是这样做:
target\u link\u libraries(glfw)


如果使用
brew
安装了
glfw
,您应该可以在以下位置找到
.cmake
-文件:
/usr/local/ceral/glfw//lib/cmake/glfw3

@desa-Updated。我想你只是忘了遵从变量
target\u link\u libraries(viewer${GLFW3\u LIBRARY})
target\u link\u libraries(viewer GLFW3\u LIBRARY)
相反@desa至于您的另一个问题,为什么
target\u link\u libraries(viewer glfw)
有效:这与将
-lglfw
添加到链接器标志相同。由于在本例中,
glfw
安装在标准位置,因此链接器知道在何处查找它。更多信息:首先,尝试删除
cmakcache.txt
(位于构建目录中),然后再次构建。第二,你能告诉我命令
消息(“GLFW LIB:${GLFW3_LIBRARY}”)
的输出吗?@desa我更新了我的答案,似乎在使用MacOS时根本没有设置这些变量。我是在Linux下测试的,但我只是用Mac电脑进行了验证。我很抱歉假设在所有基于Unix的平台上都是相同的。非常感谢您把这一点讲清楚!你的回答真的很有帮助!
# - 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