Error compiling with GLib - c

I'm pretty new to C programming, and am trying to work through the exercises in '21st Century C' second edition. I'm stuck on page 202, Example 9-7, unicode.c. This example starts with:
#include <glib.h>
#include <locale.h> //setlocale
#include "string_utilities.h"
#include "stopif.h"
//Frees instring for you--we can't use it for anything else.
char *localstring_to_utf8(char *instring){
GError *e=NULL;
setlocale(LC_ALL, ""); //get the OS's locale.
char *out = g_locale_to_utf8(instring, -1, NULL, NULL, &e);
free(instring); //done with the original
Stopif(!out, return NULL, "Trouble converting from your locale to UTF-8.");
Stopif(!g_utf8_validate(out, -1, NULL), free(out); return NULL,
"Trouble: I couldn't convert your file to a valid UTF-8 string.");
return out;
}
When I try to compile it with:
c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
I get errors such as:
$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -o unicode
/tmp/ccBDQFiH.o: In function `localstring_to_utf8':
/home/kevin/21st_Century_C/ch09/unicode.c:29: undefined reference to `g_locale_to_utf8'
/home/kevin/21st_Century_C/ch09/unicode.c:32: undefined reference to `g_utf8_validate'
/tmp/ccBDQFiH.o: In function `main':
/home/kevin/21st_Century_C/ch09/unicode.c:48: undefined reference to `g_utf8_strlen'
This seems to indicate that the Glib library is not found, but the compiler didn't complain about this, and the Glib libraries and include files are right where I specified on the command line. I've installed the libglib2.0-dev package in addition to the libglib2.0 package (all installed with 'sudo apt-get ..'). 'pkg-config' seems to find glib-2.0 just fine.
This is all on a Ubuntu 14.04.2 system.
I can't figure out how to correct this error, and don't understand why it can't find the specific Glib functions, if it finds the glib include and lib files.

The order of things in the command line matter. In general it should be something like:
gcc [options] [source files] [object files] [-L stuff] [-lstuff] [-o outputfile]
So give this a whirl instead:
gcc -g -Wall -O3 -std=gnu11 `pkg-config --cflags glib-2.0` \
unicode.c string_utilities.o `pkg-config --libs glib-2.0` \
-o unicode
This is also covered in the Compiling GLib Applications section of the GLib Reference Manual:
$ cc hello.c `pkg-config --cflags --libs glib-2.0` -o hello

Related

SDL2 : Undefined symbols for architecture x86_64 / MAC [duplicate]

I recently moved to linux and i'm having an issue with compiling SDL C programs using gcc.
The command i'm using:
gcc `sdl-config --cflags --libs` -o main main.c
Even by seperating sdl-config flags:
gcc `sdl-config --cflags` -c main.c
gcc `sdl-config --libs` -o main main.o
I'm getting the same error:
/tmp/ccHYyjKd.o: In function `main':
main.c:(.text+0xe): undefined reference to `SDL_SetMainReady'
main.c:(.text+0x18): undefined reference to `SDL_Init'
main.c:(.text+0x31): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x54): undefined reference to `SDL_MapRGB'
main.c:(.text+0x6b): undefined reference to `SDL_FillRect'
main.c:(.text+0x77): undefined reference to `SDL_Flip'
main.c:(.text+0x83): undefined reference to `SDL_WaitEvent'
main.c:(.text+0x90): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status
My very simple program:
#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED
#include <SDL/SDL.h>
int main()
{
// SDL Initialize
SDL_SetMainReady();
SDL_Init(SDL_INIT_VIDEO);
// Screen Initialize
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
Uint32 screenColor = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen, NULL, screenColor);
SDL_Flip(screen);
// Main Program Loop
SDL_Event event;
do
{
SDL_WaitEvent(&event);
} while (event.type != SDL_QUIT);
// SDL Quit
SDL_Quit();
return 0;
}
Order of arguments to gcc matters a lot.
Read about Invoking GCC (and documentation of binutils, which gcc uses). Then replace
gcc `sdl-config --libs` -o main main.o
with
gcc main.o `sdl-config --libs` -o main
Better yet, learn how to use GNU make (it is often using GNU bash) and use a Makefile inspired by this answer...
Also, always pass -Wall -g to gcc until your program is bug-free (then use -Wall -O2)
Take inspiration from open source programs on github or gitlab using SDL. Consider also using other open source libraries and frameworks, such as Qt, SFML, GTKmm, etc... And study their example code.
Add -lSDL with gcc compile command. This will add sdl library. Install sdl developement package before compiling.
EDIT:
gcc -o out main.c -lSDL
or
gcc -I/usr/include/SDL/ main.c -o out -L/usr/lib -lSDL
I See this from /usr/include/SDL2/SDL_main.h
/*
* This is called by the real SDL main function to let the rest of the
* library know that initialization was done properly.
*
* Calling this yourself without knowing what you're doing can cause
* crashes and hard to diagnose problems with your application.
*/
extern DECLSPEC void SDL_SetMainReady(void);
Also check this:
nm /usr/lib/i386-linux-gnu/libSDL.a | grep SDL_SetMainReady
This is not the solution but will allow you to focus on the real problem, I think it is not the compilation process.
Thanks a lot for the advices ! This helped me to solve an old mystery about SDL symbols never found under Linux :-) As wroten in the comments, the order of gcc line is essential. The makefile was not well written and this was causing the breakage you mentionned.
As summary, use : gcc ( flags ) -o name ( include et linking options)
Last but not least, under x86_64, use gdb64, instead of gdb ( was the next headhache ;-)
As example, a little Makefile I wrote myself (ok, not that good, but works)
# Makefile pour le contrôle du robot Arduino
# Controle Robot
# Sauf mention contraire, tout est place sous Licence GPL V2
# Historique :
# Etienne HAMON / création du makefile initial (d'après cours IFT1), Novembre 2014
#
# Corrections du Makefile : Eric Bachard décembre 2014
CC = gcc
C_STANDARD = -std=c99
INCLUDE_DIR = inc -I/usr/include/SDL
SOURCES_DIR = sources
BUILD_DIR = build
APPLICATION_NAME = Controle
FILENAME = ${BUILD_DIR}/${APPLICATION_NAME}
CFLAGS = -Wall -ansi ${C_STANDARD}
LDFLAGS = -lSDL $(sdl-config --static-libs) -lm
DEBUG_SUFFIX = _debug
CFLAGS_DEBUG = -v -gdwarf-2 -DDEBUG
OBJS = ${SOURCES_DIR}/*.c
all : ${FILENAME} ${FILENAME}${DEBUG_SUFFIX}
${FILENAME}: ${OBJS}
${CC} ${CFLAGS} -o $# $^ -I${INCLUDE_DIR} ${LDFLAGS}
${FILENAME}${DEBUG_SUFFIX}: ${OBJS}
${CC} ${CFLAGS} ${CFLAGS_DEBUG} -o $# $^ -I${INCLUDE_DIR} ${LDFLAGS}
clean:
${RM} *.o ${FILENAME} ${FILENAME}${DEBUG_SUFFIX}
${RM} -rf ${BUILD_DIR}/*.dSYM
None of the "popular" answers for this question are correct or working. Some of them have nothing to even do with the question being asked. I have the same problem.
SDL docs give an example for compiling like this: gcc -o main main.c `sdl2-config --cflags --libs`
Yet user is suggesting that it is order of arguments causing the problem!! It is not! In fact, their suggested order is something I have never seen before and is certainly not any kind of standard. SDL answers on this site are very low quality!
Update:
I found a solution for some missing functions. SDL_SetVideoMode does not exist in SDL2. Use SDL_CreateWindow.

glibconfig.h no such file or directory

I just installed glib in Raspbian(Debian version). I want to read a config file using glib. I am trying to write a C application in Codeblocks and I use the header
#include <glib.h>
But I have an error in gtypes.h
fatal error:glibconfig.h No such file or directory
I used this path
project->Build Options->Compiler Settings->Other Options
and I added
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
as I read in this tutorial. I have to declare also
-L/usr/lib -lm -lglib-2.0
as the tutorial says and if yes then where and how can I declare it in Codeblocks?
Use pkg-config.
gcc `pkg-config --cflags glib-2.0` foo.c `pkg-config --libs glib-2.0`

using glib library in c program

I want to use hash tables in my c program.
I code:
...
#include <glib.h>
void main(int argc, char **argv)
{
GHashTable *g_hash_table;
...
g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
...
}
Then I compile:
$ gcc -I/usr/include/glib-2.0
-I/usr/lib/i386-linux-gnu/glib-2.0/include
-lglib-2.0 -o test test.c
or the same command:
$ gcc `pkg-config --cflags --libs glib-2.0` -o test test.c
but anyway the result is:
test.c: underfined reference to `g_int_equal`
test.c: underfined reference to `g_int_hash`
test.c: underfined reference to `g_hash_table_new`
collect2: ld returned 1 exit status
Why I cant compile my program? I do wrong include of glib library?
You need to specify libraries in the command line after the source and object files that use them:
gcc test.c `pkg-config --cflags --libs glib-2.0` -o test
From this pdf at IBM developper works, it's better to use the pkg-config if you have a standard install of glib with this command :
$ gcc `pkg-config --cflags --libs glib-2.0` -o ex-compile ex-compile.c
Your include look right and the way you are using it to. Not sure the ' will change anything but you might want to check the PDF, it contains a lot of examples and explainations.

gcc won't include libcurl on commandline for OS X

I'm trying to compile a C project I've been working on on a remote server that runs OS X. The project depends, in part, on libcurl. I only have access to the machine through my administrator account remotely.
When I attempt to make the project I keep getting errors relating to libcurl functions and constants not being defined. I conclude that libcurl is not being properly included by the compiler.
I'm using fink to install opensource software for all the dependencies ( postgres, curl, a few others ) and all the dependencies appear to work except curl.
My compiler command looks like:
gcc -ggdb -ansi -Wall -D_GNU_SOURCE -L `/sw/bin/pg_config --libdir` `/sw/bin/curl-config --cflags` -I `/sw/bin/pg_config --includedir` -lpq -lcurl -lpthread -lm `/sw/bin/curl-config --libs` -c Client.c
If I make a test file like so:
/sw/bin/curl http://www.google.com/ --libcurl test.c
And then attempt to compile it with:
gcc test.c `/sw/bin/curl-config --cflags` `/sw/bin/curl-config --libs` -o test.o
It also fails. Can anyone help me shed some light on this problem?
One compilation line is:
gcc -ggdb -ansi -Wall -D_GNU_SOURCE -L `/sw/bin/pg_config --libdir` \
`/sw/bin/curl-config --cflags` -I `/sw/bin/pg_config --includedir` \
-lpq -lcurl -lpthread -lm `/sw/bin/curl-config --libs` -c Client.c
This will take Client.c and generate Client.o, an object file. It doesn't need the library information; there is no linking taking place because of the -c option.
The other compilation line is:
gcc test.c `/sw/bin/curl-config --cflags` `/sw/bin/curl-config --libs` -o test.o
It is aconventional to end the names of executables with '.o'; it leads to confusion. However, if test.c only references functions from the standard libraries and libcurl, it should 'work'.
On my Mac, there is a copy of curl-config in /usr/bin.
Try this test program:
$ cat curltest.c
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
if (curl_global_init(0) == CURLE_OK)
{
printf("CURL version %s\n", curl_version());
curl_global_cleanup();
}
else
fprintf(stderr, "Failed to initialize CURL\n");
return 0;
}
$ cc -o curltest $(curl-config --cflags) curltest.c $(curl-config --libs)
$ file curltest
curltest: Mach-O 64-bit executable x86_64
$ otool -L curltest
curltest:
/usr/lib/libcurl.4.dylib (compatibility version 6.0.0, current version 6.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)
$ curltest
CURL version libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
$
This is on MacOS X 10.6.8.

how to compile a program with gtkmozembed.h

i have written a program under ubuntu, in which i include gtkmozembed.h. I am facing a problem in compiling the program.Below is the simplest form of a program which uses gtkmozembed.
#include <gtk/gtk.h>
#include <stdio.h>
#include <gtkmozembed.h>
int main(){
GtkWidget *mozEmbed;
mozEmbed = gtk_moz_embed_new();
return 0;
}
Eventhough, the above program is doing nothing, compiling that program is a lot for me...
I am trying to comile the above program like below
gcc `pkg-config --libs --cflags gtk+-2.0` test.c -o test
and it is giving the following error...
error: gtkmozembed.h: No such file or directory
I can understand, something else has to be added to the above gcc line,so that the compiler can find the gtkmozembed.h, but not getting what is that, 'something'...Looking for someone's help..Thank you...
Install libxul-dev (sudo apt-get install libxul-dev) and include
#include <gtkmozembed.h>
in the main file(test.c) and compile with
gcc `pkg-config --cflags --libs gtk+-2.0 xulrunner-gtkmozembed` test.c -o test
Your problem is that gtkmozembed.h is not found in the standard include file lookup path (well, the error does tell you that pretty obviously). On my system it lives in $(include)/gtkmozembed/, so you have two options
Change the path of the included file in your source
#include <gtkmozembed/gtkmozembed.h>
or manually add the path to the lookup path
gcc `pkg-config --libs --cflags gtk+-2.0` -I/usr/include/gtkmozembed test.c -o test
You should go with option 1).
This will tell gcc where to find the include file, but as pointed out by Matthew this is not enough: you will most probably also need to add more information for linking and required additional includes. Thankfully gtk-mozembed comes with a pkg-config file, so you can get all the needed information like you did for gtk+-2.0 with
pkg-config --libs --cflags mozilla-gtkmozembed-embedding
or combined with the other call
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test
You should also (just for kicks) have a look at what pkg-config does. The part in "`" is just what is return by the shell when executing that command. On my machine:
$ pkg-config --libs --cflags mozilla-gtkmozembed-embedding
-DXPCOM_GLUE -fshort-wchar \
-I/usr/include/xulrunner-1.9.2 -L/usr/lib/xulrunner-devel-1.9.2/lib -lxpcomglue
(line breaks added by me). The -I parts just adds additional needed directories to the include file lookup path -- they were emitted because you called with --cflags. The entries with -lxpcomglue is due to calling with --libs and ask for linking against this library, i.e. libxpcomglue.so. It is located in /usr/lib/xulrunner-devel-1.9.2/lib. The rest are a define and a gcc flag needed for gtkmozembed.
Try this:
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test

Resources