我已经阅读了下面的链接,通过这个链接我理解了如何创建和使用共享库。 https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
Step 1: Compiling with Position Independent Code
$ gcc -c -Wall -Werror -fpic foo.c
Step 2: Creating a shared library from an object file
$ gcc -shared -o libfoo.so foo.o
Step 3: Linking with a shared library
$ gcc -L/home/username/foo -Wall -o test main.c -lfoo
Step 4: Making the library available at runtime
$ export LD_LIBRARY_PATH=/home/username/foo:$LD_LIBRARY_PATH
$ ./test
This is a shared library test...
Hello, I am a shared library
然而,我有几个问题:
- 在给定的链接中,没有使用dlopen()函数,但是需要使用该函数来打开共享库。没有调用dlopen()函数,这段代码如何工作的?
- 什么时候应该使用dlopen()函数?
- 如果程序具有对某个共享库的函数调用,那么是否可以在没有.so文件的情况下编译程序?
- dlopen()函数是否意味着动态加载,上面链接中的示例(步骤3)是否意味着静态加载? 如果是,则在动态加载的情况下,在链接步骤(步骤3)中是否有任何区别?
提前感谢。