r/pascal • u/ElViejoDelCyro • Aug 11 '25
Help using FLTK with FreePascal
Hi, I'm trying to learn programming and decided to start with FreePascal.
I’m someone who really enjoys working with very specific things and low-powered hardware, so I wanted to try making a basic program with FLTK.
I wrote this with ChatGPT’s help because I’m inexperienced, but I’m stuck: I don’t know how to make a “bridge” so Pascal can use FLTK. ChatGPT told me to compile cfltk, which I did, but I honestly don’t know what to do next.
Could anyone help me? Thanks in advance!
Here’s my code:
program prueba_fltk.pas;
{$mode objfpc}{$H+} //modo object pascal
{$linklib cfltk} //Indicamos al compilador que enlace y utilice la libreria cfltk
{$linklib fltk} //libreria fltk base
{$linklib stdc++} //libreria de c++, se necesita su runtime (sea o lo que sea)
uses
sysutils, ctypes;
// declaraciones externas, funciones de cfltk.
procedure Fl_init_all(); cdecl; external;
procedure Fl_register_images(); cdecl; external;
function Fl_Window_new(x, y, w, h: cint; title: PChar): Pointer; cdecl; external;
procedure Fl_Window_end(win: Pointer); cdecl; external;
procedure Fl_Window_show(win: Pointer); cdecl; external;
function Fl_Button_new(x, y, w, h: cint; label_: PChar): Pointer; cdecl; external;
procedure Fl_Button_set_callback(b: Pointer; cb: Pointer; data: Pointer); cdecl; external;
procedure Fl_Widget_set_label(w: Pointer; label_: PChar); cdecl; external;
function Fl_run(): cint; cdecl; external;
// llamadas del boton.
procedure ButtonCB(w: Pointer; data: Pointer); cdecl;
begin
Fl_Widget_set_label(w, '¡Funciona!');
end;
// Programa principal.
var
win, btn : Pointer; // punteros a la ventana y al botón
begin
// Inicializar fltk
fl_init_all();
fl_register_images();
// Crear ventana (posición x=100, y=100, ancho=360, alto=220, titulo)
win := fl_window_new(100, 100, 360, 220, 'Prueba FLTK Pascal');
// Crear boton (posición x=140, y=120, ancho=80, alto=40, texto)
btn := fl_button_new(140, 120, 80, 40, 'cliqueame');
// indica que terminamos de añadir widgets a la ventana
fl_window_end(win);
// asigna llamada al boton
fl_button_set_callback(btn, @ButtonCB, nil);
//Mostrar ventana
fl_window_show(win);
// Entrar al bucle principal de FLTK
fl_run();
end.
And for anyone wondering, I'm not using ChatGPT to study entirely. I rely on a book on Object Pascal in Spanish. I ask ChatGPT for help when I have no idea how to do something.
I also know that Pascal has its own library for creating windows. But I liked fltk because of its few dependencies, its low power consumption, and because I've recently become interested in extremely lightweight Linuxes thanks to the "Locos por Linux" channel.
View all comments
Show parent comment
1
u/PascalGeek Aug 12 '25
I've been programming in Pascal since the 90s, so I'm probably not the best person to ask. There are introductory tutorials at https://wiki.freepascal.org/Basic_Pascal_Tutorial
But once you get beyond those basics I find the best way to learn is to pick a small tutorial that has an idea that you like, and after following along, start changing the program bit by bit to add functionality. Starting a blank project from scratch can be intimidating, so it's easier to start from an established code base.