i am trying to build an gtk4 application using cmake with next structure
every guide i have found is written in c++ and it seems that for C there is some kind of different process for building libraries using cmake
my main.c file contents:
#include "mainwindow.h"
int main(int argc, char *argv[]){
GtkApplication *app;
int status;
app = gtk_application_new("org.tasklist", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate_mainwindow), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
root CMakeLists.txt file
cmake_minimum_required(VERSION 3.31.5)
project(tasklist LANGUAGES C)
add_executable(tasklist main.c)
add_subdirectory(gui)
target_link_libraries(tasklist PUBLIC gui)
target_include_directories(tasklist PUBLIC
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/gui"
)
gui directory CMakeLists.txt file
add_library(gui mainwindow.c)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtk4)
target_link_libraries(gui PUBLIC PkgConfig::GTK4)
contents of mainwindow.h
#include <gtk/gtk.h>
static void activate_mainwindow(GtkApplication *app, gpointer user_data);
contents of mainwindow.c
#include "mainwindow.h"
static void activate_mainwindow(GtkApplication *app, gpointer user_data){
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_present (GTK_WINDOW (window));
}
results of cmake command are successful, but running make in ./build directory returns
make -C ./build
make: Entering directory '/home/dima/programming/side-projects/tasklist/build'
[ 50%] Built target gui
[ 75%] Building C object CMakeFiles/tasklist.dir/main.c.o
In file included from /home/dima/programming/side-projects/tasklist/main.c:1:
/home/dima/programming/side-projects/tasklist/gui/mainwindow.h:3:13: warning: ‘activate_mainwindow’ used but never defined
3 | static void activate_mainwindow(GtkApplication *app, gpointer user_data);
| ^~~~~~~~~~~~~~~~~~~
[100%] Linking C executable tasklist
/usr/bin/ld: CMakeFiles/tasklist.dir/main.c.o: warning: relocation against `activate_mainwindow' in read-only section `.text'
/usr/bin/ld: CMakeFiles/tasklist.dir/main.c.o: in function `main':
main.c:(.text+0x3f): undefined reference to `activate_mainwindow'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/tasklist.dir/build.make:114: tasklist] Error 1
make[1]: *** [CMakeFiles/Makefile2:109: CMakeFiles/tasklist.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
make: Leaving directory '/home/dima/programming/side-projects/tasklist/build'
currently reading beginner material, static keyword prevents functions to be linked from other files, which was exactly my problem, removing it helped