Cmake build link failure on darwin but not on linux

I have a flake which provides a development environment for a project written in C++.

The project uses cmake as its build system.

The project compiles without problems on Linux. On Darwin, when cmake is linking the project’s own shared library against that of a dependency, it fails to find symbols defined by the dependency.

Here are the relevant sections of the CI logs:

Darwin

40 -- Looking for IceConnectionNumber in ICE
41 -- Looking for IceConnectionNumber in ICE - not found
42 -- Found XQuartzGL: /nix/store/8s52v5xxs8cn86xr63j5h6sspyq4s633-libGL-22.3.7-dev/include
43 -- Found Geant4: /nix/store/ymixmqqrdi26w5b206y73f23k8bbn528-geant4-11.0.4/lib/Geant4-11.0.4/Geant4Config.cmake (found version "11.0.4") 
44 -- Configuring done
45 -- Generating done
46 -- Build files have been written to: /Users/runner/work/nain4/nain4/nain4/build
47 [  8%] Building CXX object CMakeFiles/Nain4.dir/n4_run_manager.cc.o
48 [ 16%] Building CXX object CMakeFiles/Nain4.dir/g4-mandatory.cc.o
49 [ 25%] Building CXX object CMakeFiles/Nain4.dir/n4-volumes.cc.o
50 [ 33%] Building CXX object CMakeFiles/Nain4.dir/n4_ui.cc.o
51 [ 41%] Building CXX object CMakeFiles/Nain4.dir/n4-utils.cc.o
52 [ 50%] Building CXX object CMakeFiles/Nain4.dir/nain4.cc.o
53 [ 58%] Linking CXX shared library libNain4.dylib
54 Undefined symbols for architecture x86_64:
55   "aPrimaryVertexAllocator()", referenced from:
56       nain4::generator::geantino_along_x(G4Event*) in g4-mandatory.cc.o
57       G4PrimaryVertex::operator delete(void*) in g4-mandatory.cc.o

... and so on, for many more symbols defined by the dependency

Linux

40 -- Looking for IceConnectionNumber in ICE
41 -- Looking for IceConnectionNumber in ICE - found
42 -- Found Geant4: /nix/store/2nm3pfqc21fa0qpha7nl3cs3597d0ghx-geant4-11.0.4/lib/Geant4-11.0.4/Geant4Config.cmake (found version "11.0.4") 
43 -- Configuring done
44 -- Generating done
45 -- Build files have been written to: /home/runner/work/nain4/nain4/nain4/build
46 [  8%] Building CXX object CMakeFiles/Nain4.dir/g4-mandatory.cc.o
47 [ 16%] Building CXX object CMakeFiles/Nain4.dir/n4-volumes.cc.o
48 [ 25%] Building CXX object CMakeFiles/Nain4.dir/n4-utils.cc.o
49 [ 33%] Building CXX object CMakeFiles/Nain4.dir/n4_run_manager.cc.o
50 [ 41%] Building CXX object CMakeFiles/Nain4.dir/nain4.cc.o
51 [ 50%] Building CXX object CMakeFiles/Nain4.dir/n4_ui.cc.o
52 [ 58%] Linking CXX shared library libNain4.so
53 [ 58%] Built target Nain4
54 [ 66%] Building CXX object CMakeFiles/nain4-tests.dir/test/catch2-main-test.cc.o

... and so on, until successful completion
  • Lines 40-41 are just context. Up to line 41 everything is identical on both systems apart from .so vs .dylib and nix store hashes.

  • On Darwin there is an extra line (42) that finds XQuartzGL.

  • 43/42: (Darwin/Linux) finds the dependency’s cmake config.

  • The first significant difference is on line 54/53, where the linker fails to find symbols defined by the dependency.

What could be causing this difference in behaviour? What might be done to fix the problem?

(I do not have interactive access to a Darwin machine myself, )

Adding

if(APPLE)
    target_link_options(Nain4 PRIVATE -undefined dynamic_lookup)
endif()

gets me past the immediate error.

Now I get segfaults (on darwin only) when running the (Catch2-based) test suite.

It seems that this is because of a bug in the macos-12 toolchain.