How to compile CERN ROOT macro (analysis C++ code)?

ROOT (a.k.a. CERN ROOT) is a C++ data analysis framework consisting of a C++ library and a C++ interpreter (cling) mainly used in the field of high-energy physics. The Nix expression is located in pkgs/applications/science/misc/root.

Aside from running with the interpreter, the analysis code (called the ROOT macro) can also be compiled using gcc:

$ gcc $(root-config --glibs --cflags) -o myAna myAna.C

according to the documentation.
In my experience, however, the compilation fails often.
The most recent case is

$ gcc -g -O1 $(root-config --libs) $(root-config --cflags) xAna_monoZ_preselect.C
/nix/store/hy3lz2vfv9qq2v5jz9nzlx6mmiaq79rj-binutils-2.35.1/bin/ld: /tmp/nix-shell.NUGUOF/ccsYbPqQ.o: undefined reference to symbol '_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc@@GLIBCXX_3.4.21'
/nix/store/hy3lz2vfv9qq2v5jz9nzlx6mmiaq79rj-binutils-2.35.1/bin/ld: /nix/store/lg104nh0szci8slz5z6494m457jm5y3p-gcc-10.3.0-lib/lib/ error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

A ROOT maintainer suggests that it was caused by the C++ library difference between the build environment of ROOT itself and the current environment. However, I already tried to build under a devShell with the gcc coming from the save revision of Nixpkgs, and even tried a Nix expression using

root.stdenv.mkDerivation {
  # ...

but the error still occurs.

Any suggestions to building the ROOT Macro on NixOS?

Cc: @veprbl

Don’t use gcc/cc/clang/$CC as an entry point to compile C++ code. It should work with g++/clang++/$CXX (also supposed to work with -x c++ flag, but there seems to be a bug). The GCC generally provides the auto-detection of the compiled language, but it’s not effective in nixpkgs because cc-wrapper chooses not to link the standard C++ libraries unless C++ compiler is explicitly invoked.

1 Like