![glib에 대해 컴파일할 때 링커 오류가 발생했습니다...?](https://linux55.com/image/22030/glib%EC%97%90%20%EB%8C%80%ED%95%B4%20%EC%BB%B4%ED%8C%8C%EC%9D%BC%ED%95%A0%20%EB%95%8C%20%EB%A7%81%EC%BB%A4%20%EC%98%A4%EB%A5%98%EA%B0%80%20%EB%B0%9C%EC%83%9D%ED%96%88%EC%8A%B5%EB%8B%88%EB%8B%A4...%3F.png)
Ubunutu에서 glib에 대한 간단한 예제 프로그램을 컴파일하는 데 문제가 있습니다. 이러한 오류가 발생합니다. 컴파일할 수 있지만 -c 플래그로 링크할 수는 없습니다. 나는 이것이 glib 헤더가 설치되어 있지만 공유 객체 코드를 찾지 못한다는 것을 의미한다고 믿습니다. 아래 make 파일도 참조하세요.
$> 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
사용된 메이크파일:
# 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 코드 컴파일:
#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
방법을 모르지만 말입니다 .gdlib-config
$> 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
답변1
Glibness는 당신의 문제가 아닙니다. 이것은:
re.c:(.text+0xd6): undefined reference to `print_uppercase_words'
이것이 의미하는 바는 함수를 호출하고 print_uppercase_words
있는데 함수를 찾을 수 없다는 것입니다.
여기에는 이유가 있습니다. 주의 깊게 관찰하십시오. 오타가 있습니다:
void print_upppercase_words(const gchar *string)
이 문제를 해결한 후에도 이러한 라이브러리를 필요로 하는 모듈보다 먼저 지정하기 때문에 여전히 문제가 발생할 수 있습니다. 간단히 말해서, 명령은 다음과 같이 작성되어야 합니다.
gcc -o re re.o -lglib-2.0
그래서 다음 -lglib-2.0
은 다음입니다 re.o
.
그래서 저는 Makefile을 다음과 같이 작성하겠습니다:
re.o: re.c
$(CC) -I<includes> -o $@ -c $^
re: re.o
$(CC) $^ -l<libraries> -o $@
실제로 올바른 변수를 설정하면 make
모든 문제가 자동으로 해결됩니다.
CFLAGS=$(shell pkg-config --cflags glib-2.0)
LDLIBS=$(shell pkg-config --libs glib-2.0)
CC=gcc
re: re.o