针对 glib 编译时出现链接器错误...? [英] Linker errors when compiling against glib...?

查看:18
本文介绍了针对 glib 编译时出现链接器错误...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu 上针对 glib 编译简单的示例程序时遇到问题.我收到以下错误.我可以让它编译但不能与 -c 标志链接,我相信这意味着我安装了 glib 头文件,但它没有找到共享对象代码.另请参阅下面的 Make 文件.

$>重新制作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:在函数‘print_uppercase_words’中:re.c:(.text+0x21):对`g_regex_new'的未定义引用re.c:(.text+0x41):对`g_regex_match'的未定义引用re.c:(.text+0x54):对`g_match_info_fetch'的未定义引用re.c:(.text+0x6e):对`g_print'的未定义引用re.c:(.text+0x7a):对`g_free'的未定义引用re.c:(.text+0x8b):对`g_match_info_next'的未定义引用re.c:(.text+0x97):对`g_match_info_matches'的未定义引用re.c:(.text+0xa7):对`g_match_info_free'的未定义引用re.c:(.text+0xb3):对`g_regex_unref'的未定义引用collect2: ld 返回 1 个退出状态make: *** [re] 错误 1

Makefile 使用:

# 需要安装 libglib2.0-dev 一些系统特定的安装# 为 pkg-config 提供一个值INCLUDES=$(shell pkg-config --libs --cflags glib-2.0)CC=gcc $(包括)项目=re# 目标完整:干净编译干净的:rm $(项目)编译:$(CC) $(PROJECT).c -o $(PROJECT)

.c 正在编译的代码:

#include void print_upppercase_words(const gchar *string){/* 打印所有仅大写的单词.*/GRegex *正则表达式;GMatchInfo *match_info;regex = g_regex_new("[A-Z]+", 0, 0, NULL);g_regex_match(regex, string, 0, &match_info);而 (g_match_info_matches(match_info)){gchar *word = g_match_info_fetch(match_info, 0);g_print("找到 %s
", 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 = "我的身体是一个笼子.我的思想是关键.";print_uppercase_words(string);}

奇怪的是,当我运行 glib-config 时,它不喜欢那个命令,虽然我不知道如何告诉 Bash 或 Make 在它抱怨时如何使用一个而不是另一个gdlib-config 在这两个包中.

$>glib 配置未找到命令glib-config",您的意思是:来自包libgd2-xpm-dev"(主)的命令gdlib-config"来自包libgd2-noxpm-dev"(主)的命令gdlib-config"glib-config:找不到命令

解决方案

编译器命令末尾的库:

<块引用>

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

来自 GCC 链接选项:

<前>-图书馆-l 库链接时搜索名为 library 的库.(第二种选择是将库作为单独的参数仅用于 POSIX 合规性,不推荐使用.)在命令中编写此选项的位置有所不同;链接器搜索和处理库和目标文件它们被指定的顺序.因此,`foo.o -lz bar.o' 在文件 foo.o 之后搜索库 `z' 但是在 bar.o. 之前如果 bar.o 引用 `z' 中的函数,则这些函数可能无法加载.

I'm having trouble compiling a simple, sample program against glib on Ubuntu. I get the following errors. I can get it to compile but not link with the -c flag, which I believe means I have the glib headers installed, but it's not finding the shared object code. See also the Make file below.

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

# 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 code being compiled:

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

Strangely, when I run glib-config, it doesn't like that command, though I don't know how to tell Bash or Make how to just use one over the other when it complains that gdlib-config is in these two packages.

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

解决方案

Libraries at the end of the compiler command:

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

From 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.

这篇关于针对 glib 编译时出现链接器错误...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆