2012-04-01 3 views
8

우분투에서 간단한 예제 프로그램을 컴파일하는 데 문제가 있습니다. 다음과 같은 오류가 발생합니다. 컴파일 할 수 있지만 -c 플래그로 연결되지는 않습니다.이 플래그는 glib 헤더가 설치되어 있다고 믿습니다.하지만 공유 객체 코드를 찾지 못했습니다. 아래 Make 파일을 참조하십시오.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 사용 :

# 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 코드는 컴파일되는 : 나는 glib-config을 실행할 때

#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); 
} 

이상하게도, 그것은 그 명령을 좋아하지 않는다, 나도 몰라하지만이이 두 패키지에 있다고 불평 할 때 Bash에게 말하는 방법 또는 다른 것을 사용하는 방법을 만드십시오. 컴파일러 명령의 끝에

$> 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

그래서 해결책은 무엇입니까, 이해가 안되나요? – user3728501

답변

23

라이브러리 :

의 gcc -I는/usr/포함/입심 - 2.0 -I는/usr/lib 디렉토리/x86_64에-리눅스 GNU/입심-2.0/re.c을 포함 -o -lglib-2.0

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

그건 싱글 패스 링크라고합니다 : https://computation.llnl.gov/casc/components/docs/users_guide/node319.html –

+0

글쎄,이 문제를 연구하는 동안 읽었습니다. : 의 gcc -I는/usr/포함/입심 - 2.0 -I는/usr/lib 디렉토리/x86_64에-리눅스 GNU/입심-2.0 /은 -lglib-2.0 re.c을 -o 포함 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 주문이 실제로 아무 것도 변경하지 않는다고 생각합니다. 나는 -lglib-2.0 플래그가 .so 파일을 가리켜 야한다고 생각했지만, .so 파일을 찾지는 않는다고 생각한다. – lucidquiet

+0

그래서 해결책은 무엇입니까, 이해가 안되니? – 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 

어 - 지옥 - 나는 많은 것들을 시도해 보았고 아마도 조금 잘못 이해했을 것입니다.하지만 지금은 위의 것을 시도해 보았습니다.

+0

게시 한 내용이 아닌가요? – hmjd

+0

그건 - 그게 내가 너의 대답을 받아 들인 이유야.나는 당신의 포스트에 코멘트를 추가해야한다고 생각한다. – lucidquiet

+0

받아들이려면 대답 옆에있는 흰 눈금을 클릭해야합니다. – hmjd

관련 문제