Skip to content

Instantly share code, notes, and snippets.

@roachsinai
Forked from ardrabczyk/Makefile
Last active March 15, 2024 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roachsinai/561de80f8f0209eb59b2a12579a70f18 to your computer and use it in GitHub Desktop.
Save roachsinai/561de80f8f0209eb59b2a12579a70f18 to your computer and use it in GitHub Desktop.
Shared object with rpath set
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
#include <stdio.h>
void foo(void)
{
puts("I'm in foo()");
}
void foo(void);
#include <stdio.h>
void bar(void)
{
puts("I'm in bar()");
}
void bar(void);
#include <stdio.h>
#include <stdlib.h>
#include "lib_two.h"
int main(void)
{
bar();
return EXIT_SUCCESS;
}
@roachsinai
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment