Dev C++中readimagefile未定义引用错误解决及背景图实现方案
Hey, I’ve run into this exact issue with the old graphics.h library before—let’s break down what’s going on and how to fix it, plus cover the best ways to add background images in modern C++.
Why You’re Seeing That Error
graphics.h is a relic from the Turbo C era, not part of standard C++. Dev C++ uses MinGW under the hood, which doesn’t include the required library files for readimagefile by default. That’s why you get the "undefined reference" linker error.
Option 1: Fix the graphics.h Setup (If You Want to Stick With It)
If you’re set on using graphics.h for your project, here’s how to get readimagefile working:
- Get the right library files: Download a MinGW-compatible
graphics.hheader andlibbgi.alibrary file. - Place them correctly:
- Drop
graphics.hinto your Dev C++ include folder (e.g.,C:\Dev-Cpp\MinGW64\include) - Put
libbgi.ainto the lib folder (e.g.,C:\Dev-Cpp\MinGW64\lib)
- Drop
- Configure the linker:
- Go to
Tools > Compiler Optionsin Dev C++ - Under the
Linkertab, add these flags to the command line:-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
- Go to
- Working Example Code:
#include <graphics.h> int main() { // Initialize a 800x600 window initwindow(800, 600, "Image Display"); // Load and draw the image (use absolute path if needed, like "C:\\my_bg.jpg") // The last four numbers are target coordinates to fit the window readimagefile("my_bg.jpg", 0, 0, 799, 599); // Wait for user input before closing getch(); closegraph(); return 0; }
And yes—this image will act as a background! Any shapes or text you draw after readimagefile will appear on top of it.
Option 2: Ditch graphics.h for Modern Libraries (Highly Recommended)
graphics.h is outdated and has terrible cross-platform support. For any serious project, use a modern library like SFML or SDL2. Here’s a quick SFML example to display a background image—it’s way more flexible:
SFML Background Image Setup
- Download SFML and configure it in Dev C++ (add include directories, library directories, and link the required SFML libraries).
- Example Code:
#include <SFML/Graphics.hpp> int main() { // Create a 800x600 window sf::RenderWindow window(sf::VideoMode(800, 600), "Background Image Demo"); // Load the image texture sf::Texture bgTexture; if (!bgTexture.loadFromFile("my_bg.jpg")) { // Handle loading failure (e.g., print an error message) return -1; } // Create a sprite from the texture and scale it to fit the window sf::Sprite bgSprite(bgTexture); bgSprite.setScale( static_cast<float>(window.getSize().x) / bgTexture.getSize().x, static_cast<float>(window.getSize().y) / bgTexture.getSize().y ); // Main loop while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } } // Draw background first (so other elements render on top) window.clear(); window.draw(bgSprite); // Add your other drawing code here (text, shapes, sprites, etc.) window.display(); } return 0; }
The Best Way to Add Background Images in C++
For most use cases, these are your top options:
- SFML: Lightweight, easy to learn, perfect for 2D games or simple GUI apps with background needs.
- SDL2: More powerful, great for complex graphics projects with extensive customization.
- Qt: If you’re building a full desktop application, Qt’s
QPainterorQLabelmakes setting backgrounds trivial, plus it comes with a full UI toolkit.
All these libraries support common image formats (JPG, PNG, etc.), work across Windows/Linux/Mac, and are actively maintained—way better than relying on the ancient graphics.h.
内容的提问来源于stack exchange,提问作者Sanket Mathur

