针对巧舌如簧编译时链接错误...? [英] Linker errors when compiling against glib...?

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

问题描述

我无法对编译Ubuntu上巧舌如簧简单,示例程序。我收到以下错误。我可以得到它的编译,但不与 -c 标志,我相信意味着我已经安装了能说会道的标题链接,但它没有找到共享对象code 。又见让下面文件。

  $>制作重
GCC -I / usr / include目录/巧舌如簧-2.0 -I / usr / lib目录/ x86_64的-Linux的GNU /油嘴-2.0 /包括-lglib-2.0 re.c -o重
/tmp/ccxas1nI.o:在功能`print_uppercase_words:
。re.c :(文字+ 0×21):未定义的参考`g_regex_new
。re.c :(文字+ 0×41):未定义的参考`g_regex_match
。re.c :(文字+ 0x54):未定义的参考`g_match_info_fetch
re.c :(文字+ 0x6e):未定义的参考`了g_print
re.c :(文字+ 0x7a):未定义的参考`g_free
。re.c :(文字+ 0x8b):未定义的参考`g_match_info_next
。re.c :(文字+ 0x97):未定义的参考`g_match_info_matches
re.c :(文字+ 0xa7):未定义的参考`g_match_info_free
。re.c :(文字+ 0xb3):未定义的参考`g_regex_unref
collect2:劳工处返回1退出状态
使:*** [重新]错误1

的Makefile 使用:

 #极品安装libglib2.0-dev的一些系统的具体安装意志
#为pkg配置的值
INCLUDES = $(壳pkg配置--libs --cflags巧舌如簧-2.0)
CC = GCC $(含)
PROJECT =重#目标
全:干净的编译清洁:
    RM $(PROJECT)编译:
    $(CC)$(PROJECT).C -o $(PROJECT)

.C code被编译:

 的#include< glib.h>无效print_upppercase_words(常量gchar *字符串)
{
  / *打印全部大写,只有文字。 * /  GRegex ​​正则表达式;
  GMatchInfo * match_info;  正则表达式= g_regex_new([A-Z] +,0,0,NULL);
  g_regex_match(正则表达式,字符串,0,&安培; match_info);  而(g_match_info_matches(match_info))
    {
      gchar *字= g_match_info_fetch(match_info,0);
      了g_print(找到%S \\ N字);
      g_free(字);
      g_match_info_next(match_info,NULL);
    }  g_match_info_free(match_info);
  g_regex_unref(正则表达式);
}诠释的main()
{
  gchar *字符串=我的身体是一个笼子里我的头脑是关键。  print_uppercase_words(字符串);
}

奇怪的是,当我运行巧舌如簧-配置,它并不像该命令,虽然我不知道怎么告诉bash或做如何仅仅使用一个比其他时候它抱怨 gdlib-配置是在这两个包。

  $>巧舌如簧,配置
没有命令'巧舌如簧-配置发现,你的意思是:
 从包命令'gdlib-配置'libgd2-XPM-dev的'(主)
 从包命令'gdlib-配置'libgd2-noxpm-dev的'(主)
巧舌如簧-配置:命令未找到


解决方案

在编译器命令的末尾库:


  

的gcc -I / usr / include目录/巧舌如簧-2.0 -I / usr / lib目录/ x86_64的-Linux的GNU /油嘴-2.0 /包括re.c -o重新-lglib-2.0


链接选项:


-llibrary
-l库
    链接时搜索名为library的库。
    (与库第二种选择作为一个单独的参数
    仅适用于符合POSIX标准,不建议使用。)    这使得其中的命令,你写这个选项是有所不同;
    链接器搜索和处理在库和目标文件
    为了他们指定的。
    因此,`foo.o的-lz文件bar.o文件foo.o的后'搜索库'Z',但
    bar.o.前如果bar.o指`ž功能,这些功能
    可能不会被加载。

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

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.

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

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