Mac Xcode下OpenGL的GLFW、GLAD配置
最近在学OpenGL,看了一篇不错的教程:https://learnopengl-cn.github.io/01%20Getting%20started/02%20Creating%20a%20window/#glad 但是里面没有关于Mac 配置的介绍,所以花了很长的时间才弄好。写下心得供大家使用。
前提
安装Xcode,并安装command line tool
$ xcode-select --install
具体步骤:
1.我用的是GUI的cmake(图形界面),下载GLFW源码,

2. 在cmake中编译。第一行选择你下载的glfw文件夹路径,第二行是你自己在glfw文件夹下新建的build文件夹路径,编译后的东西都会在这个build文件夹下。点击configure

如果你在点击“done”后有这样的问题:
The C compiler identification is unknown CMake Error at CMakeLists.txt:3 (project): No CMAKE_C_COMPILER could be found.
Configuring incomplete, errors occurred! See also "/Users/standard/Desktop/glfw-3.2.1/build/CMakeFiles/CMakeOutput.log".
那么打开终端,输入命令:
sudo xcode-select --reset
就可以解决。
再点击一次configure,然后点击generate,编译的内容就在build路径下了。

3. 进入你在第二步新建的build文件夹,会有一个GLFW.xcodeproj,打开。然后直接点Run,编译运行成功。此时在src/Debug下会生成一个libglfw3.a 将它拷贝到/usr/local/lib路径
注意:你可能在finder下找不到该路径,所以用快捷键shift+command+G,输入/usr/local即可
同时在/usr/local下新建include文件夹(有了的话就不用了),将build文件夹中的include里的文件复制到你新建的路径中。结果:

(其他文件没有的话不要紧)。
至于glad配置请参考这篇教程http://blog.shenyuanluo.com/OpenGLEnvironment.html之GLAD配置。
4.新建一个Xcode项目,类型是command line tool,语言选C或C++

然后修改header和library搜索路径,上截图:

这里填路径时我看其他教程都说要把recursive选上,我这里选的是non-recursive,因为我选了之后不知道为什么会出问题。然后进行设置:

5.最后,编写main.cpp,源码参考的是这篇教程:https://blog.csdn.net/lm409/article/details/78420044#4main%E6%96%87%E4%BB%B6
注意图六最左侧那个glad.c是在第三步末尾提到的那篇教程中直接拖进来的。
完毕!有问题可以留言。