@bellenot
Two more:
how to link against existing root-classes (e.g. TTree::Tree(..))??
how to create a binary instead of a library (creating a file including int main())???
Thanks Georg
@bellenot
Two more:
how to link against existing root-classes (e.g. TTree::Tree(..))??
how to create a binary instead of a library (creating a file including int main())???
Thanks Georg
target_link_libraries(measure ROOT::Tree)
or:
target_link_libraries(measure ${ROOT_LIBRARIES})
add_executable(executable_name main.cxx)
See also: CMake Tutorial ā CMake 4.2.2 Documentation and Integrating ROOT into CMake projects - ROOT
@bellenot
This help a lot, thank you very much. compiling is working now
Unfortunately, the windows-nightmare is going on.
Iām facing some interesting issue.
Running my classes in root-terminal is working without any issues (using gSystem->Load()), but when Iām putting elements in a script, its becoming stange: What I did is:
R__LOAD_LIBRARY(build/Release/measure)
#include "src/K2000.h"
int testRS232K2000() {
K2000 k("COM4",9600);
[...]
}
this gives me āerror: base class has incomplete typeā, ādefinition of 'Deviceā is not complete until the closing ā}ā ānon-cost lvalue reference to type āDeviceā cannot bind to a value of unrelated typeā¦ā
from the basic structure my K2000 class looks like
class __declspec(dllexport) K2000: public Device {
public:
class __declspec(dllexport) Input : public Channel {
//! The interface to the K2000
K2000 &_dev;
//! Getter for the K2000.
Device & getDevice() override { return _dev; }
[..]
};
[...]
};
}
I have the impression that the foward declarations in the nested-classes are not working anymore. This was never a problem (on earlier c++ and linux).
I do not want you to look over my code, but the fact that running from terminal runs and from script not is really strange to meā¦any hint? Did I load the libraries correctly?
Georg
Sorry, but I donāt see anything obvious with the code you postedā¦
Hi @bellenot,
I was able to create a minimalistic example, hope you can help:
The compilations runs.
testexe.exe runs:
.\testexe.exe
A::input()
42
root -l test.C fails to run:
root -l test.C
root [0]
Processing test.C...
In file included from libDictOutput dictionary payload:5:
C:/Users/troska/Desktop/git/measure/test/A.h:5:40: error: base class has incomplete type
class __declspec(dllexport) A : public Device {
~~~~~~~^~~~~~
[...a lot more...]
the interesting thing is, that test.C runs, when I put the content of A.h into oneheader.h.
I still think that something is not setup correctly in the CMakeLists.txt.
would be nice, if you could give me some advice - thanks a lot
Georg
oneheader.h
#ifndef oneheader_h
#define oneheader_h
#include <iostream>
class __declspec(dllexport) Device {
public:
Device() {}
virtual ~Device() {}
double read() { return 42; }
void write(double p) { std::cout << "writing " << p << std::endl; }
};
#ifdef __CLING__
#pragma link C++ class Device-;
#endif
#endif
A.h
#ifndef A_h
#define A_h
#include "oneheader.h"
class __declspec(dllexport) A : public Device {
public:
class __declspec(dllexport) Input {
public:
A &_dev;
Device &getDevice() { return _dev; }
Input(A *d) : _dev(*d) {}
virtual double operator()() {
std::cout << "A::input()" << std::endl;
return _dev.read();
}
virtual ~Input() {}
} input;
A() : Device(), input(this) {}
virtual ~A() {}
};
#ifdef __CLING__
#pragma link C++ class A-;
#endif
#endif
testexe.cxx
#include "oneheader.h"
#include "A.h"
int main() {
A *a = new A();
std::cout << a->input() << std::endl;
return 0;
}
test.C
R__LOAD_LIBRARY(test)
#include "../../oneheader.h"
#include "../../A.h"
#include <iostream>
int test() {
A *a = new A();
std::cout << a->input() << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(test LANGUAGES CXX)
find_package(ROOT REQUIRED )
include_directories(${ROOT_INCLUDE_DIRS})
link_directories(${ROOT_LIBRARY_DIR})
set(CMAKE_CXX_FLAGS "${ROOT_CXX_FLAGS}")
root_generate_dictionary(DictOutput
${CMAKE_CURRENT_SOURCE_DIR}/A.h
${CMAKE_CURRENT_SOURCE_DIR}/oneheader.h
)
add_library(test SHARED
DictOutput.cxx
)
add_executable(testexe ${CMAKE_CURRENT_SOURCE_DIR}/testexe.cxx)
# set_target_properties(test PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
# target_link_libraries(test PUBLIC ROOT::Core)
target_link_libraries(test PUBLIC ${ROOT_LIBRARIES})
First try:
C:\root-dev\rootdev\forum\Georg_T\build>testexe
A::input()
42
C:\root-dev\rootdev\forum\Georg_T\build>root -l test.C
root [0]
Processing test.C...
Error in <TInterpreter::TCling::AutoLoad>: failure loading library libDictOutput.dll for oneheader.h
A::input()
42
(int) 0
root [1]
And to disable the Error in <TInterpreter::TCling::AutoLoad>: failure loading library libDictOutput.dll for oneheader.h, you can add set(CMAKE_ROOTTEST_NOROOTMAP ON) in your CMakeLists.txt
@bellenot,
seems you are getting a completely different error on your system, see here:
PS C:\Users\troska\Desktop\git\measure\test\build\Release> root -l test.C
root [0]
Processing test.C...
In file included from libDictOutput dictionary payload:5:
C:/Users/troska/Desktop/git/measure/test/A.h:5:40: error: base class has incomplete type
class __declspec(dllexport) A : public Device {
~~~~~~~^~~~~~
C:/Users/troska/Desktop/git/measure/test/oneheader.h:5:29: note: definition of 'Device' is not complete until the closing '}'
class __declspec(dllexport) Device {
^
In file included from libDictOutput dictionary payload:5:
C:/Users/troska/Desktop/git/measure/test/A.h:10:32: error: non-const lvalue reference to type 'Device' cannot bind to a
value of unrelated type 'A'
Device &getDevice() { return _dev; }
^~~~
C:/Users/troska/Desktop/git/measure/test/A.h:14:16: error: no member named 'read' in 'A'
return _dev.read();
~~~~ ^
C:/Users/troska/Desktop/git/measure/test/A.h:19:8: error: type 'Device' is not a direct or virtual base of 'A'
A() : Device(), input(this) {}
^~~~~~
Error in <TInterpreter::AutoParse>: Error parsing payload code for class Device with content:
#line 1 "libDictOutput dictionary payload"
#define _BACKWARD_BACKWARD_WARNING_H
// Inline headers
#include "C:/Users/troska/Desktop/git/measure/test/A.h"
#include "C:/Users/troska/Desktop/git/measure/test/oneheader.h"
#undef _BACKWARD_BACKWARD_WARNING_H
Error in <TInterpreter::TCling::AutoLoad>: failure loading library libDictOutput.dll for oneheader.h
Error in <TInterpreter::AutoParse>: Error parsing payload code for class A with content:
#line 1 "libDictOutput dictionary payload"
#define _BACKWARD_BACKWARD_WARNING_H
// Inline headers
#include "C:/Users/troska/Desktop/git/measure/test/A.h"
#include "C:/Users/troska/Desktop/git/measure/test/oneheader.h"
#undef _BACKWARD_BACKWARD_WARNING_H
root [1]
maybe different root or compiler version??
with the additional flag in CMakeLists.txt nothing changed for me
Georg
Here is all Iāve done:
C:\root-dev\rootdev\forum\Georg_T\build>cmake -Wno-dev -G"Visual Studio 17 2022" -A x64 -Thost=x64 -DCMAKE_VERBOSE_MAKEFILE=ON ..\
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.26200.
-- The CXX compiler identification is MSVC 19.44.35222.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.1s)
-- Generating done (0.1s)
-- Build files have been written to: C:/root-dev/rootdev/forum/Georg_T/build
C:\root-dev\rootdev\forum\Georg_T\build>cmake --build . --config RelWithDebInfo
Change Dir: 'C:/root-dev/rootdev/forum/Georg_T/build'
Run Build Command(s): "C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe" ALL_BUILD.vcxproj /p:Configuration=RelWithDebInfo /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:n
MSBuild version 17.14.40+3e7442088 for .NET Framework
Build started 28/01/2026 10:26:29.
Project "C:\root-dev\rootdev\forum\Georg_T\build\ALL_BUILD.vcxproj" on node 1 (default targets).
Project "C:\root-dev\rootdev\forum\Georg_T\build\ALL_BUILD.vcxproj" (1) is building "C:\root-dev\rootdev\forum\Georg_T\build\ZERO_CHECK.vcxproj" (2) on node 1 (default targets).
PrepareForBuild:
Creating directory "x64\RelWithDebInfo\ZERO_CHECK\".
Structured output is enabled. The formatting of compiler diagnostics will reflect the error hierarchy. See https://aka.ms/cpp/structured-output for more details.
Creating directory "x64\RelWithDebInfo\ZERO_CHECK\ZERO_CHECK.tlog\".
InitializeBuildStatus:
Creating "x64\RelWithDebInfo\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
Touching "x64\RelWithDebInfo\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
CustomBuild:
1>Checking Build System
FinalizeBuildStatus:
Deleting file "x64\RelWithDebInfo\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
Touching "x64\RelWithDebInfo\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate".
Done Building Project "C:\root-dev\rootdev\forum\Georg_T\build\ZERO_CHECK.vcxproj" (default targets).
Project "C:\root-dev\rootdev\forum\Georg_T\build\ALL_BUILD.vcxproj" (1) is building "C:\root-dev\rootdev\forum\Georg_T\build\test.vcxproj" (3) on node 1 (default targets).
PrepareForBuild:
Structured output is enabled. The formatting of compiler diagnostics will reflect the error hierarchy. See https://aka.ms/cpp/structured-output for more details.
Creating directory "C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo\".
Creating directory "test.dir\RelWithDebInfo\test.tlog\".
InitializeBuildStatus:
Creating "test.dir\RelWithDebInfo\test.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
Touching "test.dir\RelWithDebInfo\test.tlog\unsuccessfulbuild".
CustomBuild:
Generating G__DictOutput.cxx, libDictOutput_rdict.pcm
CUSTOMBUILD : warning : Unused class rule: oneheader [C:\root-dev\rootdev\forum\Georg_T\build\test.vcxproj]
Building Custom Rule C:/root-dev/rootdev/forum/Georg_T/CMakeLists.txt
ClCompile:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\CL.exe /c /I"C:\root-dev\build\x64\relwithdebinfo\include" /Zi /nologo /W1 /WX- /diagnostics:column /O2 /Ob1 /D _WINDLL /D _MBCS
/D _XKEYCHECK_H /D NOMINMAX /D _CRT_SECURE_NO_WARNINGS /D _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING /D _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING /D NDEBUG /D "CMAKE_INTDIR=\"RelWithDebInfo\"" /D test_EXPORT
S /EHsc /MD /GR /std:c++17 /Fo"test.dir\RelWithDebInfo\\" /Fd"test.dir\RelWithDebInfo\vc143.pdb" /external:W1 /TP /wd4141 /wd4291 /wd4244 /wd4049 /wd4146 /wd4250 /wd4624 /wd4267 /FIw32pragma.h /FIsehmap.h /errorReport:queue -Z
c:__cplusplus "C:\root-dev\rootdev\forum\Georg_T\build\G__DictOutput.cxx"
G__DictOutput.cxx
Link:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\link.exe /OUT:"C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo\test.dll" /INCREMENTAL /ILK:"test.dir\RelWithDebInfo\test.i
lk" /NOLOGO /LIBPATH:"C:/root-dev/build/x64/relwithdebinfo/lib" /LIBPATH:"C:/root-dev/build/x64/relwithdebinfo/lib/RelWithDebInfo" "C:\root-dev\build\x64\relwithdebinfo\lib\libCore.lib" "C:\root-dev\build\x64\relwithdebinfo\lib
\libImt.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libRIO.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libNet.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libHist.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libGraf.l
ib" "C:\root-dev\build\x64\relwithdebinfo\lib\libGraf3d.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libGpad.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libROOTDataFrame.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libTr
ee.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libTreePlayer.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libRint.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libPostscript.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\
libMatrix.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libPhysics.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libMathCore.lib" "C:\root-dev\build\x64\relwithdebinfo\lib\libThread.lib" "C:\root-dev\build\x64\relwithdebinfo\
lib\libROOTVecOps.lib" kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB
:"C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/test.pdb" /TLBID:1 /IMPLIB:"C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/test.lib" /MACHINE:X64 /machine:x64 /DLL test.dir\RelWithDebInfo\G__DictOutput.obj
Creating library C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/test.lib and object C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/test.exp
test.vcxproj -> C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo\test.dll
FinalizeBuildStatus:
Deleting file "test.dir\RelWithDebInfo\test.tlog\unsuccessfulbuild".
Touching "test.dir\RelWithDebInfo\test.tlog\test.lastbuildstate".
Done Building Project "C:\root-dev\rootdev\forum\Georg_T\build\test.vcxproj" (default targets).
Project "C:\root-dev\rootdev\forum\Georg_T\build\ALL_BUILD.vcxproj" (1) is building "C:\root-dev\rootdev\forum\Georg_T\build\testexe.vcxproj" (4) on node 1 (default targets).
PrepareForBuild:
Structured output is enabled. The formatting of compiler diagnostics will reflect the error hierarchy. See https://aka.ms/cpp/structured-output for more details.
Creating directory "testexe.dir\RelWithDebInfo\testexe.tlog\".
InitializeBuildStatus:
Creating "testexe.dir\RelWithDebInfo\testexe.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
Touching "testexe.dir\RelWithDebInfo\testexe.tlog\unsuccessfulbuild".
CustomBuild:
Building Custom Rule C:/root-dev/rootdev/forum/Georg_T/CMakeLists.txt
ClCompile:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\CL.exe /c /I"C:\root-dev\build\x64\relwithdebinfo\include" /Zi /nologo /W1 /WX- /diagnostics:column /O2 /Ob1 /D _MBCS /D _XKEYCHE
CK_H /D NOMINMAX /D _CRT_SECURE_NO_WARNINGS /D _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING /D _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING /D NDEBUG /D "CMAKE_INTDIR=\"RelWithDebInfo\"" /EHsc /MD /GR /std:c++17
/Fo"testexe.dir\RelWithDebInfo\\" /Fd"testexe.dir\RelWithDebInfo\vc143.pdb" /external:W1 /TP /wd4141 /wd4291 /wd4244 /wd4049 /wd4146 /wd4250 /wd4624 /wd4267 /FIw32pragma.h /FIsehmap.h /errorReport:queue -Zc:__cplusplus "C:\roo
t-dev\rootdev\forum\Georg_T\testexe.cxx"
testexe.cxx
Link:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\link.exe /OUT:"C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo\testexe.exe" /INCREMENTAL /ILK:"testexe.dir\RelWithDebInfo\
testexe.ilk" /NOLOGO /LIBPATH:"C:/root-dev/build/x64/relwithdebinfo/lib" /LIBPATH:"C:/root-dev/build/x64/relwithdebinfo/lib/RelWithDebInfo" kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.
lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/testexe.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /IMPLIB:"C:/
root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/testexe.lib" /MACHINE:X64 /machine:x64 testexe.dir\RelWithDebInfo\testexe.obj
Creating library C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/testexe.lib and object C:/root-dev/rootdev/forum/Georg_T/build/RelWithDebInfo/testexe.exp
testexe.vcxproj -> C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo\testexe.exe
FinalizeBuildStatus:
Deleting file "testexe.dir\RelWithDebInfo\testexe.tlog\unsuccessfulbuild".
Touching "testexe.dir\RelWithDebInfo\testexe.tlog\testexe.lastbuildstate".
Done Building Project "C:\root-dev\rootdev\forum\Georg_T\build\testexe.vcxproj" (default targets).
PrepareForBuild:
Creating directory "x64\RelWithDebInfo\ALL_BUILD\".
Structured output is enabled. The formatting of compiler diagnostics will reflect the error hierarchy. See https://aka.ms/cpp/structured-output for more details.
Creating directory "x64\RelWithDebInfo\ALL_BUILD\ALL_BUILD.tlog\".
InitializeBuildStatus:
Creating "x64\RelWithDebInfo\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
Touching "x64\RelWithDebInfo\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild".
CustomBuild:
Building Custom Rule C:/root-dev/rootdev/forum/Georg_T/CMakeLists.txt
FinalizeBuildStatus:
Deleting file "x64\RelWithDebInfo\ALL_BUILD\ALL_BUILD.tlog\unsuccessfulbuild".
Touching "x64\RelWithDebInfo\ALL_BUILD\ALL_BUILD.tlog\ALL_BUILD.lastbuildstate".
Done Building Project "C:\root-dev\rootdev\forum\Georg_T\build\ALL_BUILD.vcxproj" (default targets).
Build succeeded.
"C:\root-dev\rootdev\forum\Georg_T\build\ALL_BUILD.vcxproj" (default target) (1) ->
"C:\root-dev\rootdev\forum\Georg_T\build\test.vcxproj" (default target) (3) ->
(CustomBuild target) ->
CUSTOMBUILD : warning : Unused class rule: oneheader [C:\root-dev\rootdev\forum\Georg_T\build\test.vcxproj]
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:04.00
C:\root-dev\rootdev\forum\Georg_T\build>cd RelWithDebInfo
C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo>testexe.exe
A::input()
42
C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo>root -l -b -q test.C
Processing test.C...
Error in <TCling::LoadPCM>: ROOT PCM C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo\libDictOutput_rdict.pcm file does not exist
A::input()
42
(int) 0
C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo>copy ..\libDictOutput_rdict.pcm .
1 file(s) copied.
C:\root-dev\rootdev\forum\Georg_T\build\RelWithDebInfo>root -l -b -q test.C
Processing test.C...
A::input()
42
(int) 0
Sorry, I think I found the issue. Can you add OPTIONS -writeEmptyRootPCM in your CMakeLists.txt, as shown below:
root_generate_dictionary(DictOutput
${CMAKE_CURRENT_SOURCE_DIR}/A.h
${CMAKE_CURRENT_SOURCE_DIR}/oneheader.h
OPTIONS
-writeEmptyRootPCM
)
(I forgot I added it to your fileā¦)
@bellenot,
I just tested at the above minimalistic example.
PS C:\Users\troska\Desktop\git\measure\test\build\Release> root -l .\test.C
root [0]
Processing .\test.C...
Error in <TInterpreter::TCling::AutoLoad>: failure loading library libDictOutput.dll for oneheader.h
A::input()
42
(int) 0
root [1]
seems to work, but still this strange error messageā¦
I tried toggling
set(CMAKE_ROOTTEST_NOROOTMAP ON)
no differenceā¦
I will try in the afternoon my full code.
What is āwriteEmptyRootPCMā doing?
Georg
AFAIK itās telling not to include the header files in the pcm file
Hi @bellenot
seems my code is running now, hope I will not discover something else.
May I recommend to describe the āhowto compile code depending on root and code to be used in root-command line in windows environmentsā in a better way
the explanation given at Integrating ROOT into CMake projects - ROOT is really not enough. A minimalistic example should include from my point of view:
There are soooo many details, it would really be nice to find them at one place
Thanks a lot for your help
Georg
Youāre very welcome! And OK, weāll improve the instructions. Thanks for the feedback.
Cheers, Bertrand