I often have linking problems, when I build a project with multiple static libraries. When I am going to link the .o files. Then, I found this problem that is if I change the .o files' order, I won't correctly run this kernel. Later, I searched a lot of materials, and I found this:
Quote:
On Unix-like systems, the traditional behavior of compilers and linkers
is to search for external functions from left to right in the object files
specified on the command line. This means that the object file which
contains the definition of a function should appear after any files which
call that function.
Most current compilers and linkers will search all object files, regardless
of order, but since not all compilers do this it is best to follow the
convention of ordering object files from left to right.
The link instruction are:
Code:
1. ld -nostdlib -T link.lds -o kernel.elf print_amd64.o main.o kernel.o trapa.o trap.o
2. ld -nostdlib -T link.lds -o kernel.elf kernel.o main.o trapa.o trap.o print_amd64.o
objcopy -O binary kernel.elf kernel.bin
dd if=kernel.bin of=boot.img bs=512 count=100 seek=6 conv=notrunc
qemu-system-x86_64 -cpu Skylake-Server -m 512 -hda boot.img -boot d
The first ld instruction is the right and the second one is wrong. In this case, I can certainly change this order of the .o files to make it link correctly. But I use the makefile to compile the huge system containing a lot of files. It is pretty hard to command the makefile to compile each file in order. Do you have any idea to solve this problem?