0

The problem is I struggle to make the Autotools work with my C program. I am stuck getting the errors "undefined reference to...".

I work on Ubuntu 20.04.1 LTS.

I am using:

  • Xml glade file
  • Css file
  • stdlib.h
  • gtk/gtk.h (v3)
  • glib.h
  • stdbool.h

The hierarchy is:

main.c
Makefile.am
configure.ac
  /src
    structures.h
    /features
      timer.c
      timer.h
    /frontend
      frontend.c
      frontend.h
      style.css
      view.glade
    /signals
      options.c
      options.h
      timer_buttons.c
      timer_buttons.h

I successfully compile the program using:

gcc-9 `pkg-config --cflags gtk+-3.0` -o main main.c `pkg-config --libs gtk+-3.0` -rdynamic -g -Wall

It’s when I want to make a makefile using the autotools that I need a bit of help.

Configure.ac

AC_INIT([main], [0.1])
AM_INIT_AUTOMAKE([foreign subdir-objects -Wall])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
PKG_CHECK_MODULES([GTK], ["gtk+-3.0"])
AC_OUTPUT

Makefile.am

bin_PROGRAMS = main
main_SOURCES = main.c
main_CPPFLAGS = $(GTK_CFLAGS)
main_LDFLAGS = $(GTK_LIBS)

Then I do the following:

autoconf

automake

./configure

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for GTK... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands

make

gcc  -g -O2 -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0  -o main main-main.o  
/usr/bin/ld: main-main.o: in function `run_timer':
/home/myname/Desktop/project/src/features/timer.c:46: undefined reference to `gtk_label_get_type'
/usr/bin/ld: /home/myname/Desktop/project/src/features/timer.c:46: undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: /home/myname/Desktop/project/src/features/timer.c:46: undefined reference to `gtk_label_set_text'
/usr/bin/ld: main-main.o: in function `stick_checkbox_signal':
/home/myname/Desktop/project/src/signals/options.c:6: undefined reference to `gtk_toggle_button_get_active'
/usr/bin/ld: main-main.o: in function `start_signal':
/home/myname/Desktop/project/src/signals/timer_buttons.c:11: undefined reference to `g_timeout_add'
/usr/bin/ld: main-main.o: in function `pause_signal':
/home/myname/Desktop/project/src/signals/timer_buttons.c:17: undefined reference to `g_source_remove'
/usr/bin/ld: main-main.o: in function `reset_signal':
/home/myname/Desktop/project/src/signals/timer_buttons.c:26: undefined reference to `g_source_remove'

###
etc.............
###

collect2: error: ld returned 1 exit status
make: *** [Makefile:364: main] Error 1

You can see that I get these "undefined reference to..." errors. How do I make it work? I have been looking this up on internet quite a lot but I'm stuck here.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 1
    As described at https://www.gnu.org/software/automake/manual/html_node/Linking.html#Linking , the `main_LDFLAGS` variable is the wrong place to put libraries. You need to put them in `main_LIBADD` instead. This is because libraries need to go *after* object files on the command line. – zwol Dec 08 '20 at 19:30
  • 1
    I'm pretty sure this question has been asked and answered before, but I'm not having any luck finding a good "canonical duplicate" for it. – zwol Dec 08 '20 at 19:32
  • 1
    I found a pretty food dupe, I think, @zwol. It even ties in pkg-config as in this question. – John Bollinger Dec 09 '20 at 00:12
  • Thank you, that actually helped. I tried main_LIBADD and then I got an error telling me to switch to main_LDADD so it helped in a way. And the linked question gives main_LDADD as the answer so it's all good. Now it works fine. – gliese667cc Dec 09 '20 at 20:43
  • @gliese667cc Sorry about that, I forgot about the difference between `_LIBADD` and `_LDADD`. – zwol Dec 22 '20 at 20:26

0 Answers0