Can not compile my C++ code including headers from ROOT

_ROOT Version: 6.26/06
Platform: macOS 12.4
Compiler: g++ / clang ?

I can compile the root macros written in C with root /path/to/mymacro.c command just fine but I can’t compile a C++ file including headers from ROOT. I am using bash.

A basic cpp file like this

(with or without) #include <iostream> 
#include "TTree.h"
#include "TFile.h"

void trying(){

    std::cout << "Hello World!" << std::endl;


int main(){

    return 0;

Does not compile. It gives many errors.

I am trying to write a C++ program where I get a TSV file from the user, in which the columns are converted to vectors in another cpp program I wrote ( which works fine), and I want to convert those vectors to TTree’s with a new cpp file I linked together with #include “giant.h” .

I used the prompt g++ all_of_the_files_I_want_to_compile_.cpp -o output $(root-config --libs) and g++ all_the_files.cpp -o output $(root-config --cflags) and g++ myfile.c I$(root-config --incdir) -o output and all the other configurations I can’t remember now.

I am a noob idk what to do at this point!

$(root-config --cxx --cflags) -O2 -Wall -Wextra -o output all_the_files.cpp $(root-config --libs)

1 Like

Thank you for your prompt response.

When I run the command you gave with all the source files, I get the variations of the following error:

ld: warning: ignoring file /usr/local/Cellar/root/6.26.06_2/lib/root/, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

plus this:

Undefined symbols for architecture arm64:
  "TVersionCheck::TVersionCheck(int)", referenced from:
      __GLOBAL__sub_I_functions.cpp in functions-e54aa0.o
      __GLOBAL__sub_I_interface.cpp in interface-7ec9d8.o
      __GLOBAL__sub_I_main.cpp in main-ac668d.o
ld: symbol(s) not found for architecture arm64

I haven’t used the functions dependent on the ROOT headers in my program yet. When I remove TFile.h and TTree.h from the giant.h file that every source file depends on and run the exact same thing with:

g++ allsource.cpp -o output

I can get the output file with no problems.

Looks like you have ROOT binaries for an “x86_64” while your machine is an “arm64”.

1 Like

Do I have to uninstall and install the correct version of ROOT? If so, may I ask how do I find it? I am new to programming C++ and ROOT.

1 Like

Thank you very much! I wasn’t able to solve this problem for the last couple days.

So, I rebuilded ROOT a few times using different sources, which I probably made a mistake with because I kept getting the same error. Adding -arch x86_64 at the end of the command you shared solved my problem for some reason:

$(root-config --cxx --cflags) -O2 -Wall -Wextra -o output all_the_files.cpp $(root-config --libs) -arch x86_64

Leaving this here if anyone else needs it.

Thank you very much.