如何编译官方ls.c源代码的sorcecode?

qib*_*bra 3 compiling coreutils

我下载了官方ls.c源码并尝试用gcc编译。问题是 gcc 向我显示了很多错误。

$ gcc ls.c -o ls

ls.c:38:20: fatal error: config.h:
Run Code Online (Sandbox Code Playgroud)

我确实通过使用 find 命令在我的字典中搜索来修复了很多错误

$ find /usr/src/ -name "nameofthelibary" 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

对于某些库,我找到了正确的路径,因此例如我可以将它们放入#include <config.h> => #include </usr/src/linuxheader.../config.h> 我提到的此方法适用于某些库,但不适用于标头#include "die.h"

所以我现在的问题是:有什么方法可以编译这个 ls.c 吗?cz 我想通过修改它来了解它是如何工作的

Oli*_*Oli 9

ls是 coreutils 包的一部分,因此取决于 coreutils 中的其他文件以及配置步骤。您可能会花很长时间从图书馆中解开它,但除非您计划每天编译数百次,否则我认为这是不值得的。

正如 pim 在评论中所说,您可以从git 树根目录中的文件了解README.hacking完整的构建过程。我运行了以下内容,但没有真正给予太多关注,但这对我有用。

sudo apt install git build-essential
sudo apt build-dep coreutils

git clone git://git.savannah.gnu.org/coreutils.git
cd coreutils

./bootstrap  # grabs submodules, sets up configuration
./configure  # does actual compiler configuration

make clean  # remove old attempts
make -j8  # compile using 8 threads (you might want to alter this)
Run Code Online (Sandbox Code Playgroud)

这将编译src目录中的所有内容并将二进制文件保留在那里。您可以运行新编译ls./src/ls. 您可以进行更改,只要它们是轻量级的,您就可以仅通过步骤重新编译它们make

  • `make -j $(nproc)` 通常是一个很好的默认值 - nproc 打印可用逻辑 cpu 核心的数量 (3认同)