-
-
Save roachsinai/561de80f8f0209eb59b2a12579a70f18 to your computer and use it in GitHub Desktop.
Shared object with rpath set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
cc -shared -fPIC lib_one.c lib_one.h -o libone.so | |
cc -shared -fPIC lib_two.c lib_two.h -o libtwo.so -L. -lone -Wl,-rpath=libs | |
mkdir -p libs | |
mv libone.so libs | |
# -L. 是针对编译时的,使得链接器(ld)可以在 gcc 编译时找到 libtwo.so 让 name loopup 成功 | |
# -rpath=. 是针对运行时的,使得在执行 main 时让动态链接器(ld.so)可以找到 main 依赖的 so | |
# Wl 表示后面的是一个链接器选项(针对 ld,而不是 gcc 的选项) | |
cc main.c -o main -L. -ltwo -Wl,-rpath=. | |
clean: | |
rm -f libone.so libone.a libtwo.so main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void foo(void) | |
{ | |
puts("I'm in foo()"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void foo(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void bar(void) | |
{ | |
puts("I'm in bar()"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bar(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "lib_two.h" | |
int main(void) | |
{ | |
bar(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://unix.stackexchange.com/a/579349/232089