5

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 3

6

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 
Sign up to request clarification or add additional context in comments.

Comments

2

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

7 Comments

i am having only .so file and .c file how to link these two im getting error like no such file or directory
If your .so file is in the current directory, then use the following command. $gcc main.c -o main.out -L./ -l<libraryfile>. 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
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.
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':
I feel you should Check this link first. 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.
|
0

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()

1 Comment

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'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.