1、安装homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2、安装GLEW与GLFW
brew install glew
brew install glfw3
如果已经安装了,可以利用更新为最新版本
brew reinstall glew
brew reinstall glfw3
3、利用CLion法
3.1 在CLion:A cross-platform IDE for C and C++下载CLion
3.2 打开在线服务(点击这个链接),API选择3.3及以上,Profile选择Core,勾选Options中的Generate a loader,点击GENERATE,会生成一个压缩包,下载。同时也要下载src文件夹
3.3 新建一个项目,配置对应的Cmake文件, {} 用当前系统的真实版本替换
例如: set(GLEW_H /usr/local/Cellar/glew/2.2.0_1/include/GL)
cmake_minimum_required(VERSION 3.12)
project(OpenGL)
set(CMAKE_CXX_STANDARD 14)
set(GLEW_H /usr/local/Cellar/glew/{}/include/GL)
set(GLFW_H /usr/local/Cellar/glfw/{}/include/GLFW)
set(GLAD_H /usr/local/include/glad)
set(KH_H /usr/local/include/KHR)
include_directories(${GLEW_H} ${GLFW_H} ${GLAD_H} ${KH_H})
# 添加目标链接
set(GLEW_LINK /usr/local/Cellar/glew/{}/lib/libGLEW{}.dylib)
set(GLFW_LINK /usr/local/Cellar/glfw/{}/lib/libglfw{}.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK})
# 执行编译命令
# 这个glad.c在第2步中说过要拉到项目中,注意路径
set(SOURCE_FILES "src/glad.c" "main.cpp")
add_executable(OpenGL ${SOURCE_FILES})
# mac下这步很重要
if (APPLE)
target_link_libraries(OpenGL "-framework OpenGL")
target_link_libraries(OpenGL "-framework GLUT")
endif()
3.4 注意这样配置之后需要更改`/usr/local/include/glad文件夹下的glad.h`中的`#include
3.5 运行测试代码(把下列代码复制到main.cpp中):
#include
#include
#include
using namespace std;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
//如果按下ESC,把windowShouldClose设置为True,外面的循环会关闭应用
if(key==GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
std::cout<<"ESC"<
出现下图,说明安装成功
参考文章:Mac下配置OpenGL环境