Code::Blocks中GTK+2编译报错:undefined reference to `gdk_cairo_create`
Hey there, let's get this linking issue sorted out—you're already halfway there since you noticed the missing libraries in the working project! Here's how to fix it step by step:
1. Add the Missing Link Libraries
First, we need to make sure your problematic project includes the libraries that the working one has:
- Right-click your project in Code::Blocks and select Build Options
- Switch to the Linker settings tab
- Under Link libraries, click the Add button
- Browse to
C:\Program Files (x86)\CodeBlocks\gtk\liband selectgdk-win32-2.0.lib - Repeat to add
pango-1.0.lib
- Browse to
- Alternatively, you can add these directly to Other linker options with
-lgdk-win32-2.0and-lpango-1.0(just make sure they're in the right order—more on that next!)
2. Adjust Linker Order (Critical for MinGW!)
MinGW's linker is picky about library order—libraries that depend on other libraries need to come BEFORE the libraries they depend on. For GTK+2 projects, the correct order usually looks like this:
-lgtk-win32-2.0 -lgdk-win32-2.0 -lpangocairo-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lcairo
To adjust this in Code::Blocks:
- In the Link libraries list, drag and drop the entries to match the order above
- If you're using Other linker options, just retype the flags in that exact sequence
3. Verify the Fix
Rebuild your project and check the build log again—you should see the full set of libraries in the correct order in the mingw32-g++.exe link command. If the undefined reference error is gone, you're good to go!
Quick Why This Happened
After your system reinstall, your project's linker configuration probably didn't carry over the full set of GTK+2 dependencies. Since gdk_cairo_create() is part of the GDK library, linking against gdk-win32-2.0.lib is non-negotiable, and the order ensures the linker finds all the required symbols properly.
内容的提问来源于stack exchange,提问作者Eric Wajnberg

