1. 安装opengl开发环境
brew install glew https://github.com/glfw/glfw.git mkdir build cd build cmake .. make make install
2. 安装cmake插件
3. 测试代码 test.cpp
#define GLEW_STATIC #include#include #include #pragma comment( lib, "GLEW.dylib" ) void Render(void) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); { glColor3f(1.0,0.0,0.0); glVertex2f(0, .5); glColor3f(0.0,1.0,0.0); glVertex2f(-.5,-.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(.5, -.5); } glEnd(); } int main(int argc, const char * argv[]) { GLFWwindow* win; if(!glfwInit()){ return -1; } win = glfwCreateWindow(640, 480, "OpenGL Base Project", NULL, NULL); if(!win) { glfwTerminate(); exit(EXIT_FAILURE); } // if(!glewInit()) // { // return -1; // } glfwMakeContextCurrent(win); while(!glfwWindowShouldClose(win)){ Render(); glfwSwapBuffers(win); glfwPollEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.9) project(glprj) include_directories(/usr/local/include) find_package(OpenGL REQUIRED) find_package(glfw3 REQUIRED) set( CMAKE_CXX_FLAGS "-std=c++11" ) link_directories(/usr/local/lib) add_executable(glprj test.cpp) target_link_libraries(glprj glfw) target_link_libraries(glprj OpenGL::GL)
编译可能遇到的问题
Error: Undefined symbols for architecture x86_64: "_glBegin", referenced from: Render() in test.cpp.o
Solution: find_package(OpenGL REQUIRED) find_package(glfw3 REQUIRED) set( CMAKE_CXX_FLAGS "-std=c++11") target_link_libraries(glprj glfw) target_link_libraries(glprj OpenGL::GL) Error: Undefined symbols for architecture x86_64: "_glewInit", referenced from: _main in test.cpp.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: *** [glprj] Error 1 make[1]: *** [CMakeFiles/glprj.dir/all] Error 2 make: *** [all] Error 2
Solution:
Detete glewInit function.
cmake --build ./build --config Debug --target all -- -j 4
Error: could not load cache
Solution:
step 1: cmake ..
step 2: cmake --build . --config Debug --target all -- -j 4
4. tasks.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "cmake build active file", "type": "shell", "options": { "cwd": "${workspaceFolder}/build" }, "command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ..", //"args":[".."], //"args":["--build . --config Debug --target all -- -j 4"], //"args":["-G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."], //"args":[".. -DCMAKE_BUILD_TYPE=Debug"], "group": { "kind": "build", "isDefault": true } }, { "label": "Make", "type": "shell", "options": { "cwd": "${workspaceFolder}/build" }, "command": "make", "group": { "kind": "build", "isDefault": true } } ] }
5. c_cpp_properties.json
{ "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "/usr/local/include" ], "defines": [], "macFrameworkPath": [ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/bin/g++", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
6. launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(lldb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/glprj", "args": [], //"stopAtEntry": false, "cwd": "${workspaceFolder}/build/", "environment": [], "externalConsole": false, "MIMode": "lldb", // "setupCommands": [ // { // "description": "Enable pretty-printing for gdb", // "text": "-enable-pretty-printing", // "ignoreFailures": true // } // ], // "preLaunchTask": "Make" } ] }
7. 编译生成可执行文件
command + shift + B 选择 cmake build active file, 生成Makefile,
command + shift + B 选择 Make, 生成可执行文件。
8. 运行调试
插入断点进行调试
调试时有断点无效的情况,可去掉
"preLaunchTask": "Make"
或者确保生成的是可调试的可执行文件。
F5得到程序运行结果