首页 > 解决方案 > 尝试从 Bjarne Stroustrup PPP2 图形章节构建文件时出现链接器错误

问题描述

当我尝试使用 MinGW 在 Visual Studio 代码中构建代码时,出现链接器错误。我目前正在阅读 Bjarne Stroustrup 的“使用 C++ 的原理和实践,第 2 版”中的图形章节,并试图让必要的文件正常工作。我设法让 FLTK 工作,但是当我添加 Stroustrup 的文件时,在尝试构建时出现了两个错误。我目前在基于 Windows 10 x64 的系统上并使用最新版本的 MinGW、FLTK 和 Visual Studio 代码。在过去的几天里,我一直在试图让它发挥作用,正如您所见,我的 tasks.json 有点乱。

错误信息:

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/MinGW/msys/1.0/local/lib/libfltk_images.a(Fl_JPEG_Image.o):Fl_JPEG_Image.cxx:(.text$_ZN13Fl_JPEG_ImageC2EPKcPKh+0x343): undefined reference to `Fl_Shared_Image::Fl_Shared_Image(char const*, Fl_Image*)'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/MinGW/msys/1.0/local/lib/libfltk_images.a(Fl_JPEG_Image.o):Fl_JPEG_Image.cxx (.text$_ZN13Fl_JPEG_ImageC2EPKcPKh+0x34d): undefined reference to `Fl_Shared_Image::add()'

主文件

#include "Simple_window.h"
#include "Graph.h"
int main() {
    Point tl{100,100};

    Simple_window win{tl,600,400,"Canvas"};

    Graph_lib::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();
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/MinGW/msys/1.0/local/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "clang-x86"
        }
    ],
    "version": 4
}

任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "shell: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${fileDirname}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-LC:/MinGW/msys/1.0/local/lib",
                "-IC:/MinGW/msys/1.0/local/include",
                "-mwindows",
                "-lfltk",
                "-lfltk_images",
                "-lfltk_jpeg",
                "-lole32",
                "-luuid",
                "-lcomctl32",
            ],
            "options": {
                "cwd": "C:\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

标签: c++visual-studio-codefltk

解决方案


推荐阅读