0

Say I have libA and libB, both needs zlib 1.2.11, and both are compiled as static lib. my understanding is that static lib just put all .o files together. as such, both libA and libB will need zutil.c.o to resolve symbols in their .a files. Now I am trying to build another project that relies on both libA and libB, and that should generate a .so file. so during the linkage, the linker will find both zutil.c.o through both libA and libB, if they point to different zutil.c.o, then error of multiple defintions of zlibs functions will occur.

The reason I am asking is that, I got this kind error when trying to build a project. But the weird part is that, with the same conanfile.py, but with different building environment i got different results. I am under ubuntu 18.04, I have conan installed only for system python3. my project uses pybind11, so i have system's own python3 and python3-dev installed (e.g., in /usr/lib/x86_64-linux-gnu/libpython3.7m.so), I also installed conda, which has its own python3 and python3-dev installed (e.g., in /home/username/anaconda3/lib/libpython3.7m.so), I have all conan packages that are needed installed, and they are the same for all the following building process, my conan profile sets compiler version of gcc to 9, and here is what happens.

  1. if I build in conda's python3 env, I can conan build the project without any problem, note that my conda comes with gcc 7.3, so i have to set(CONAN_DISABLE_CHECK_COMPILER TRUE) to build.
  2. If I build with conda's python3 env, and export CXX=/usr/lib/ccache/g++ so that system's gcc is used (actually I am using ccache here), i dont need to set(CONAN_DISABLE_CHECK_COMPILER TRUE), then i got the following error:

/usr/bin/ld: cannot find /lib/libpthread.so.0 /usr/bin/ld: cannot find /usr/lib/libpthread_nonshared.a collect2: error: ld returned 1 exit status

  1. if i build in system's python3 env, and my system has gcc 9.3, i got the following error:

/home/username/.conan/data/zlib/1.2.11///package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libz.a(zutil.o): In function zlibVersion': zutil.c:(.text+0x0): multiple definition of zlibVersion' /home/username/.conan/data/pcl/1.11.0///package/a4ccc62deb773be50caa050a2fb7f7e769a17a54/lib/libpcl_surface.a(zutil.c.o):zutil.c:(.text+0x0): first defined here /home/username/.conan/data/zlib/1.2.11///package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libz.a(zutil.o): In function zlibCompileFlags': zutil.c:(.text+0x10): multiple definition of zlibCompileFlags' /home/username/.conan/data/pcl/1.11.0///package/a4ccc62deb773be50caa050a2fb7f7e769a17a54/lib/libpcl_surface.a(zutil.c.o):zutil.c:(.text+0x10): first defined here /home/username/.conan/data/zlib/1.2.11///package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libz.a(zutil.o):(.data.rel.ro.local+0x0): multiple definition of `z_errmsg' /home/username/.conan/data/pcl/1.11.0///package/a4ccc62deb773be50caa050a2fb7f7e769a17a54/lib/libpcl_surface.a(zutil.c.o):(.data.rel.ro.local+0x0): first defined here collect2: error: ld returned 1 exit status

If I don't use conan build, just use the cmake file generated by conan and build by cmake then it is all the same multiple definition error for all cases. What I am mis-unerstanding here? or did I miss anything?

shelper
  • 10,053
  • 8
  • 41
  • 67
  • 1
    "my understanding that static lib just put all .o files together. as such, both libA and libB will have something like zutil.c.o in their .a files." - A static library is actually a bunch of object files (`.o`). But these object files are never become part of other static library. The record `libpcl_surface.a(zutil.c.o)` means that `libpcl_surface` has been **compiled from** given object file. This can NOT be interpreted as `libpcl_surface` has been **linked** with other library which contains that object file. – Tsyvarev Dec 18 '20 at 16:50
  • so basically in `libpcl_surface` all it has for `zutil.c.o` is that: i have this symbol which is from `zutil.c.o`, go look for this file so you resolve the symbol. right? – shelper Dec 18 '20 at 17:32

1 Answers1

0

I think just figured out what causes this multiple definition. I have this line in my cmakelists.txt file target_link_libraries(${TARGET} PUBLIC ${PYTHON_LIBRARIES}) and this one looks for python static libs, and thus will link to the zlib dependency for python itself. This differs and thus conflicts with the one installed by conan.

When building using conda's python, for some reason maybe the version of zlib in conda' python build it uses the same version of zlib.

The pthread missing thing is due to some cxx complier flags setted by conda when activating its python environment.

shelper
  • 10,053
  • 8
  • 41
  • 67