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

131 views Asked by At

When compiling the FLTK library using MinGW for project development, I encountered the following three approaches when generating or locating the MinGW-compiled FLTK library:

  1. Using FLTK's CMake files to generate a CodeBlocks MinGW solution and compiling all library files using CodeBlocks. This approach generated fltk.lib and fltk_forms.lib, but encountered an error halfway through due to the missing library file <unistd.h> (which is for Linux).
  2. Using CLion to open FLTK's CMake and compile resulted in errors, generating libfltk.a, libfltk_forms, and libfltk_gl.a. The error message was: fatal error: libpng/png.h: No such file or directory.
  3. Utilizing vcpkg with the --triplet=x64-mingw-dynamic command successfully generated the MinGW-compiled FLTK library, but encountered an error midway with the message: error: building zlib:x64-mingw-dynamic failed with: BUILD_FAILED.

Previously, I successfully compiled the FLTK library using the MSVC compiler from Visual Studio without any errors. I correctly configured the header and library files for a new project and successfully ran FLTK's sample program.

Here's the project structure:

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

The CMake file is as follows:

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)

And the sample program:

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

The error message you received:

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
0

There are 0 answers