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