如何在CLion中配置OpenGL库以实现IDE内编译运行程序?
在CLion中配置OpenGL项目以直接编译运行
Hey there! I've dealt with this exact issue before—getting CLion to work smoothly with your custom OpenGL Makefile (instead of forcing you to switch to CMake) is totally achievable. Let's walk through the steps to set this up:
1. Open your existing OpenGL project in CLion
First, just fire up CLion and open the root directory of your OpenGL project—make sure your working Makefile is in this root folder, since that's what we'll be pointing CLion to.
2. Configure CLion to use your custom Makefile
CLion defaults to CMake, but we can override that to use your existing build script:
- Open the Settings window: go to
File > Settings(or pressCtrl+Alt+Son Windows/Linux,Cmd+,on macOS) - Navigate to
Build, Execution, Deployment > Build Tools > Makefile - Check the "Use native build" box—this tells CLion to rely on your Makefile instead of generating its own CMake config
- In the "Makefile path" field, browse to select your project's root
Makefile - Set the "Build target" to whatever default target you use in your Makefile (usually
all, but if you have a specific executable target, you can use that too) - Hit "Apply" and "OK" to save these settings
3. Set up a Run/Debug configuration
Next, we need to create a configuration so CLion knows how to run your compiled OpenGL program:
- Click the dropdown menu in the top-right corner of CLion (it probably says "Add Configuration..." initially) and select "Edit Configurations..."
- Click the
+button in the top-left corner and choose "Makefile" from the list - In the new configuration window:
- Set the "Target" to your build target (same as step 2, e.g.,
all) - Make sure the "Working directory" is set to your project's root folder—this is critical if your program loads shaders, textures, or other assets, since it'll look for them relative to this directory
- If you want to debug your program, ensure your Makefile includes the
-gflag in compile commands (e.g.,gcc -g -o my_opengl_app main.c -lGL -lGLU -lglut), then check the "Debug" option in this configuration
- Set the "Target" to your build target (same as step 2, e.g.,
- Click "Apply" and "OK"
4. Test build and run
You're ready to go now:
- Click the hammer icon (or press
Ctrl+F9) to build your project—CLion will run your Makefile exactly as it does in the terminal - Once the build finishes without errors, click the green play button (or press
Shift+F10) to run your OpenGL program directly in CLion's integrated terminal - If you need to run Makefile targets like
cleanorinstall, you can open the Make tool window (go toView > Tool Windows > Make) and click those targets directly
Quick tips to avoid headaches
- If your Makefile uses custom library paths (e.g.,
-L/path/to/opengl/libs), you don't need to duplicate those in CLion's settings—since we're using your native Makefile, CLion will follow all the paths and flags you've already defined - If you run into "library not found" errors, double-check that your Makefile includes all necessary linker flags for OpenGL (common ones are
-lGL,-lGLU,-lglut,-lGLEW, or-glfw3depending on what you're using) - For debugging, set breakpoints in your code as usual—CLion will attach the debugger to your program as long as you compiled with
-g
内容的提问来源于stack exchange,提问作者Burak Kaan Bilgehan
相关产品推荐
相关产品推荐

