I am having .c and .so file. I tried by using the following compilation: gcc main.c -ldl
. In that .c file i linked to .so file through dlsym()
. How to compile using .so file with .c.
3 Answers
Probably you can do this:
when linking do:
g++ -o prog prog.o -ldllname
If libdllname.so is not in the system directory then add its directory to the library path:
g++ -o prog prog.o -L/path/to/my/library/folder -ldllname

- 3,358
- 3
- 25
- 36

- 849
- 1
- 8
- 12
This is based on your further comments. First guard the declarations of your header file.
#ifndef HEADER_PROTECT
#define HEADER_PROTECT
---------- Here is the content of header
#endif
Next, check in your code, are you defining multiple definitions. Or are you re-defining the standard functions again? Can you please post your code to guide you better? Looks like you have re-defined Close_Comm(), can you check it? Error says that the definition is there in main.c also.
The following is the general way to compile shared object and link it. To compile shared objects.
-g : for debug information
fPIC: for position independent code
$gcc -fPIC -g myfile
The following will create the shared object libmyfile.so
$gcc -shared -o libymyfile.so myfile.o
Now,In order to link it with your main.c.
I assume that the libmyfile.so is in your current path, thus -L./
$gcc main.c -o main.out -L./ -lmyfile
Now, you need to export the LD_LIBRARY_PATH on the bash; in order to execute the binary.
$LD_LIBRARAY_PATH=$LD_LIBRARAY_PATH:./
$./main.out
The dlsym is to load the symbol from the shared object at the run-time. If you want to load the shared object at run time, this can be used. The following is one of the example of dlsym Hack the standard function in library and call the native library function afterwards
-
i am having only .so file and .c file how to link these two im getting error like no such file or directory – Brainy Oct 02 '13 at 04:47
-
If your .so file is in the current directory, then use the following command. $gcc main.c -o main.out -L./ -l
. Remember that if it is libmyfile.so then you have to specify as -lmyfile , no need of specifying lib, also there is no need of extension .so. Now, run it ./main.out before running do the LD_LIBRARAY_PATH set in the bash – dexterous Oct 02 '13 at 04:50 -
In case you are facing still error, just paste the exact error. I will be in a position to tell you the exact issue. It sounds to me a path issue. Because you are unable to link the .so file. – dexterous Oct 02 '13 at 05:10
-
My file name is main.c and crt.so and i tried as you instructed above like $gcc main.c -main.o -L./ -lcrt. And i got error as follows: main.o: In function `Close_Comm': main.c:(.text+0x0): multiple definition of `Close_Comm' /tmp/ccQt5qnj.o:main.c:(.text+0x12f): first defined here crt.o:(.bss+0x0): multiple definition of `NotOpen' /tmp/ccQt5qnj.o:(.bss+0x0): first defined here crt.o: In function `menu': crt.c:(.text+0x77): multiple definition of `menu' /tmp/ccQt5qnj.o:crt284utest.c:(.text+0x171e): first defined here crt.o: In function `Open_Comm': – Brainy Oct 02 '13 at 06:16
-
I feel you should Check this link first. http://www.tune2wizard.com/c-how-main-is-executed/ . It covers about crt part also. In short, your main function gets links to the crt1.s file. No need to links with crt. Just do $gcc main.c -o main.out. Also, can you share the source of library you created, and the source of main.c. – dexterous Oct 02 '13 at 13:23
-
No, it's literally multiple definition of several functions - both in main.c and in .so. The code is wrong, there is nothing to do with gcc or anything. – keltar Oct 03 '13 at 03:21
-
@keltar: Yes, I agree that the code is wrong. The title says "how to link and compile .so file in Linux"? This is a generic question. But later he describes his problem. Of course, he has multiple definitions for these functions Close_Comm,, in main.c ,etc. If he can share the code, we can help him better. – dexterous Oct 03 '13 at 04:10
dlsym()
is used to find a symbol in an open library file.
you first need to use dlopen()
in order to open the file, and only then use dlsym()

- 27,613
- 18
- 81
- 125
-
My file name is main.c and crt.so and i tried as you instructed above like $gcc main.c -main.o -L./ -lcrt. And i got error as follows: main.o: In function Close_Comm': main.c:(.text+0x0): multiple definition of Close_Comm' /tmp/ccQt5qnj.o:main.c:(.text+0x12f): first defined here crt.o:(.bss+0x0): multiple definition of NotOpen' /tmp/ccQt5qnj.o:(.bss+0x0): first defined here crt.o: In function menu': crt.c:(.text+0x77): multiple definition of menu' /tmp/ccQt5qnj.o:crt284utest.c:(.text+0x171e): first defined here crt.o: In function Open_Comm' – Brainy Oct 02 '13 at 08:51