理解编译命令C和配置代码块

理解编译命令C和配置代码块,c,linux,gcc,codeblocks,allegro5,C,Linux,Gcc,Codeblocks,Allegro5,我在大学里学C。我想尝试新的东西,所以我决定使用allegro游戏库来创建一些东西。我遵循了教程,一切都很顺利。我在文本编辑器中编写了一段代码,执行了教程提供的命令,然后编译并运行了它。(顺便说一句,我在linux上)。 以下是命令: gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags) ./hello 因此,我知道gcc正在调用gcc编译器,hello.c是源代码文件的名称,-o hello指定

我在大学里学C。我想尝试新的东西,所以我决定使用allegro游戏库来创建一些东西。我遵循了教程,一切都很顺利。我在文本编辑器中编写了一段代码,执行了教程提供的命令,然后编译并运行了它。(顺便说一句,我在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 allegro-5 allegro_font-5--libs--cflags) 那么,我应该在代码块中更改什么配置,以便它能够使用allegro库很好地编译代码


顺便说一句,我并没有故意包含源代码,因为它没有给问题添加任何有用的信息,只是让问题变得杂乱无章。

这是一个Bash命令语法功能,它执行另一个命令,然后以字符串形式提供其输出。这里使用这个习惯用法以编程方式向
gcc
命令提供命令行参数。正在执行
pkg config
命令,它返回一个字符串,该字符串成为调用
gcc
的命令字符串的一部分,并以文本形式包含在其中。(当然,它既不知道也不关心它的命令行参数“来自哪里”)

(聪明吧?)

例如,尝试以下方法:

echo$(ls)


您将获得当前目录中所有文件的列表,并以字符串形式连接。

要详细说明@Mike Robinson的答案

$(pkg config allegro-5 allegro\u font-5--libs--cflags)
将执行命令pkg config并替换结果。是编译C程序时提供帮助的构建实用程序。它不是allegro特有的,它被许多C库使用

在这种特殊情况下,调用pkg config时使用参数
allegro-5 allegro_font-5--libs--cflags
,询问pkg config“编译和链接allegro时需要哪些编译器选项(-cflags)和库(-libs)”。allegro由几个模块组成,在本例中,您选择了主模块“allegro”和字体模块“allegro_font”。在这两种情况下,你指的都是allegro的主要版本5

在您的系统上,该命令的结果是
-I/usr/include/x86\u 64-linux-gnu-lallegro\u font-lallegro
。这些选项的含义如下:

-I/usr/include/x86_64-linux-gnu
告诉编译器将路径
/usr/include/x86_64-linux-gnu
添加到头搜索路径(因为allegro头位于那里)


-lallegro_font
和“-lallegro`告诉链接器分别添加allegro_字体和allegro库。这也解释了为什么当你删除它时会出现所有未定义的引用错误——你不再告诉链接器添加allegro库,因此它无法再找到所有这些函数。

我使用了code:blocks中返回的字符串,它最终编译了。我现在很高兴,但我希望了解每个参数的含义“-I/usr/include/x86_64-linux-gnu-lallegro_font-lallegro”