DEV Community

Cover image for Shared Library (Dynamic linking) - It's not about libs
Vinicius Pereira
Vinicius Pereira

Posted on

Shared Library (Dynamic linking) - It's not about libs

This is my first post here so, let's go.

Disclaimer: I won't create expectations with my posts. Everything I share is part of my learning process, which often involves explaining things to others. I found this method to be particularly effective during my time at 42 School. Therefore, I'll be posting about various topics I'm currently learning or have already learned.


Why this post?

I'm actually doing a challenge for a job vacancy and I was struggling with shared objects and I'm doing it in my home, so I remember who hard are don't have another person near you to ask things even if they don't know the answer, they help you to think and find new ways or they have new ideas or even better, they help you to have new ideas and so forth.


First step

This challenge involves creating a shared library in any programming language of my choice.
The library will be tested with a specifically crafted binary file written in C. My goal is to ensure the library functions as intended based on the provided test outputs.


Working on it

During a recent interview, I was presented with a challenge: create a shared library that functions as a CSV processor. Initially, I opted to develop it in Go. It seemed like a straightforward task...

While writing the Go code itself wasn't an issue, the real challenge arose in integrating the library with the C binary. Every attempt resulted in different errors, often related to missing symbols in the generated shared object (.so file).

Fortunately, I eventually found a solution and was able to test the library using the provided binary. However, running the program resulted in a core dump, indicating unexpected behavior.

To address this issue, I opted to switch to C++. The primary reason was the perceived ease of interfacing shared objects created in C++ with C binaries. This approach minimized debugging difficulties and eliminated core dumps.


Final Thoughts

Despite the initial hurdles, I'm still tackling the CSV processor challenge in C++. Having a better understanding of shared objects and dynamic linking (thanks to my previous experience with static libraries) is definitely helpful. However, the initial issues I encountered took a significant amount of time to resolve.

Cya!

Some references to learn about static and dynamic:

Reduce Your Compile Time
Low level learning video I love this channel
Introduction and creation
C Programming

Top comments (4)

Collapse
 
vieirandre profile image
André Vieira

I'm curious about the failure in running the Go approach.

Did you get to debug it? Did you run a backtrace on the dump?

It would be nice to delve into what exactly went wrong. :)

Collapse
 
nivicius profile image
Vinicius Pereira

To be honest, I have a little experience with golang, and when I got this error I just tried to debug the binary, but it was not compiled with debugging flags so in GDB does not appear nothing and I remember that I used valgrind before too but I don't find the error correctly, so I decided to change for c++ that I have more knowledge!

Collapse
 
pauljlucas profile image
Paul J. Lucas

I suspect you had other issues in your C version unrelated to shared libraries, e.g., bad memory management. Creating and using shared libraries is in no way easier in C++ than C.

Collapse
 
nivicius profile image
Vinicius Pereira

I don't tested about leaks yet. I'll try to finish it today/tomorrow. But I'll probably have this kind of issue but I don't manage any kind of memory in this step yet. I'll test the provided binary to verify about leaks!