makefile 在 Mac 中使用了编译程序,在 it.what 中使用了交叉编译器标志,它们的意思是如果我不使用交叉编译器,则选择 4 标志
makefile used compile program in Mac and cross compiler flags it uses in it.what they means & alternative 4 the flags if I am not using cross-compiler
我有一个简单的 OS 和从 https://github.com/FRosner/FrOS/tree/minimal-c-kernel
下载的引导加载程序代码
教程位于 https://dev.to/frosnerd/writing-my-own-boot-loader-3mld
我对 make 文件有一些疑问
在下面的makefile中什么是x86_64-elf-ld
,ld我相信是link目标文件和库到输出.a和bin文件,它们是库。所以我相信要输出一个名为 kernel.bin
的库,作者使用了 ld 的 -o
标志,因为它的 linker 然后输出是 .bin 文件。我想知道我可以从 ld 程序输出 .o 文件(通过仅使用 ld 程序我可以输出 .o 二进制可执行文件,如果不能,那么我假设它是一个 linker 而不是编译器并且编译器只能输出可执行文件/ or self executable with main() and linker can output only library which needed by some other executable code to call its functions)
注意到的第一行是
kernel.bin: kernel_entry.o kernel.o
x86_64-elf-ld -m elf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary
由于作者在 Mac 计算机上构建程序并希望从在 x86-64 架构上运行的代码输出二进制文件,因此他需要 x86-64
linker 即 x86_64-elf-ld
并且可以轻松安装在 Mac 上。但是我没有 Mac,我有 x86-64 计算机,那么我可以只使用 ld
而不是跨平台 ld
,它在作者计算机上是 x86_64-elf-ld
也是什么 -m
旗帜? -m
是一个选项,elf_i386
是选项 -m
的值吗,谁能澄清一下。还是它们(-m 和 elf_i386 是两个不同的东西,如果是,那么它们是什么意思?)并且带有标志 -Ttext
我可以指定输出文件的地址吗?那么上面两行的确切含义以及那里的标志
这是下一个
kernel_entry.o: kernel_entry.asm
nasm $< -f elf -o $@
上面两行的-f
flag是什么,elf是什么?在其输出 .o $@ target
文件的上方,该文件是可执行的。使用 NASM 汇编编译器
所以在 makefile 的其余部分,-m32,-b,-f,-ffreestanding,-fda
标志是什么,xxd 程序是什么?
这些问题太多了,或者有人只是将我引导到某个页面,我不想迷失在几十个旗帜中。可以覆盖这些标志的简洁页面。或者,如果有人花时间解释它们,那么非常感谢
# $@ = target file
# $< = first dependency
# $^ = all dependencies
# First rule is the one executed when no parameters are fed to the Makefile
all: run
# Notice how dependencies are built as needed
kernel.bin: kernel_entry.o kernel.o
x86_64-elf-ld -m elf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary
kernel_entry.o: kernel_entry.asm
nasm $< -f elf -o $@
kernel.o: kernel.c
x86_64-elf-gcc -m32 -ffreestanding -c $< -o $@
# Disassemble
kernel.dis: kernel.bin
ndisasm -b 32 $< > $@
mbr.bin: mbr.asm
nasm $< -f bin -o $@
os-image.bin: mbr.bin kernel.bin
cat $^ > $@
run: os-image.bin
qemu-system-i386 -fda $<
echo: os-image.bin
xxd $<
clean:
$(RM) *.bin *.o *.dis
now the only question I have is why I have to resort to using
x86_64-elf-ld linker and x86_64-elf-gcc compiler since the author used
them because he was not developinh makefile and program on x86-64. But
why I have to use them. So question actually is now: can I use only
gcc instead of x86_64-elf-gcc cross compiler and can I use ld as
linker instead of x86_64-elf-ld cross platform linker.
简而言之,建议在开发os时使用cross编译器,因为它:
allows you to leave the current operating system behind, meaning that
no headers or libraries of your host operating system will be used.
You need a cross-compiler for operating system development, otherwise
a lot of unexpected things can happen because the compiler assumes
that your code is running on your host operating system.
有关详细信息,请参阅:
因此,即使你在x86-64
上,仍然建议使用cross编译器,因为它会为你省去很多麻烦。
至于链接器,在 osdev.org 他们建议:
Linking with your compiler rather than ld
You shouldn't be invoking ld directly. Your cross-compiler is able to
work as a linker and using it as the linker allows it control at the
linking stage. This control includes expanding the -lgcc to the full
path of libgcc that only the compiler knows about. If you get weird
errors during compilation, use your cross-compiler for linking and it
may go away. If you do need ld, be sure to use the cross-linker
(i686-elf-ld) rather than the system linker.
我有一个简单的 OS 和从 https://github.com/FRosner/FrOS/tree/minimal-c-kernel
下载的引导加载程序代码教程位于 https://dev.to/frosnerd/writing-my-own-boot-loader-3mld
我对 make 文件有一些疑问
在下面的makefile中什么是x86_64-elf-ld
,ld我相信是link目标文件和库到输出.a和bin文件,它们是库。所以我相信要输出一个名为 kernel.bin
的库,作者使用了 ld 的 -o
标志,因为它的 linker 然后输出是 .bin 文件。我想知道我可以从 ld 程序输出 .o 文件(通过仅使用 ld 程序我可以输出 .o 二进制可执行文件,如果不能,那么我假设它是一个 linker 而不是编译器并且编译器只能输出可执行文件/ or self executable with main() and linker can output only library which needed by some other executable code to call its functions)
注意到的第一行是
kernel.bin: kernel_entry.o kernel.o
x86_64-elf-ld -m elf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary
由于作者在 Mac 计算机上构建程序并希望从在 x86-64 架构上运行的代码输出二进制文件,因此他需要 x86-64
linker 即 x86_64-elf-ld
并且可以轻松安装在 Mac 上。但是我没有 Mac,我有 x86-64 计算机,那么我可以只使用 ld
而不是跨平台 ld
,它在作者计算机上是 x86_64-elf-ld
也是什么 -m
旗帜? -m
是一个选项,elf_i386
是选项 -m
的值吗,谁能澄清一下。还是它们(-m 和 elf_i386 是两个不同的东西,如果是,那么它们是什么意思?)并且带有标志 -Ttext
我可以指定输出文件的地址吗?那么上面两行的确切含义以及那里的标志
这是下一个
kernel_entry.o: kernel_entry.asm
nasm $< -f elf -o $@
上面两行的-f
flag是什么,elf是什么?在其输出 .o $@ target
文件的上方,该文件是可执行的。使用 NASM 汇编编译器
所以在 makefile 的其余部分,-m32,-b,-f,-ffreestanding,-fda
标志是什么,xxd 程序是什么?
这些问题太多了,或者有人只是将我引导到某个页面,我不想迷失在几十个旗帜中。可以覆盖这些标志的简洁页面。或者,如果有人花时间解释它们,那么非常感谢
# $@ = target file
# $< = first dependency
# $^ = all dependencies
# First rule is the one executed when no parameters are fed to the Makefile
all: run
# Notice how dependencies are built as needed
kernel.bin: kernel_entry.o kernel.o
x86_64-elf-ld -m elf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary
kernel_entry.o: kernel_entry.asm
nasm $< -f elf -o $@
kernel.o: kernel.c
x86_64-elf-gcc -m32 -ffreestanding -c $< -o $@
# Disassemble
kernel.dis: kernel.bin
ndisasm -b 32 $< > $@
mbr.bin: mbr.asm
nasm $< -f bin -o $@
os-image.bin: mbr.bin kernel.bin
cat $^ > $@
run: os-image.bin
qemu-system-i386 -fda $<
echo: os-image.bin
xxd $<
clean:
$(RM) *.bin *.o *.dis
now the only question I have is why I have to resort to using x86_64-elf-ld linker and x86_64-elf-gcc compiler since the author used them because he was not developinh makefile and program on x86-64. But why I have to use them. So question actually is now: can I use only gcc instead of x86_64-elf-gcc cross compiler and can I use ld as linker instead of x86_64-elf-ld cross platform linker.
简而言之,建议在开发os时使用cross编译器,因为它:
allows you to leave the current operating system behind, meaning that no headers or libraries of your host operating system will be used. You need a cross-compiler for operating system development, otherwise a lot of unexpected things can happen because the compiler assumes that your code is running on your host operating system.
有关详细信息,请参阅:
因此,即使你在x86-64
上,仍然建议使用cross编译器,因为它会为你省去很多麻烦。
至于链接器,在 osdev.org 他们建议:
Linking with your compiler rather than ld
You shouldn't be invoking ld directly. Your cross-compiler is able to work as a linker and using it as the linker allows it control at the linking stage. This control includes expanding the -lgcc to the full path of libgcc that only the compiler knows about. If you get weird errors during compilation, use your cross-compiler for linking and it may go away. If you do need ld, be sure to use the cross-linker (i686-elf-ld) rather than the system linker.