Editing LAMMPS as a shared library

Hello,

I have no issues building LAMMPS as a shared library and coupling it with C++ and python, but I don't understand why editing the LAMMPS source code and then building as a shared library doesn't yield any changes.

For example, if I edit the source code and build LAMMPS normally (ex. "make serial") then I will observe the changes I made in the source code when I run an input script with "./lmp_serial < in.test".

But if I edit the source code and build LAMMPS as a shared library (ex. "make mode=shlib ompi_g++") then I don't observe any changes I made in the source code after I compile my C++ code, which runs LAMMPS, and then run LAMMPS through the C++ code. It's as if I never edited the LAMMPS source code in the first place.

Is there something I'm missing about building LAMMPS as a shared library?

Thanks for your time.

Hello,

I have no issues building LAMMPS as a shared library and coupling it with C++ and python, but I don’t understand why editing the LAMMPS source code and then building as a shared library doesn’t yield any changes.

For example, if I edit the source code and build LAMMPS normally (ex. “make serial”) then I will observe the changes I made in the source code when I run an input script with “./lmp_serial < in.test”.

But if I edit the source code and build LAMMPS as a shared library (ex. “make mode=shlib ompi_g++”) then I don’t observe any changes I made in the source code after I compile my C++ code, which runs LAMMPS, and then run LAMMPS through the C++ code. It’s as if I never edited the LAMMPS source code in the first place.

Is there something I’m missing about building LAMMPS as a shared library?

How does your C++ code “find” the shared library? How do you link with it?

in addition to that, is the build successful? do you see in the makefile the file you changed being recompiled?

Yes the build is successful, the changed LAMMPS file is recompiled and I observe a change when I build an executable and run it.

I don’t observe a change, however, when I build a shared library and run it from C++. It’s as if I never even changed the source code.

I realized the issue thanks to your question Axel. It’s not enough to just have the shared library in the same directory that you are compiling. I had older shared libraries that I compiled and placed in /usr/lib where my computer links all the shared libraries to during runtime of a program, so my C++ code was reading those libraries. I needed to link the new shared libraries with my code during runtime, or place them in /usr/lib.

I wasn’t as up to date on linking shared libraries as I should be… but the issue is fixed now.

Thanks

I realized the issue thanks to your question Axel. It's not enough to just
have the shared library in the same directory that you are compiling. I had
older shared libraries that I compiled and placed in /usr/lib where my
computer links all the shared libraries to during runtime of a program, so
my C++ code was reading those libraries. I needed to link the new shared
libraries with my code during runtime, or place them in /usr/lib.

setting LD_LIBRARY_PATH should do the trick as well.

I wasn't as up to date on linking shared libraries as I should be... but the
issue is fixed now.

well, you have just discovered why it is not necessarily a good idea
to link to a shared library.
for what you describe, it would likely be much better to link the
LAMMPS library statically. no runtime dependency, no version skew, no
problem.
would become even more of a problem, if there are libraries that are
linked to the LAMMPS shared library and but have the same name or
colliding symbols with libraries in your executable.

axel.