multiscale / muscle3

The third major version of the MUltiScale Coupling Library and Environment
Apache License 2.0
25 stars 13 forks source link

Missing linker dependency to pthread in pkgconfig files #96

Closed msebregts closed 2 years ago

msebregts commented 2 years ago

When compiling and linking a muscle program in two steps, the linker doesn't recognize it needs to link against pthread:

Reproduction:

  1. Build & install libmuscle (I took the develop branch)
  2. Set the PKG_CONFIG_PATH to include /lib/pkgconfig
  3. Navigate to the cpp examples directory: cd docs/source/examples/cpp
  4. Building and linking an example in one command is working fine: g++ `pkg-config --cflags libmuscle ymmsl` reaction.cpp `pkg-config --libs libmuscle ymmsl`
  5. Splitting it in two steps (one for building, one for linking) fails: g++ `pkg-config --cflags libmuscle ymmsl` -o reaction.o -c reaction.cpp g++ reaction.o -o reaction `pkg-config --libs libmuscle ymmsl` Outputs a failure: /usr/bin/ld: <install_dir>/lib/libmuscle.so: undefined reference to `pthread_create'

Suggested solution:

Add -lpthread to the Libs section in the various libmuscle*.pc files

LourensVeen commented 2 years ago

It looks like the problem is that libmuscle.so and libmuscle_mpi.so aren't built with -pthread. As a result, they don't link with libpthread, and so pthread_create isn't found. MUSCLE uses threads internally in the communication part, so it has a private dependency on pthread, but the model built against libmuscle shouldn't need -pthread unless it uses threads itself.

So I think the solution is to build the shared libraries with -pthread, and to add some clauses to the pkg-config files that add -pthread to the flags and the libs when linking statically (because a .a static library doesn't contain dependency links). That way pthread should be available exactly when needed.

LourensVeen commented 2 years ago

Released in 0.5.0.