2012-04-01 13 views
8

Tengo problemas para compilar un sencillo programa de ejemplo contra glib en Ubuntu. Obtengo los siguientes errores Puedo hacer que se compile pero no se vincula con el indicador -c, lo que creo que significa que tengo instalados los encabezados glib pero no encuentra el código de objeto compartido. Vea también el archivo Make a continuación.¿Errores del enlazador al compilar contra glib ...?

$> make re 
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0  re.c -o re 
/tmp/ccxas1nI.o: In function `print_uppercase_words': 
re.c:(.text+0x21): undefined reference to `g_regex_new' 
re.c:(.text+0x41): undefined reference to `g_regex_match' 
re.c:(.text+0x54): undefined reference to `g_match_info_fetch' 
re.c:(.text+0x6e): undefined reference to `g_print' 
re.c:(.text+0x7a): undefined reference to `g_free' 
re.c:(.text+0x8b): undefined reference to `g_match_info_next' 
re.c:(.text+0x97): undefined reference to `g_match_info_matches' 
re.c:(.text+0xa7): undefined reference to `g_match_info_free' 
re.c:(.text+0xb3): undefined reference to `g_regex_unref' 
collect2: ld returned 1 exit status 
make: *** [re] Error 1 

Makefile utilizado:

# Need to installed libglib2.0-dev some system specific install that will 
# provide a value for pkg-config 
INCLUDES=$(shell pkg-config --libs --cflags glib-2.0) 
CC=gcc $(INCLUDES) 
PROJECT=re 

# Targets 
full: clean compile 

clean: 
    rm $(PROJECT) 

compile: 
    $(CC) $(PROJECT).c -o $(PROJECT) 

.c código en fase de archivo:

#include <glib.h> 

void print_upppercase_words(const gchar *string) 
{ 
    /* Print all uppercase-only words. */ 

    GRegex *regex; 
    GMatchInfo *match_info; 

    regex = g_regex_new("[A-Z]+", 0, 0, NULL); 
    g_regex_match(regex, string, 0, &match_info); 

    while (g_match_info_matches(match_info)) 
    { 
     gchar *word = g_match_info_fetch(match_info, 0); 
     g_print("Found %s\n", word); 
     g_free(word); 
     g_match_info_next(match_info, NULL); 
    } 

    g_match_info_free(match_info); 
    g_regex_unref(regex); 
} 

int main() 
{ 
    gchar *string = "My body is a cage. My mind is THE key."; 

    print_uppercase_words(string); 
} 

Curiosamente, cuando corro glib-config, que no le gusta ese comando, aunque no sé cómo decir Bash o cómo usar uno sobre el otro cuando se queja de que gdlib-config está en estos dos paquetes.

$> glib-config 
No command 'glib-config' found, did you mean: 
Command 'gdlib-config' from package 'libgd2-xpm-dev' (main) 
Command 'gdlib-config' from package 'libgd2-noxpm-dev' (main) 
glib-config: command not found 
+0

Entonces, ¿cuál fue la solución, no entiendo? – user3728501

Respuesta

23

Bibliotecas al final del comando del compilador:

gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/simplistas-2.0/re.c incluir -o re -lglib-2,0

De GCC Link Options:

 
-llibrary 
-l library 
    Search the library named library when linking. 
    (The second alternative with the library as a separate argument 
    is only for POSIX compliance and is not recommended.) 

    It makes a difference where in the command you write this option; 
    the linker searches and processes libraries and object files in the 
    order they are specified. 
    Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but 
    before bar.o. If bar.o refers to functions in `z', those functions 
    may not be loaded. 
+1

Eso se llama enlace de paso único: https://computation.llnl.gov/casc/components/docs/users_guide/node319.html –

+0

Bueno, lo he leído mientras investigaba este problema, y ​​he intentado todos estos : gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0 re.c -o re gcc -lglib-2.0 - I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include re.c -o re gcc -I/usr/include/glib-2.0 -I/usr /lib/x86_64-linux-gnu/glib-2.0/include re.c -o re -lglib-2.0 No creo que el pedido realmente esté cambiando nada. Pensé que el indicador -lglib-2.0 debería apuntar a los archivos .so, pero no creo que encuentre los archivos .so. – lucidquiet

+0

Entonces, ¿cuál fue la solución, no entiendo? – user3728501

2
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include re.c -o re -lglib-2.0 

Uh - demonios - He intentado tantas cosas y probablemente las he equivocado un poco, pero intenté lo anterior, y funcionó .... tan resueltamente.

+0

¿No es eso lo que publiqué? – hmjd

+0

Es - por eso acepté su respuesta.Supongo que debería haber agregado un comentario a tu publicación. – lucidquiet

+0

Tiene que hacer clic en la marca blanca al lado de la respuesta para aceptar. – hmjd

Cuestiones relacionadas