我在大学里学C。我想尝试一些新的东西,所以我决定用快板游戏库来创造一些东西。我遵守了教程,一切都很顺利。我在文本编辑器中编写了一段代码,并执行了教程提供的命令,并编译并运行了这些命令。(我在linux上)。以下是命令:
gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags)
./hello因此,我知道gcc正在调用gcc编译器,hello.c是源代码文件的名称,-o hello指定编译文件的名称。但下一部分是模棱两可的,超出我所知的:
$(pkg-config allegro-5 allegro_font-5 --libs --cflags)因此,任何人都能解释它的含义(这与链接有关)。如果删除该部分,编译器将返回以下错误代码:
/usr/bin/ld: /tmp/ccIQsEis.o: in function `main':
hello.c:(.text+0x2f): undefined reference to `al_install_system'
/usr/bin/ld: hello.c:(.text+0x34): undefined reference to `al_install_keyboard'
/usr/bin/ld: hello.c:(.text+0x45): undefined reference to `al_create_timer'
/usr/bin/ld: hello.c:(.text+0x4e): undefined reference to `al_create_event_queue'
/usr/bin/ld: hello.c:(.text+0x61): undefined reference to `al_create_display'
/usr/bin/ld: hello.c:(.text+0x6a): undefined reference to `al_create_builtin_font'
/usr/bin/ld: hello.c:(.text+0x73): undefined reference to `al_get_keyboard_event_source'
/usr/bin/ld: hello.c:(.text+0x85): undefined reference to `al_register_event_source'
/usr/bin/ld: hello.c:(.text+0x91): undefined reference to `al_get_display_event_source'
/usr/bin/ld: hello.c:(.text+0xa3): undefined reference to `al_register_event_source'
/usr/bin/ld: hello.c:(.text+0xaf): undefined reference to `al_get_timer_event_source'
/usr/bin/ld: hello.c:(.text+0xc1): undefined reference to `al_register_event_source'
/usr/bin/ld: hello.c:(.text+0xd4): undefined reference to `al_start_timer'
/usr/bin/ld: hello.c:(.text+0xe7): undefined reference to `al_wait_for_event'
/usr/bin/ld: hello.c:(.text+0x125): undefined reference to `al_is_event_queue_empty'
/usr/bin/ld: hello.c:(.text+0x13d): undefined reference to `al_map_rgb'
/usr/bin/ld: hello.c:(.text+0x16a): undefined reference to `al_clear_to_color'
/usr/bin/ld: hello.c:(.text+0x17e): undefined reference to `al_map_rgb'
/usr/bin/ld: hello.c:(.text+0x1ca): undefined reference to `al_draw_text'
/usr/bin/ld: hello.c:(.text+0x1cf): undefined reference to `al_flip_display'
/usr/bin/ld: hello.c:(.text+0x1e7): undefined reference to `al_destroy_font'
/usr/bin/ld: hello.c:(.text+0x1f3): undefined reference to `al_destroy_display'
/usr/bin/ld: hello.c:(.text+0x1ff): undefined reference to `al_destroy_timer'
/usr/bin/ld: hello.c:(.text+0x20b): undefined reference to `al_destroy_event_queue'
collect2: error: ld returned 1 exit status对于问题的第二部分,我试着用代码:块编译这段代码,但是它返回编译器给出的相同错误,没有术语:$(pkg-config快板-5快板字体-5-libs-c标志)所以,我应该在代码块中更改什么样的配置,这样它才能使用快板库编译好代码。
PS。我没有故意地包含源代码,因为它没有为这个问题添加任何有用的信息,只是把它弄得乱七八糟。
发布于 2021-04-20 00:53:35
这是一个Bash命令语法特性,它执行另一个命令,然后以字符串的形式提供其输出。这里使用这个成语以编程方式向gcc命令提供命令行参数。正在执行pkg-config命令,它返回一个字符串,该字符串成为调用gcc的命令字符串的一部分-是文本包含的。(当然,它既不知道也不关心它的命令行参数“来自”哪里)。
(聪明,是吧?)
例如,尝试如下:
echo $(ls)
您将获得当前目录中所有文件的列表,并以字符串的形式连接。
发布于 2021-04-28 07:41:39
为了详细说明迈克罗宾逊的回答,
$(pkg-config allegro-5 allegro_font-5 --libs --cflags)将执行pkg-config命令并替换结果。pkg-config是帮助编译C程序的构建实用程序。它不是专用于快板,它被许多C库所使用。
在这种情况下,使用参数allegro-5 allegro_font-5 --libs --cflags调用pkg-config,其中询问pkg-config“在编译和链接快板时我需要哪些编译器选项(-c威)和库(-libs)?”快板由几个模块组成,在这种情况下,你选择了主模块‘快板’和字体模块‘快板_字体’。在这两种情况下,你指的是主要版本5的快板。
在您的系统上,命令的结果是-I/usr/include/x86_64-linux-gnu -lallegro_font -lallegro。这些选择的含义如下:
-I/usr/include/x86_64-linux-gnu告诉编译器将路径/usr/include/x86_64-linux-gnu添加到标题搜索路径(因为快板标题位于那里)
-lallegro_font和'-lallegro`‘分别告诉链接器添加allegro_font库和快板库。这也解释了为什么当你删除它时,你会得到所有那些未定义的引用错误--你不再告诉链接器添加快板库,所以它再也找不到所有这些函数了。
https://stackoverflow.com/questions/67171023
复制相似问题