使用 MinGW 编译 FLTK 库并将其集成到 CMake 项目中

Compiling FLTK Library with MinGW and Integrating it into a CMake Project

提问人:Mudern 提问时间:9/24/2023 更新时间:9/24/2023 访问量:56

问:

在使用 MinGW 编译 FLTK 库进行项目开发时,在生成或定位 MinGW 编译的 FLTK 库时,我遇到了以下三种方法:

  1. 使用 FLTK 的 CMake 文件生成 CodeBlocks MinGW 解决方案,并使用 CodeBlocks 编译所有库文件。此方法生成了 和 ,但由于缺少库文件(适用于 Linux),因此在中途遇到错误。fltk.libfltk_forms.lib<unistd.h>
  2. 使用 CLion 打开 FLTK 的 CMake 并进行编译会导致错误,生成 、 和 。错误消息为:。libfltk.alibfltk_formslibfltk_gl.afatal error: libpng/png.h: No such file or directory
  3. 将 vcpkg 与命令结合使用时,成功生成了 MinGW 编译的 FLTK 库,但中途遇到错误,并显示以下消息:.--triplet=x64-mingw-dynamicerror: building zlib:x64-mingw-dynamic failed with: BUILD_FAILED

以前,我使用 Visual Studio 中的 MSVC 编译器成功编译了 FLTK 库,没有任何错误。我正确地配置了新项目的头文件和库文件,并成功运行了 FLTK 的示例程序。

项目结构如下:

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

CMake 文件如下:

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)

示例程序:

cpp
#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);
    (FL_SHADOW_LABEL);
    window->end();
    window->show(argc, argv);
    return Fl::run();
}

您收到的错误消息:

makefile
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
C++ Windows CMake mingw FLTK

评论

1赞 HolyBlackCat 9/24/2023
也许使用 MSYS2?他们已经为 MinGW 预构建了 Fltk。
1赞 Mudern 9/24/2023
“谢谢你的建议。我使用 MSYS2 的 MinGW 安装了 FLTK,并将我的 CLion 编译器设置为 MinGW。在 CMake 中,我使用了 find_package(FLTK REQUIRED),它成功运行。

答: 暂无答案