glib에 대해 컴파일할 때 링커 오류가 발생합니까...?

glib에 대해 컴파일할 때 링커 오류가 발생합니까...?

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에 지시하는 방법이나 이 2개 패키지에 있다고 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

glib는 당신의 문제가 아닙니다. 이것은:

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

관련 정보