Tôi có một số câu hỏi về thứ tự liên kết gcc. Người đàn ông GCC nói các biểu tượng tìm kiếm liên kết từ trái sang phải mà không cần tìm kiếm lặp lại theo mặc định. Đây là thử nghiệm của tôi:một số câu hỏi về thứ tự tìm kiếm của GCC linker
main.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("HELLO WROLD\n");
return 0;
}
printf.c
#include <stdio.h>
#include <stdlib.h>
int printf(const char *fmt, ...)
{
write(1, "AAA\n", 4);
}
[[email protected] testcode]# gcc -c -fno-builtin-printf *.c
[[email protected] testcode]# gcc -o test main.o printf.o
[[email protected] testcode]# ./test
AAA
[[email protected] testcode]# gcc -o test printf.o main.o
[[email protected] testcode]# ./test
AAA
[[email protected] testcode]# ar rcs libprintf.a printf.o
[[email protected] testcode]# gcc -o test libprintf.a main.o
[[email protected] testcode]# ./test
HELLO WROLD
[[email protected] testcode]# gcc -o test main.o libprintf.a
[[email protected] testcode]# ./test
AAA
[[email protected] testcode]# gcc -shared -o libprintf.so printf.o
[[email protected] testcode]# gcc -o test libprintf.so main.o
[[email protected] testcode]# export LD_LIBRARY_PATH=.
[[email protected] testcode]# ./test
AAA
[[email protected] testcode]# gcc -o test main.o libprintf.so
[[email protected] testcode]# ./test
AAA
Từ kết quả, chúng ta có thể thấy thứ tự của .o và .o, .o và .so làm cho không có sự khác biệt, chỉ có trật tự của .o và .a có hiệu lực. Nhưng điều đó không phù hợp với trang người dùng gcc. Vậy tại sao?
Tôi đã sử dụng -v, nhưng tôi vẫn không hiểu tại sao. Bạn có thể giải thích chi tiết điều này không? – D3Hunter
Tôi không thể sử dụng -nodefaultlibs, đối với một số chức năng trong crt như _start nên tồn tại. – D3Hunter
Tôi đoán tôi TL, DR-ed trước đây. Theo cách nào bạn nghĩ 'nó' không phù hợp với trang gcc amn? – sehe