Fail to compile using root libraries


I have a root 6.13.02 build from source on Ubuntu 17.10 (64bit).

I’m trying to compile an application using root libraries, but I’m getting undefined libraries error:

g++ $(root-config --cflags) $(root-config --libs --ldflags)  test.C -o test
/tmp/ccqYc4Hn.o: In function `main':
test.C:(.text+0x33): undefined reference to `TString::TString(char const*)'
test.C:(.text+0x64): undefined reference to `TString::~TString()'
test.C:(.text+0x86): undefined reference to `TString::~TString()'
collect2: error: ld returned 1 exit status

The code itself is a simple test declaration of basic root TString:

#include <stdio.h>
#include "TString.h"
int main(){
  printf("Hello, world!\n");
  TString str = "Root string";
  printf("%s", str.Data());
  return 0;

If I try to compile it from inside root it works normally:

root [0] .L test.C++
Info in <TUnixSystem::ACLiC>: creating shared library /home/beliy/tests/./
root [1] main()
Hello, world!
Root string(int) 0
root [2] 

When I try at lxplus, it compiles, so the problem is in my installation.

I have both ROOTSYS and and LD_LIBRARY_PATH set up by script. Is there a some variable to set up, or something else that I’m missing?

Thanks in advance

g++ $(root-config --cflags) -o test test.C $(root-config --libs)

BTW. Instead of g++ you might better use $(root-config --cxx)

It worked. But I do not get why. Is it related to gcc version? Libraries must come after the source files?

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.