2021-07-29 配置小记 Mac M1 + CLion + OpenGL

Mac M1 + CLion + OpenGL配置

一开始运行brew install glew时出现了错误,如图:
brew install error

查了下资料,需依次运行以下命令(其中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

运行结果如图:
command result

接着再运行之前的命令,就可以了:

brew install glew
brew install glfw

install result

打开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:
new directories
最后在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 you press ESC and set windowShouldClose to True, the outer loop will close the application
    if(key==GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
    std::cout<<"ESC"<<mode;
}

int main()
{
    if(!glfwInit())
        return -1;
    //Create window and context
    GLFWwindow* window = glfwCreateWindow(640, 480, "hello world", NULL, NULL);
    if(!window)
    {
        //Create failure will return NULL
        glfwTerminate();
    }
    glfwMakeContextCurrent(window);

    glfwSetKeyCallback(window, key_callback); //Register callback function
    while(!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        glClearColor(0.2, 0.3, 0.3, 1);
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
        glColor3f(1, 0, 0); //Red
        glVertex3f(0, 1, 1);

        glColor3f(0, 1, 0); //Green
        glVertex3f(-1, -1, 0);

        glColor3f(0, 0, 1); //Blue
        glVertex3f(1, -1, 0);
        //End a drawing step
        glEnd();

        glBegin(GL_POLYGON);
        //Draw another trapezoid, you need to pay attention to the stroke order
        glColor3f(0.5, 0.5, 0.5); //Grey
        glVertex2d(0.5, 0.5);
        glVertex2d(1, 1);
        glVertex2d(1, 0);
        glVertex2d(0.5, 0);
        glEnd();


        /******Exchange buffer, update the content on the window******/
        glfwSwapBuffers(window);
    }
    glfwTerminate();
    return 0;
}

运行后看到以下结果即配置成功:
running

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
ESP32是一款32位的微控制器,而ESP-IDF是官方提供的用于开发ESP32的软件开发框架。Clion是一款集成开发环境(IDE),可以用于开发和调试ESP32项目。 在使用Clion进行ESP32开发之前,需要先搭建好ESP-IDF和Clion的开发环境。以下是搭建ESP32+Clion开发环境的步骤: 1. 安装ESP-IDF:根据官方文档的指引,下载并安装ESP-IDF。确保安装的是与你的ESP32兼容的版本。 2. 安装Clion:下载并安装Clion,确保安装的是最新版本。 3. 配置ESP-IDF路径:打开Clion,进入File -> Settings -> Build, Execution, Deployment -> CMake,将ESP-IDF的路径添加到"CMake Options"中。例如,如果ESP-IDF安装在`/path/to/esp-idf`,则添加以下内容: ``` -DCMAKE_TOOLCHAIN_FILE=/path/to/esp-idf/tools/cmake/toolchain-esp32.cmake ``` 4. 创建ESP32项目:在Clion中创建一个新的CMake项目,并将ESP-IDF的示例项目导入到Clion中。可以通过以下命令将示例项目复制到你的项目目录中: ``` cp -r /path/to/esp-idf/examples/get-started/hello_world /path/to/your/project ``` 5. 配置CMakeLists.txt:打开项目中的CMakeLists.txt文件,并根据你的项目需求进行配置。确保设置了正确的目标硬件和端口。 6. 构建和烧录:在Clion中点击Build按钮,Clion将自动构建项目并生成可执行文件。然后,使用ESP-IDF提供的烧录工具将可执行文件烧录到ESP32上。 7. 调试:在Clion配置调试器,可以使用GDB进行调试。在调试过程中,可以设置断点、查看变量的值等。 请注意,以上步骤仅为搭建ESP32+Clion开发环境的基本步骤,具体的配置和操作可能会因个人需求和环境而有所不同。建议参考ESP-IDF和Clion的官方文档以获取更详细的指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值