admin管理员组

文章数量:1277902

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(".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'

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(".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'
Share Improve this question edited Feb 24 at 18:36 Dima asked Feb 24 at 18:12 DimaDima 214 bronze badges 5
  • 1 There doesn't seem to be any gui.h header file in your project. But there is agui/mainwindow.h header file. You might want to spend some time with your beginners material (books, tutorials, class-notes) to refresh how header files work. – Some programmer dude Commented Feb 24 at 18:15
  • my bad, i was trying things out of desperation, however when i change #include "gui.h" to #include "mainwindow.h" is looks like cmake failed to link source file to header make -C ./build /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 – Dima Commented Feb 24 at 18:31
  • 2 Hmm... I also think you need to take some time more with your beginners material, and learn what the static keyword means. – Some programmer dude Commented Feb 24 at 18:39
  • "it seems that for C there is some kind of different process for building libraries using cmake" -- no, not really. You do need to tell CMake that C is among the project's languages, but other than that, the CMake part is pretty much the same for C and C++. – John Bollinger Commented Feb 24 at 18:39
  • removing static keyword helped, thanks for your help, also i decided to spend this whole night reading beginners materials, including third grade calculus and second grade grammar – Dima Commented Feb 24 at 18:45
Add a comment  | 

1 Answer 1

Reset to default 0

currently reading beginner material, static keyword prevents functions to be linked from other files, which was exactly my problem, removing it helped

本文标签: How to create library for cmake project written in cStack Overflow