$ gcc -c -o mylib mylib.c In file included from mylib.c:2: mylib.h:3:5: warning: conflicting types for built-in function ‘printf’; expected ‘int(const char *, ...)’ [-Wbuiltin-declaration-mismatch] 3 | int printf(const char* s1); | ^~~~~~ mylib.h:1:1: note: ‘printf’ is declared in header ‘<stdio.h>’ +++ |+#include <stdio.h> 1 | // #define __NATIVE_USE_MYLIB__
luyoung at luyoung-desktop in ~/Test/test6 $ gcc -o main main.c mylib In file included from main.c:1: mylib.h:3:5: warning: conflicting types for built-in function ‘printf’; expected ‘int(const char *, ...)’ [-Wbuiltin-declaration-mismatch] 3 | int printf(const char* s1); | ^~~~~~ mylib.h:1:1: note: ‘printf’ is declared in header ‘<stdio.h>’ +++ |+#include <stdio.h> 1 | // #define __NATIVE_USE_MYLIB__
luyoung at luyoung-desktop in ~/Test/test6 $ ./main hello.
$ gcc -c -o mylib mylib.c In file included from mylib.c:2: mylib.h:3:5: warning: conflicting types for built-in function ‘printf’; expected ‘int(const char *, ...)’ [-Wbuiltin-declaration-mismatch] 3 | int printf(const char* s1); | ^~~~~~ mylib.h:1:1: note: ‘printf’ is declared in header ‘<stdio.h>’ +++ |+#include <stdio.h> 1 | #define __NATIVE_USE_MYLIB__
luyoung at luyoung-desktop in ~/Test/test6 $ gcc -o main main.c mylib In file included from main.c:1: mylib.h:3:5: warning: conflicting types for built-in function ‘printf’; expected ‘int(const char *, ...)’ [-Wbuiltin-declaration-mismatch] 3 | int printf(const char* s1); | ^~~~~~ mylib.h:1:1: note: ‘printf’ is declared in header ‘<stdio.h>’ +++ |+#include <stdio.h> 1 | #define __NATIVE_USE_MYLIB__
luyoung at luyoung-desktop in ~/Test/test6 $ ./main
为了让 ioe 健壮执行,必须让它去选择链接 glibc 中的函数而不是我们的 klib,那么 am 是怎么解决这个问题的呢?它使用了动态加载库dl 中的函数来控制链接行为,就是上文描述的那样。
1 2 3 4 5 6 7
staticvoidinit_platform() { ... // use dynamic linking to avoid linking to the same function in RT-Thread int (*ftruncate_libc)(int, off_t) = dlsym(RTLD_NEXT, "ftruncate"); ... }