0

I have been provided a dynamic library (.so file) generated in the Linux platform. The file is compiled using the below command.

gcc -c -Wall -Werror -fpic -o factorial.o factorial.c
gcc -shared -o libFactorial.so factorial.o 

Now, I have written a C program to call some methods inside the library (.so file) and get the desired output. The dynamic library is kept in the same directory of my C program. I need to link the library with my C program at runtime so that I can debug my C program using Visual Studio Code.

Compiler: gcc
IDE: Visual Studio Code
Platform: Linux
C Program: my_C_Program.c
Dynamic Library : libFactorial.so

When I compile and run my code using the command prompt, I am able to call the methods inside the .so file.

Commands

gcc my_C_Program.c -ldl -o  my_C_Program   
./my_C_Program -l libFactorial

But when I debug my code using visual studio code, I am getting errors. Do I need to link the library (statically probably ?) with my c program so that it recognizes my library. I don't know how to do that. Do I need to create a MakeFile? If yes, can you please advise how to link a library using that?

Error while debugging:

undefined reference to `dlerror.'
undefined reference to `dlsym.'
undefined reference to `dlerror.'

Please note that I don't have to generate the dynamic library. It is provided by others.

Dev136
  • 1
  • 2
  • 2
    I think you may need to use LD_LIBRARY_PATH. Study this here: https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html. – Gabriel Staples Dec 04 '21 at 14:57
  • 2
    The library must be compiled with debug information `-g` which it isn't – bolov Dec 04 '21 at 15:07
  • Consider reading carefully all of [ld.so(8)](https://man7.org/linux/man-pages/man8/ld.so.8.html), and [dlopen(3)](https://man7.org/linux/man-pages/man3/dlopen.3.html), [environ(7)](https://man7.org/linux/man-pages/man7/environ.7.html) and [Program Library Howto](https://tldp.org/HOWTO/Program-Library-HOWTO/) and [C++ dlopen minihowto](https://tldp.org/HOWTO/html_single/C++-dlopen/) and the documentation of [GCC](http://gcc.gnu.org/). See also the C++ code of [RefPerSys](http://refpersys.org/) and of [Qt](https://qt.io/) – Basile Starynkevitch Dec 04 '21 at 15:14
  • Do you call dlopen/dlsym in your main program? Or perhaps they are necessary in the mathematical calculation performed by libFactorial.so? Anyways, try this: `gcc -g my_C_Program.c -L. -lFactorial "-Wl,-rpath=${PWD}" -ldl -o my_C_Program; ./my_C_Program` – Lorinczy Zsigmond Dec 04 '21 at 17:00
  • @Lorinczy Zsigmond, I do call dlopnen/ dlsym from my main program. The shared library has some function, lets say, func_1() that computes the factorial of a given number. The given number I provide from my main program. Now, when I compile the C program using the command line in the terminal of Visual Studio Code, I am able to call successfully the method inside the Shared Object (.so) and get the result. Now I need to debug the main C program. When I put breakpoint and press F5, I gets error that dlopen(), dlsym() are undefined. So, I need to configure VS Code so that it can debug my code. – Dev136 Dec 04 '21 at 19:14
  • @bolov, I tried with compiling with -g option and generated the .so file. The error still exists when I try to debug the code in VS Code. – Dev136 Dec 04 '21 at 19:18
  • Do you need to turn off optimization with `-O0` maybe, as I explain here? https://stackoverflow.com/a/63386263/4561887 Or maybe not--I think that is the default level if not specified. – Gabriel Staples Dec 04 '21 at 23:36
  • The solution to my problem was simple but I could not find that so far. Thank you all for your valuable advice. I just had to add two-parameter into my task.jason file. "args": [ "-fdiagnostics-color=always", "-ldl" ], – Dev136 Dec 05 '21 at 14:51

0 Answers0