MacOS OpenGL环境配置-使用VSCode

MacOS OpenGL环境配置-使用VSCode

MacOS OpenGL环境配置-使用VSCode

环境:MacOS 11.0

需要安装的内容:

  • CMake
  • Glad
  • Glfw
  • Homebrew
  • Glm

打开Terminal(VSCode当中直接打开Terminal)

1.安装Homebrew

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

2.安装glfw/glew/glm

1
2
3
brew install glfw3
brew install glew
brew install glm

2.1 下载glad,并将glad.h放到usr/local/include目录下,glad.c放到待会要构建的项目根目录下

http://glad.dav1d.de/

3.安装CMake

1
brew install CMake

4.VSCode安装CMake插件(可选)

创建前先检查一下:

/usr/local/include 和 /usr/local/Cellar 下都有glad、glfw、glm文件夹及其内容

/usr/local/lib下存在libglfw.3.dylib

创建一个项目:

(假设助教已经提供了base文件)

在项目根目录下存在glad.c

根目录下创建CMakeLists.txt,保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
cmake_minimum_required(VERSION 3.19)
project (projName)#项目名

set(GLAD_H ${PROJECT_SOURCE_DIR}/include)
set(GLFW_H /usr/local/include)
set(dependencies /base)
#/base是助教给的基类文件夹(如果还没给可以先做修改)
include_directories(${GLAD_H} ${GLFW_H} ${dependencies})
set(CMAKE_CXX_STANDARD 17)
set(GLFW_LINK /usr/local/lib/libglfw.3.dylib)

link_libraries(${GLFW_LINK})

# 执行编译命令 testc
set(baseSOURCES glad.c base/shader.cpp base/application.cpp)
#助教给的基类cpp文件 还有glad.c
set(SOURCES star.cpp main.cpp render_flag.cpp flag.cpp )
#这里填写你要的编译的文件
add_executable(projName) ${baseSOURCES} ${SOURCES})
#项目名
# 链接系统的 OpenGL 框架
if (APPLE)
target_link_libraries(projName "-framework OpenGL")
endif()

set(GLFW_LINK /usr/local/lib/libglfw.3.dylib)
link_libraries(${GLFW_LINK})


include(CTest)
enable_testing()

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

项目文件夹目录应该像是这个样子:

1
2
3
4
5
6
/base
助教提供的基类文件
/build
CMake生成的文件
CMakeLists.txt
其他编译的文件

image-20210317091209034

编译文件:

terminal内输入

1
2
3
4
mkdir build
cd build
cmake ..
make

如果出现linker error 可能是头文件include出现问题,检查文件夹

编译成功后输入./(项目名称),例如本案例中输入./projName即可打开

可能遇到的问题:

1.brew安装特别慢:替换国内源

1
2
3
4
5
6
7
8
9
10
# 替换brew.git
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 替换homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 刷新源
brew update

2.仍然缺少头文件?

确保 usr/local/Cellar文件夹下有GLFW/GLM/GLEW文件夹

同时 usr/local/include有.h文件

如果还有问题,检查一下CMakeLists内容是否正确