You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MinGW编译FLTK库并集成CMake项目遇问题求助

MinGW编译FLTK库及项目链接问题求助

编译FLTK库的三种尝试及问题

  • 通过FLTK的CMake生成CodeBlocks MinGW解决方案编译:生成了fltk.lib和fltk_forms.lib,但中途因缺失Linux下的<unistd.h>文件报错。
  • 使用CLion打开FLTK的CMake编译:生成了libfltk.a、libfltk_forms.a和libfltk_gl.a,但报错fatal error: libpng/png.h: No such file or directory。
  • 使用vcpkg指定--triplet=x64-mingw-dynamic命令:虽成功生成MinGW编译版FLTK库,但中途出现error: building zlib:x64-mingw-dynamic failed with: BUILD_FAILED错误。

此前使用Visual Studio的MSVC编译器成功编译FLTK库,且在新项目中正确配置头文件和库文件后能正常运行FLTK示例程序。

项目结构

fltk-learn
│
├── CMakeLists.txt
├── main.cpp
└── fltk_x64-windows
    ├── include
    ├── lib
    ├── share
    └── tools

CMake配置文件内容

cmake_minimum_required(VERSION 3.25)
project(fltk_learn)

set(CMAKE_CXX_STANDARD 17)

# Declare header file path
set(INC_DIR ./fltk_x64-windows/include)

# Declare library file path
set(LINK_DIR ./fltk_x64-windows/lib)

# Include header files
include_directories(${INC_DIR})

# Include library files
link_directories(${LINK_DIR})

add_executable(fltk_learn main.cpp)

# Link third-party libraries
target_link_libraries(fltk_learn fltk.lib)

示例程序代码

#include <iostream>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>

int main (int argc, char *argv[]) {
    Fl_Window *window;
    Fl_Box *box;
    window = new Fl_Window(300, 180);
    window->label("HelloWorld!");
    box = new Fl_Box(20, 40, 260, 100, "Hello World!");
    box->box(FL_UP_BOX);
    box->labelsize(36);
    box->labelfont(FL_BOLD + FL_ITALIC);
    box->labeltype(FL_SHADOW_LABEL); // 原代码此处存在语法错误,已修正
    window->end();
    window->show(argc, argv);
    return Fl::run();
}

注:原示例代码中(FL_SHADOW_LABEL);为无效语句,会导致编译错误,已修正为调用labeltype方法设置标签样式。

当前链接错误信息

C:\Program Files\JetBrains\CLion 2023.1\bin\cmake\win\x64\bin\cmake.exe" --build C:\Code\C++\Clion\fltk_learn\cmake-build-debug --target fltk_learn -- -j 14
[ 50%] Linking CXX executable fltk_learn.exe
C:\MinGW\bin/ld.exe: CMakeFiles\fltk_learn.dir/objects.a(main.cpp.obj): in function `main':
C:/Code/C++/Clion/fltk_learn/main.cpp:9: undefined reference to `Fl_Window::Fl_Window(int, int, char const*)'
C:\MinGW\bin/ld.exe: C:/Code/C++/Clion/fltk_learn/main.cpp:10: undefined reference to `Fl_Window::label(char const*)'
C:\MinGW\bin/ld.exe: C:/Code/C++/Clion/fltk_learn/main.cpp:11: undefined reference to `Fl_Box::Fl_Box(int, int, int, int, char const*)'
C:\MinGW\bin/ld.exe: C:/Code/C++/Clion/fltk_learn/main.cpp:15: undefined reference to `fl_define_FL_SHADOW_LABEL()'
C:\MinGW\bin/ld.exe: C:/Code/C++/Clion/fltk_learn/main.cpp:16: undefined reference to `Fl_Group::end()'
C:\MinGW\bin/ld.exe: C:/Code/C++/Clion/fltk_learn/main.cpp:17: undefined reference to `Fl_Window::show(int, char**)'
C:\MinGW\bin/ld.exe: C:/Code/C++/Clion/fltk_learn/main.cpp:18: undefined reference to `Fl::run()'
collect2.exe: error: ld returned 1 exit status
make[3]: *** [CMakeFiles\fltk_learn.dir\build.make:99: fltk_learn.exe] Error 1
make[2]: *** [CMakeFiles\Makefile2:82: CMakeFiles/fltk_learn.dir/all] Error 2
make[1]: *** [CMakeFiles\Makefile2:89: CMakeFiles/fltk_learn.dir/rule] Error 2
make: *** [Makefile:123: fltk_learn] Error 2

需求

现需解决以下问题:

  1. 修复三种MinGW编译FLTK库方式中的报错,成功编译出可用的MinGW版FLTK库;
  2. 解决当前项目的链接错误,使示例程序能正常编译运行。

内容的提问来源于stack exchange,提问作者Mudern

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 11:52:49