Undefined reference to `g_ascii_strcasecmp' - c

I can compile with this line:
#include <glib.h>
But when I add this line:
GTree* t = g_tree_new((GCompareFunc)g_ascii_strcasecmp);
I'm getting this error:
gcc cli.c -g -Wall -O0 -o httpget `pkg-config --cflags glib-2.0`
/s/cli.c:215: undefined reference to `g_ascii_strcasecmp'
/s/cli.c:215: undefined reference to `g_tree_new'
What do I do wrong?

You're forgetting to link with glib:
gcc `pkg-config --cflags glib-2.0` -g -Wall -O0 -o httpget cli.c `pkg-config --libs glib-2.0`
Or better, separate compilation and linking:
gcc `pkg-config --cflags glib-2.0` -g -Wall -O0 -c cli.c
gcc -o httpget cli.o `pkg-config --libs glib-2.0`

Related

Trouble in linking the libIEC61850 library with GTK3+ for C

I'm having trouble combining compilations and linkages for two separate libraries. Following is a makefile for one of the libraries (libIEC61850):
LIBIEC_HOME=../../iec61850/libiec61850-1.4.0/
#Add this somehow:
#cc `pkg-config --cflags gtk+-3.0` main.c -o a.out `pkg-config --libs gtk+-3.0`
PROJECT_BINARY_NAME = a.out
PROJECT_SOURCES += main.c
INCLUDES += -I.
include $(LIBIEC_HOME)/make/target_system.mk
include $(LIBIEC_HOME)/make/stack_includes.mk
all: $(PROJECT_BINARY_NAME)
include $(LIBIEC_HOME)/make/common_targets.mk
$(PROJECT_BINARY_NAME): $(PROJECT_SOURCES) $(LIB_NAME)
$(CC) $(CFLAGS) $(LDFLAGS) -o $(PROJECT_BINARY_NAME) $(PROJECT_SOURCES) $(INCLUDES) $(LIB_NAME) $(LDLIBS)
clean:
rm -f $(PROJECT_BINARY_NAME)
I would like to add the gtk-3.0 library in the compilation of main.c. To execute a typical gtk3 function, I simply write:
$ cc `pkg-config --cflags gtk+-3.0` main.c -o a.out `pkg-config --libs gtk+-3.0`
and the executable is generated without problem. How can I combine these two?
Fixed. Following is a Makefile to combine the libIEC61850 and GTK+ 3.0 libraries:
# Makefile to combine the libIEC61850 and GTK+-3.0 Libraries
# path to libIEC61850:
LIBIEC_HOME=../../iec61850/libiec61850-1.4.0/
#Add this somehow:
#cc `pkg-config --cflags gtk+-3.0` main.c -o a.out `pkg-config --libs gtk+-3.0`
PROJECT_BINARY_NAME = a.out
PROJECT_SOURCES += main.c
include $(LIBIEC_HOME)/make/target_system.mk
include $(LIBIEC_HOME)/make/stack_includes.mk
all: $(PROJECT_BINARY_NAME)
include $(LIBIEC_HOME)/make/common_targets.mk
$(PROJECT_BINARY_NAME): $(PROJECT_SOURCES) $(LIB_NAME)
$(CC) $(CFLAGS) $(shell pkg-config --cflags gtk+-3.0) $(LDFLAGS) \
-o $(PROJECT_BINARY_NAME) $(PROJECT_SOURCES) $(INCLUDES) \
$(LIB_NAME) $(LDLIBS) $(shell pkg-config --libs gtk+-3.0)
clean:
rm -f $(PROJECT_BINARY_NAME)
I would add pkg-config --cflags gtk+-3.0 to CFLAGS and pkg-config --libs gtk+-3.0 to LDLIBS:
CFLAGS += $(shell pkg-config --cflags gtk+-3.0)
LDLIBS += $(shell pkg-config --libs gtk+-3.0)
They'll be sucked into the build commands the way you want, assuming all recipes follow conventional variable naming.
Use target specific variables if passing the flags to everything isn't to your liking.

error compiling c program on r-pi raspbian

I have this code that compiles fine on my desktop, but when I try to compile it on the raspberry pi, I get this error
gcc -Wall -O2 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi `sdl-config --cflags --libs` -c main.c
gcc -Wall -O2 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi `sdl-config --cflags --libs` -c fft.c
gcc -Wall -O2 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi `sdl-config --cflags --libs` -c draw.c
gcc -Wall -O2 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi `sdl-config --cflags --libs` -c table.c
gcc -Wall -O2 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi `sdl-config --cflags --libs` -c serial.c
gcc -Wall -O2 -lGL -lGLU -lm -lSDL_image -lfftw3 -lftdi `sdl-config --cflags --libs` main.o fft.o draw.o table.o serial.o -o main
/usr/bin/ld: Warning: size of symbol `table' changed from 2048 in
table.o to 204 in
//usr/lib/arm-linux-gnueabihf/pulseaudio/libpulsecommon-10.0.so
/usr/bin/ld: table.o: undefined reference to symbol 'table'
//usr/lib/arm-linux-gnueabihf/pulseaudio/libpulsecommon-10.0.so: error
adding symbols: DSO missing from command line collect2: error: ld
returned 1 exit status makefile:6: recipe for target 'main' failed
make: *** [main] Error 1
I've tried reinstalling libpulse0 and made sure pulseaudio was installed, and I can find the libpulsecommon-10.0.so when I search for it on the pi. So I'm not sure what to do from here.
libpulsecommon has a global symbol named table, and your code (probably table.c) also has a public symbol with this name. The symbols are colliding when linking. Rename that variable/function on your code.

Linking SDL in a C program

I have recently become interested in using SDL after having learned some basics of C. I have installed SDL_image and SDL_mixer. They are located in /usr/local/include/SDL2. I realize that you must link against the header files however I am not sure how to do it. I am getting the error that SDL_mixer or SDL_image do not exist (depending on their line order in my source code). I have tried two different compilation commands and neither work here they are:
gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
gcc filename.c -o test -I./usr/local/include/SDL2 -L./lib -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
If anyone has any ideas I would appreciate it! Thanks in advance!
you do not want that leading period
wrong
gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
closer - not necessarily correct yet
gcc filename.c -o test -L/usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
any path with a leading period indicates to start from current dir and go relative instead of the intended absolute path
any system has the notion of a default library path which is fine if you are using a standard install ... so no need to do a
-I/include
... sometime a library has helpers to identify and auto populate these ...
sdl and sdl2 do have such a helper ... this will give you those settings
gcc -o test filename.c `pkg-config --cflags --libs sdl2`
notice those backticks ... another syntax style would be
gcc -o test filename.c $(pkg-config --cflags --libs sdl2)
you are free to issue that stand alone just to take a peek
pkg-config --cflags --libs sdl2
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2
Now onto your sdl mixer ... well it has a
pkg-config --cflags --libs SDL2_mixer
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2_mixer -lSDL2
you probably do not want to mix sdl with sdl2 so replace mention of
-lSDL_mixer -lSDL_image
with
-lSDL2_mixer -lSDL2_image
as per
pkg-config --cflags --libs SDL2_image
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2_image -lSDL2
so bundling these together
gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs sdl2) $(pkg-config --cflags --libs SDL2_mixer) $(pkg-config --cflags --libs SDL2_image)
or more simply combined to
gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs sdl2 SDL2_mixer SDL2_image )
this can be stripped down to simply the following ... yet above syntax is more robust to changes
gcc -o test filename.c -D_REENTRANT -I/usr/include/SDL2 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_image
You can use sdl2-config to supply the appropriate flags to gcc:
gcc filename.c -o test `sdl2-config --cflags --libs`
sdl2-config --cflags produces a list of options that should be passed to the compiler, and sdl2-config --libs produces a list of libraries that should be linked to.

Use Azure Iothub_client examples in own code

I'm trying to use the AMQPoverWebsockets-Client example from the C-SDK of Azure-iothub.
How do I link the specific libraries to the makefile to compile it successfully?
I tried to write the path down in my makefile. But it didn't work.
Here is my makefile so far:
SENSOR=mcp9808
SENSOR2=adxl345x
SENSOR3=init
SENSOR4=filestorage
IOT1=src/iothubtransporthttp.c.o
IOT2=src/iothub_client_ll.c.o
IOT3=src/iothub_message.c.o
IOT4=src/platform_linux.c.o
IOT5=src/crt_abstractions.c.o
all:
gcc -Wall -c $(SENSOR).c -o $(SENSOR).o -lm -lwiringPi -lrt
gcc -Wall -c $(SENSOR2).c -o $(SENSOR2).o -lm -lwiringPi -lrt
gcc -Wall -c $(SENSOR4).c -o $(SENSOR4).o -lm -lwiringPi -lrt
gcc -Wall -c $(SENSOR3).c -o $(SENSOR3).o -lm -lrt `pkg-config --cflags --libs glib-2.0`
#gcc -Wall $(IOT1) $(IOT2) $(IOT3) $(IOT4) $(IOT5) iothub_client_sample_http.c -o iot_client -lm -lrt
gcc -Wall $(SENSOR).o $(SENSOR2).o $(SENSOR3).o $(SENSOR4).o main.c -o main -lm -lwiringPi -lrt `pkg-config --cflags --libs glib-2.0`
clean:
rm *.o > /dev/null 2>&1 &

How to use pkg-config in Make

I want to compile the simplest GTK program.
I can compile it using the command line:
gcc $(pkg-config --cflags --libs gtk+-3.0) main.c -o main.o
However, if I use Make it doesnt work:
CFLAGS=-g -Wall -Wextra $(pkg-config --cflags)
LDFLAGS=$(pkg-config --libs gtk+-3.0)
CC=gcc
SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))
all: $(EXECUTABLES)
It tells me this:
gcc -g -Wall -Wextra -c -o main.o main.c
main.c:1:21: fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
^
compilation terminated.
<builtin>: recipe for target 'main.o' failed
make: *** [main.o] Error 1
Where do I stick $(pkg-config --cflags --libs gtk+-3.0) in the Makefile to make it compile?
Thanks very much in advance for your kind help.
There are two issues.
First, your CFLAGS line is wrong: you forgot to say gtk+-3.0 in the pkg-config part, so pkg-config will spit out an error instead:
CFLAGS=-g -Wall -Wextra $(pkg-config --cflags gtk+-3.0)
Second, and more important, $(...) is intercepted by make itself for variable substitution. In fact, you've seen this already:
SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))
all: $(EXECUTABLES)
is all done by make.
There are two things you can do.
First, you can use `...` instead, which does the same thing ($(...) is newer shell syntax).
CFLAGS=-g -Wall -Wextra `pkg-config --cflags gtk+-3.0`
LDFLAGS=`pkg-config --libs gtk+-3.0`
Second, since you seem to be using GNU make, you can use the shell substitution command, which was shown in the answer Basile Starynkevitch linked above:
CFLAGS=-g -Wall -Wextra $(shell pkg-config --cflags gtk+-3.0)
LDFLAGS=$(shell pkg-config --libs gtk+-3.0)

Resources