# ROOT + CMake + VisualStudio - macro builds but doesn't work

**URL:** <https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317>\
**Category:** Newbie\
**Created:** [October 11, 2019, 3:32pm UTC](https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317 "2019-10-11T15:32:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![piotr-nowakowski](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/82dd89/32.png) [@piotr-nowakowski](https://root-forum.cern.ch/u/piotr-nowakowski)\
**Post date:** [October 11, 2019, 3:32pm UTC](https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317/1 "2019-10-11T15:32:40Z")

</div>

Hello,  
I am using Visual Studio 2019 (16.3.4).  
I have installed ROOT 6.18.04 using the installer.

I tried to compile a simple program, but it doesn’t work.

This is my CMakeLists:

```auto
project(CMakeRootTest CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

cmake_minimum_required (VERSION 3.9)

# Add ROOT to path
list(APPEND CMAKE_PREFIX_PATH ${ROOTSYS}) # $ROOTSYS points to C:\root_v6.18.04\

find_package(ROOT 6.18 CONFIG REQUIRED)

# Add source to this project's executable.
add_executable (CMakeRootTest "CMakeRootTest.cpp")
target_link_libraries(CMakeRootTest PUBLIC ROOT::Core)

```

CMakeRootTest.cpp:

```auto
#include <TString.h>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
	std::cout << "Program start\n";
	TString t = "TEST TString";
	std::cout << t << std::endl;

	return EXIT_SUCCESS;
}

```

The project builds. I have copied all of the \*.dll, \*.rdict.pcm and \*.rootmap files from the ROOT’s bin folder to ensure that the executable has all of the files it needs.

Unfortunately, it doesn’t even reach the _main()_ function, it crashes somewhere in the initialization with

```auto
Unhandled exception at 0x77A20E43 (ntdll.dll) in CMakeRootTest.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77A5E930).

```

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 14, 2019, 3:32pm UTC](https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317/2 "2019-10-14T15:32:17Z")

</div>

I’ll try, but in the meanwhile, can you tell me how you build your application?

---

<div class="post-metadata">

**Author:** ![piotr-nowakowski](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/82dd89/32.png) [@piotr-nowakowski](https://root-forum.cern.ch/u/piotr-nowakowski)\
**Post date:** [October 14, 2019, 7:40pm UTC](https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317/3 "2019-10-14T19:40:01Z")

</div>

I have created the CMake project in the IDE using the built-in wizard, added the “-DROOTSYS=C:\root\_v6.18.04” to CMakeSettings.json (“CMake command arguments”), changed “Configuration type” to “Release”, “Toolset” to “msvc\_x86” and replaced the content of the default files with what I’ve shown.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 15, 2019, 8:10am UTC](https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317/4 "2019-10-15T08:10:25Z")

</div>

I just built your app with ROOT (master), Visual Studio 2019 v16.3.4 and this `CMakeLists.txt`:

```auto
project(CMakeRootTest CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

cmake_minimum_required (VERSION 3.9)

# Add ROOT to path
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

find_package(ROOT 6.18 CONFIG REQUIRED)
include_directories($ENV{ROOTSYS}/include)

# Add source to this project's executable.
add_executable (CMakeRootTest "CMakeRootTest.cpp")
target_link_libraries(CMakeRootTest PUBLIC ROOT::Core)

```

Configuring this way:

```auto
C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>cmake -A Win32 -Thost=x64 -G"Visual Studio 16 2019" ..\
-- The CXX compiler identification is MSVC 19.23.28106.4
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build

```

Here is the result when building with the `cmake --build` command:

```auto
C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>cmake --build . --config Debug
Microsoft (R) Build Engine version 16.3.1+1def00d3d for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  CMake does not need to re-run because C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build/CMakeFiles/generate.stam
  p is up-to-date.
  Building Custom Rule C:/Users/bellenot/rootdev/forum/piotr-nowakowski/CMakeLists.txt
  CMake does not need to re-run because C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build/CMakeFiles/generate.stam
  p is up-to-date.
  CMakeRootTest.cpp
C:\Users\bellenot\build\debug\include\ROOT\libcpp_string_view.h(193,9): warning C4068: unknown pragma [C:\Users\belleno
t\rootdev\forum\piotr-nowakowski\build\CMakeRootTest.vcxproj]
  CMakeRootTest.vcxproj -> C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build\Debug\CMakeRootTest.exe
  Building Custom Rule C:/Users/bellenot/rootdev/forum/piotr-nowakowski/CMakeLists.txt
  CMake does not need to re-run because C:/Users/bellenot/rootdev/forum/piotr-nowakowski/build/CMakeFiles/generate.stam
  p is up-to-date.

```

And then, running it it:

```auto
C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>Debug\CMakeRootTest.exe
Program start
TEST TString

C:\Users\bellenot\rootdev\forum\piotr-nowakowski\build>

```

N.B. I called `root\bin\thisroot.bat` before configuring/building the app. And I build in `Debug` mode because I use a `Debug` build of ROOT (one cannot mix debug/release modes on Windows.

I also tried with ROOT v6.18.04 and it works the same.

---

<div class="post-metadata">

**Author:** ![piotr-nowakowski](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/82dd89/32.png) [@piotr-nowakowski](https://root-forum.cern.ch/u/piotr-nowakowski)\
**Post date:** [October 15, 2019, 5:58pm UTC](https://root-forum.cern.ch/t/root-cmake-visualstudio-macro-builds-but-doesnt-work/36317/5 "2019-10-15T17:58:30Z")

</div>

The crash was caused by the _ROOTSYS_ variable not being present when the program is executed.
