Mac M1 + CLion + OpenGL配置
一开始运行brew install glew时出现了错误,如图:
查了下资料,需依次运行以下命令(其中ls -al可以不运行):
brew update --verbose
cd /opt/homebrew/Library/Taps/homebrew/homebrew-core
ls -al
git fetch --prune origin
git pull --rebase origin master
brew update
运行结果如图:
接着再运行之前的命令,就可以了:
brew install glew
brew install glfw
打开CLion并创建一个新项目,配置CMakeLists: (版本号要看自己安装的到底是多少)
cmake_minimum_required(VERSION 3.19)
project(opengl)
set(CMAKE_CXX_STANDARD 14)
# Add header file
set(GLEW_H /opt/homebrew/Cellar/glew/2.2.0_1/include/GL)
set(GLFW_H /opt/homebrew/Cellar/glfw/3.3.4/include/GLFW)
include_directories(${GLEW_H} ${GLFW_H})
# Add target link
set(GLEW_LINK /opt/homebrew/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.dylib)
set(GLFW_LINK /opt/homebrew/Cellar/glfw/3.3.4/lib/libglfw.3.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK})
# Execute compilation command
set(SOURCE_FILES main.cpp)
add_executable(opengl main.cpp)
if (APPLE)
target_link_libraries(opengl "-framework OpenGL")
target_link_libraries(opengl "-framework GLUT")
endif()
其中,需要注意的是header file和target的路径。由于之前查资料,看到的教程都是写 /usr/local/Cellar,但自己却没有这条路径,仔细看了一下之前安装glew和glfw时出现的信息,发现是:
/opt/homebrew/Cellar
所以header file和target的路径需修改成:(比如)
/opt/homebrew/Cellar/glew/2.2.0_1/include/GL
对应的版本号也要根据自己安装后的版本进行修改 CMakeLists完成后,会发现多了两个新目录,GL和GLFW: 最后在main.cpp里:
#include <iostream>
#include <glew.h>
#include <glfw3.h>
using namespace std;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if(key==GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
std::cout<<"ESC"<<mode;
}
int main()
{
if(!glfwInit())
return -1;
GLFWwindow* window = glfwCreateWindow(640, 480, "hello world", NULL, NULL);
if(!window)
{
glfwTerminate();
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2, 0.3, 0.3, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0, 1, 1);
glColor3f(0, 1, 0);
glVertex3f(-1, -1, 0);
glColor3f(0, 0, 1);
glVertex3f(1, -1, 0);
glEnd();
glBegin(GL_POLYGON);
glColor3f(0.5, 0.5, 0.5);
glVertex2d(0.5, 0.5);
glVertex2d(1, 1);
glVertex2d(1, 0);
glVertex2d(0.5, 0);
glEnd();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
运行后看到以下结果即配置成功:
|