Problem with gcc flag/file order when compiling against ROOT libraries

Hello, I’m having quite a silly problem while trying to compile a very simple code using ROOT libraries. I’ve reduced the problem to something as simple as:

#include <iostream>
#include "TFile.h"
int main()
    std::cout << "Hello world" << std::endl;
    return 0;

Where I’m just naively importing some ROOT class.
Then I try to compile using the required flags (and more)

`c++ `root-config --cflags --libs` code.C`

But I get the following error:

/tmp/cc92ofKo.o: In function `__static_initialization_and_destruction_0(int, int)':
code.C:(.text+0x86): undefined reference to `TVersionCheck::TVersionCheck(int)'
collect2: error: ld returned 1 exit status

However, if I just happen to place the source code before the flags, it works fine:

c++ code.C `root-config --cflags --libs`

Why does this happens? It is a problem since CMake automatically places the flags before the source file, and fails to build. I’ve seen this problem reported, but with no further insight provided…

ROOT Version: 6.18/04
Platform: Ubuntu 18.04
Compiler: c++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

Search for “--as-needed” in man ld -> Command Line Options

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.