Please read tips for efficient and successful posting and posting code
ROOT Version: Latest as of April the 23rd
Platform: Ubuntu 20.04
Compiler: CMake
I am trying to create a ROOT project using cmake. Everything is working fine, I just was wondering how I can import for example “TVector3.h” into my main .cxx file? When I try it, it says the file was not found. I followed this tutorial and everything compiled successfully.
Here is my main .cxx file:
#include <iostream>
#include "HeatSim.h"
int main() {
TVector3 *test = new TVector3(1, 2, 3);
std::cout << test->X() << std::endl;
return 0;
}
in the HeatSim.h file i have written `include “TVector3.h” ´
You must call thisroot.sh
before calling CMake/make, in order to have ${ROOTSYS}/include
in your include PATH
And you might have to add
include(${ROOT_USE_FILE})
include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})
link_directories(${ROOT_LIBRARY_DIR})
in your CMakeLists.txt
1 Like
Ah yes, this seems to be working. However now (with the same code as above), it is giving me a new error:
usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `main':
HeatSim.cxx:(.text+0x5e): undefined reference to `TObject::operator delete(void*)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `__static_initialization_and_destruction_0(int, int)':
HeatSim.cxx:(.text+0xcd): undefined reference to `TVersionCheck::TVersionCheck(int)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TObject::operator new(unsigned long)':
HeatSim.cxx:(.text._ZN7TObjectnwEm[_ZN7TObjectnwEm]+0x18): undefined reference to `TStorage::ObjectAlloc(unsigned long)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TObject::TObject()':
HeatSim.cxx:(.text._ZN7TObjectC2Ev[_ZN7TObjectC5Ev]+0x13): undefined reference to `vtable for TObject'
/usr/bin/ld: HeatSim.cxx:(.text._ZN7TObjectC2Ev[_ZN7TObjectC5Ev]+0x52): undefined reference to `TObject::fgObjectStat'
/usr/bin/ld: HeatSim.cxx:(.text._ZN7TObjectC2Ev[_ZN7TObjectC5Ev]+0x66): undefined reference to `TObject::AddToTObjectTable(TObject*)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TVector3::TVector3(double, double, double)':
HeatSim.cxx:(.text._ZN8TVector3C2Eddd[_ZN8TVector3C5Eddd]+0x2e): undefined reference to `vtable for TVector3'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/HeatSimulation.dir/build.make:84: HeatSimulation] Error 1
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/HeatSimulation.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I tried to fix this by adding ROOT::Core to my target_link_libraries in CMakeLists.txt, but the error persists. What can I do?
Try with target_link_libraries(${PROJECT_NAME} ${ROOT_LIBRARIES})
1 Like
When I do this I get:
CMake Error at CMakeLists.txt:19 (target_link_libraries):
Cannot specify link libraries for target "heatsim" which is not built by
this project.
when I add PUBLIC
in between the two I get the same error as above.
Did you replace ${PROJECT_NAME}
by your real target name (heatsim
maybe) ?
Yes, but then I am greeted with the error from above:
usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `main':
HeatSim.cxx:(.text+0x5e): undefined reference to `TObject::operator delete(void*)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `__static_initialization_and_destruction_0(int, int)':
HeatSim.cxx:(.text+0xcd): undefined reference to `TVersionCheck::TVersionCheck(int)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TObject::operator new(unsigned long)':
HeatSim.cxx:(.text._ZN7TObjectnwEm[_ZN7TObjectnwEm]+0x18): undefined reference to `TStorage::ObjectAlloc(unsigned long)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TObject::TObject()':
HeatSim.cxx:(.text._ZN7TObjectC2Ev[_ZN7TObjectC5Ev]+0x13): undefined reference to `vtable for TObject'
/usr/bin/ld: HeatSim.cxx:(.text._ZN7TObjectC2Ev[_ZN7TObjectC5Ev]+0x52): undefined reference to `TObject::fgObjectStat'
/usr/bin/ld: HeatSim.cxx:(.text._ZN7TObjectC2Ev[_ZN7TObjectC5Ev]+0x66): undefined reference to `TObject::AddToTObjectTable(TObject*)'
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TVector3::TVector3(double, double, double)':
HeatSim.cxx:(.text._ZN8TVector3C2Eddd[_ZN8TVector3C5Eddd]+0x2e): undefined reference to `vtable for TVector3'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/HeatSimulation.dir/build.make:84: HeatSimulation] Error 1
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/HeatSimulation.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
After doing some digging, I think the problem may be that i’m not linking against libCore. How would I do this (if this is indeed the problem)?
Try to add libCore
(or Core
) in target_link_libraries
No change. Still the same error
Please provide something we can try
Here is my CMakeLists.txt file at the moment:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(heatsim)
find_package(ROOT 6.16 CONFIG REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include(${ROOT_USE_FILE})
include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})
link_directories(${ROOT_LIBRARY_DIR})
add_library(HeatSim)
root_generate_dictionary(G__HeatSim HeatSim.h MODULE HeatSim LINKDEF HeatSimLinkDef.h)
target_link_libraries(HeatSim PUBLIC ${ROOT_LIBRARIES} Core)
add_executable(HeatSimulation HeatSim.cxx)
Maybe you can spot the new error? Sorry this is taking so long
Or maybe you can try it on your own system?
First of all, G__HeatSim.cxx
is missing from add_executable
add_executable(HeatSimulation HeatSim.cxx G__HeatSim.cxx)
And do you have a ClassDef()
in your class definition?
1 Like
You need to pass the dictionary and link the libraries to your executable:
add_executable(HeatSimulation HeatSim.cxx G__HeatSim.cxx)
target_link_libraries(HeatSimulation Core)
Unrelated, but
add_library(HeatSim)
does not seem to do anything.
1 Like
Okay, I have done this but am still getting the error.
when I remove that line the cmake .
command fails.
Ahh never mind, when I do this
add_executable(HeatSimulation HeatSim.cxx G__HeatSim.cxx)
target_link_libraries(HeatSimulation Core)
The initial error goes away, however, I then get this one which is more specific to what I am doing:
[ 14%] Building CXX object CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o
[ 28%] Linking CXX executable HeatSimulation
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `TVector3::TVector3(double, double, double)':
HeatSim.cxx:(.text._ZN8TVector3C2Eddd[_ZN8TVector3C5Eddd]+0x2e): undefined reference to `vtable for TVector3'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/HeatSimulation.dir/build.make:112: HeatSimulation] Error 1
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/HeatSimulation.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
For refrence, here is my HeatSim.cxx file:
#include <iostream>
#include "HeatSim.h"
#include "TVector3.h"
int main() {
TVector3 *test = new TVector3(1, 2, 3);
std::cout << test->X() << std::endl;
return 0;
}
Okay, I fixed it by simply adding Physics
to target_link_libraries . It’s working now! Thank you both for your help!! Have a nice weekend 
@eguiraud @bellenot terribly sorry to bother you again, but when I try to create a TGraph or a TCanvas I get something like this:
/usr/bin/ld: CMakeFiles/HeatSimulation.dir/HeatSim.cxx.o: in function `main':
HeatSim.cxx:(.text+0x76): undefined reference to `TCanvas::TCanvas(char const*, char const*, int, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/HeatSimulation.dir/build.make:112: HeatSimulation] Error 1
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/HeatSimulation.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
How can I fix this?
I have imported TPad.h and TCanvas.h in the header file
EDIT: I fixed it. Forgot to import Gpad
undefined reference to..
is not a missing #include
, it’s a missing linked library. In this case, you need Gpad
I think.
1 Like
Yes, I did this and I am getting no errors now. Is there a way to make the TCanvas appear in a window? Like when running a normal ROOT project?