gpt4 book ai didi

c++ - 如何使用 cmake 在 OSX 上编译 glfw

转载 作者:行者123 更新时间:2023-11-30 03:45:13 31 4
gpt4 key购买 nike

我正在使用 jetbrains 的 CLion IDE 来编译一个小型的 hello world 应用程序。

这是我在尝试编译时得到的命令和错误的完整日志:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/brian/Library/Caches/CLion12/cmake/generated/ac340afb/ac340afb/Debug --target Dunjun -- -j 4
[ 50%] Building CXX object CMakeFiles/Dunjun.dir/src/main.cpp.o
clang: warning: -framework Cocoa: 'linker' input unused
clang: warning: -framework OpenGL: 'linker' input unused
clang: warning: -framework IOKit: 'linker' input unused
[100%] Linking CXX executable Dunjun
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", 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]: *** [Dunjun] Error 1
make[2]: *** [CMakeFiles/Dunjun.dir/all] Error 2
make[1]: *** [CMakeFiles/Dunjun.dir/rule] Error 2
make: *** [Dunjun] Error 2

我意识到要防止这些错误,我必须向编译器添加标志。其他 stackoverflow 问题将此引用为正确的命令:

gcc -Iglfw3/include/ -Lglfw3/lib/ -lglfw3 -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo main.c

我如何通过 Cmake 执行此操作?我怎样才能让它在 CLion 下工作。

下面是我的 CmakeLists.txt 供引用:

cmake_minimum_required(VERSION 3.3)
project(Dunjun)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -framework Cocoa -framework OpenGL -framework IOKit")
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}" )

set(SOURCE_FILES
bin/Debug/Dunjun_game
include/Dunjun/Common.hpp
src/main.cpp
CMakeLists.txt
README.md)

add_executable(Dunjun ${SOURCE_FILES})
include_directories(include)

最佳答案

-framework 标志添加到链接器标志,而不是编译标志。

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")

关于c++ - 如何使用 cmake 在 OSX 上编译 glfw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979805/

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