使用 main() 创建共享库
Creating a shared library with main()
在 glibc (and also EGLIBC 我相信),libc.so
库有一个 main()
方法:
$ /lib/i386-linux-gnu/libc.so.6
GNU C Library (Debian GLIBC 2.19-18+deb8u1) stable release version 2.19, by Roland McGrath et al.
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.8.4.
Compiled on a Linux 3.16.7 system on 2015-08-30.
Available extensions:
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.debian.org/Bugs/>.
现在我想对以下简约示例做同样的事情(即我希望我自己的 SO 也有一个 main()
):
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
链接为常规可执行文件:
$ gcc -g -O0 -Wall -c test.c
$ gcc -o test test.o
$ ./test
Hello, World!
现在链接为共享对象:
$ gcc -g -O0 -Wall -c test.c
$ gcc -shared -o libtest.so test.o
$ ./libtest.so
Segmentation fault
nm
表示存在 main
符号 (000004f5 T main
)
- 调试时,
gdb
不显示任何有意义的回溯。
- 在 gcc 命令行中添加
-fpic
或 -fPIC
没有帮助。
我做错了什么?
the libc.so library has a main() method
不是,gcc的C库实现有一个入口点,不是主符号。
您可以找到如何执行此操作的示例 here。
在 glibc (and also EGLIBC 我相信),libc.so
库有一个 main()
方法:
$ /lib/i386-linux-gnu/libc.so.6
GNU C Library (Debian GLIBC 2.19-18+deb8u1) stable release version 2.19, by Roland McGrath et al.
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.8.4.
Compiled on a Linux 3.16.7 system on 2015-08-30.
Available extensions:
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.debian.org/Bugs/>.
现在我想对以下简约示例做同样的事情(即我希望我自己的 SO 也有一个 main()
):
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
链接为常规可执行文件:
$ gcc -g -O0 -Wall -c test.c
$ gcc -o test test.o
$ ./test
Hello, World!
现在链接为共享对象:
$ gcc -g -O0 -Wall -c test.c
$ gcc -shared -o libtest.so test.o
$ ./libtest.so
Segmentation fault
nm
表示存在main
符号 (000004f5 T main
)- 调试时,
gdb
不显示任何有意义的回溯。 - 在 gcc 命令行中添加
-fpic
或-fPIC
没有帮助。
我做错了什么?
the libc.so library has a main() method
不是,gcc的C库实现有一个入口点,不是主符号。
您可以找到如何执行此操作的示例 here。