编译Stroustrup GUI库遇Polygon歧义与未定义引用问题求助
编译《C++编程原理与实践》配套GUI库遇到的核心问题
已成功安装FLTK库,单独引用#include<FL/Fl_Window.H>可正常编译,但编译Bjarne Stroustrup配套GUI库时遇到以下问题:
- 使用测试代码编译时,
Polygon标识符出现歧义,冲突来自Graph_lib::Polygon与wingdi.h中的Polygon函数 - 注释
using namespace Graph_lib后,Polygon歧义问题依然存在 - 注释
Polygon相关代码后,出现Simple_window等符号未定义的链接错误
测试代码
#include "Simple_window.h" // 引入窗口库 #include "Graph.h" // 引入图形库工具 int main() { using namespace Graph_lib; // 图形工具位于Graph_lib命名空间 Point tl {100,100}; // 窗口左上角坐标 Simple_window win {tl,600,400,"Canvas"}; // 创建窗口 Polygon poly; // 创建多边形形状 poly.add(Point{300,200}); // 添加顶点 poly.add(Point{350,100}); // 添加顶点 poly.add(Point{400,200}); // 添加顶点 poly.set_color(Color::red); // 设置多边形颜色 win.attach (poly); // 将多边形绑定到窗口 win.wait_for_button(); // 等待用户点击按钮 }
编译含Polygon代码时的错误信息
C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp: In function 'int main()': C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp:8:5: error: reference to 'Polygon' is ambiguous 8 | Polygon poly; // make a shape (a polygon) | ^~~~~~~ In file included from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\GUI.h:11, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Simple_window.h:2, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp:1: C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Graph.h:248:8: note: candidates are: 'struct Graph_lib::Polygon' 248 | struct Polygon : Closed_polyline { // closed sequence of non-intersecting lines | ^~~~~~~ In file included from C:/msys64/mingw64/include/windows.h:71, from C:/msys64/mingw64/include/FL/Fl_Window.H:26, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Window.h:5, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\GUI.h:10: C:/msys64/mingw64/include/wingdi.h:3467:28: note: 'WINBOOL Polygon(HDC, const POINT*, int)' 3467 | WINGDIAPI WINBOOL WINAPI Polygon(HDC hdc,CONST POINT *apt,int cpt); | ^~~~~~~ C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp:9:5: error: 'poly' was not declared in this scope 9 | poly.add(Point{300,200}); // add a point
注释using namespace Graph_lib后的错误信息
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp -lfltk -o C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.exe C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp: In function 'int main()': C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp:8:5: error: reference to 'Polygon' is ambiguous 8 | Polygon poly; // make a shape (a polygon) | ^~~~~~~ In file included from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\GUI.h:11, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Simple_window.h:2, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp:1: C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Graph.h:248:8: note: candidates are: 'struct Graph_lib::Polygon' 248 | struct Polygon : Closed_polyline { // closed sequence of non-intersecting lines | ^~~~~~~ In file included from C:/msys64/mingw64/include/windows.h:71, from C:/msys64/mingw64/include/FL/Fl_Window.H:26, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Window.h:5, from C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\GUI.h:10: C:/msys64/mingw64/include/wingdi.h:3467:28: note: 'WINBOOL Polygon(HDC, const POINT*, int)' 3467 | WINGDIAPI WINBOOL WINAPI Polygon(HDC hdc,CONST POINT *apt,int cpt); | ^~~~~~~ C:\Users\ASUS\Desktop\C++\GUI_Train\Libs\Test_output.cpp:9:5: error: 'poly' was not declared in this scope 9 | poly.add(Point{300,200}); // add a point
注释Polygon后的链接错误信息
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ASUS\AppData\Local\Temp\ccgW0oJL.o: in function `main': C:\Users\ASUS\Desktop\C++\GUI_Train\Libs/Test_output.cpp:7: undefined reference to `Simple_window::Simple_window(Graph_lib::Point, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ASUS\Desktop\C++\GUI_Train\Libs/Test_output.cpp:14: undefined reference to `Simple_window::wait_for_button()' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ASUS\AppData\Local\Temp\ccgW0oJL.o:Test_output.cp:(.rdata$_ZTV13Simple_window[_ZTV13Simple_window]+0x20): undefined reference to `Graph_lib::Window::draw()' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ASUS\AppData\Local\Temp\ccgW0oJL.o:Test_output.cp:(.rdata$.refptr._ZTVN9Graph_lib6ButtonE[.refptr._ZTVN9Graph_lib6ButtonE]+0x0): undefined reference to `vtable for Graph_lib::Button' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ASUS\AppData\Local\Temp\ccgW0oJL.o:Test_output.cp:(.rdata$.refptr._ZTVN9Graph_lib6WindowE[.refptr._ZTVN9Graph_lib6WindowE]+0x0): undefined reference to `vtable for Graph_lib::Window'
编译任务配置(VS Code tasks.json)
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-lfltk", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }
问题解决方法
1. 消除Polygon标识符歧义
方案一:显式指定命名空间
直接在代码中用Graph_lib::Polygon代替Polygon,强制指定使用GUI库中的类:
Graph_lib::Polygon poly; // 显式指定命名空间,避免和Windows API的Polygon冲突
方案二:从根源屏蔽Windows API污染
在GUI库的Window.h头文件中,包含FLTK头文件前添加宏定义,禁止Windows GDI函数的声明:
#define NOGDI // 阻止wingdi.h暴露Polygon等GDI函数 #include <FL/Fl_Window.H>
2. 解决链接错误(符号未定义)
当前编译任务仅链接了FLTK库,但未编译并链接配套GUI库的源文件。需要:
- 从仓库中取出
Graph.cpp、GUI.cpp、Window.cpp、Simple_window.cpp等配套源文件,放在测试代码同目录或指定路径 - 修改编译任务,将这些源文件加入编译列表,同时按需添加FLTK依赖库(如
fltk_images)
修改后的tasks.json示例:
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "Graph.cpp", "GUI.cpp", "Window.cpp", "Simple_window.cpp", "-lfltk", "-lfltk_images", // 部分环境需要此库 "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }
内容的提问来源于stack exchange,提问作者Beka Tchigladze
相关产品推荐
相关产品推荐

