# TCanvas doesn't show up

**URL:** <https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694>\
**Category:** ROOT\
**Tags:** graphics, root\
**Created:** [January 17, 2025, 9:52pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694 "2025-01-17T21:52:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [January 17, 2025, 9:52pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/1 "2025-01-17T21:52:36Z")

</div>

Hello everyone,

I want to use TCanvas library for my C++ project to plot some data. For that I did created `test.cc` file which looks like this

```auto
int main(int argc, char* argv[])
{
  double var_x[3] = {1, 2, 3};
  double var_y[3] = {1, 2, 3};

  TCanvas *can1 = new TCanvas();
  TGraph *gr1 = new TGraph(3, var_x, var_y);
  gr1->Draw("ALP");

  std::cout << "code is working" << std::endl; 
  return 1;
}

```

Since, it is a C++ code, I also created this make file

```auto
test: test.o
	g++ test.o -o test `root-config --cflags --glibs`

test.o: test.cc
	g++ -c test.cc `root-config --cflags`

clean:
	rm test.o

```

However, when I run `./test` in terminal, the canvas doesn’t show up.

Please note that if I create a macro

```auto
int test2()
{
  // same code as test.C
}

```

and run it with `root -l test2.C`, then only canvas shows up. But this is not what I want. I want to create an executable `test`. Any solution to this problem?

Thanks in advance.

Best,  
Divyang.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 17, 2025, 10:18pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/2 "2025-01-17T22:18:23Z")

</div>

If the same macro runs correctly in the interactive prompt, I guess the canvas is also being created and shown when compiled, but note that when the program ends (same as quitting ROOT in interactive mode) the canvas is destroyed; in short, it is shown and closed so fast that you don’t really see it. Try saving the canvas within the macro and then you can look for the output file (like a png, for instance) to check whether it ran correctly; e.g.

```auto
//...
  gr1->Draw("ALP");
  can1->SaveAs("myplot.png");
//...

```

Or make the macro ask for some user input after drawing, to create a “pause” to allow you to see the canvas.

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [January 17, 2025, 10:28pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/3 "2025-01-17T22:28:59Z")

</div>

if I do `can1->SaveAs("can1.pdf");` then it does save the pdf file. However, this is not what I need.

I tried to add the pause part by asking the user for an input. Unfortunately, the canvas is still not appearing. Here is what I added

```auto
int dummy_inpt;
std::cout << "wait for canvas: ";
std::cin >> dummy_inpt;

```

Here is my ROOT version

```auto
   ------------------------------------------------------------------
  | Welcome to ROOT 6.24/08 https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 29 2022, 13:04:57 |
  | From tags/v6-24-08@v6-24-08 |
  | With |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------------

```

My machine specifications

```auto
OS: Ubuntu 20.04.6 LTS x86_64 
Host: 82KB Lenovo V15 G2 ITL Ua 
Kernel: 5.15.0-130-generic 
Packages: 2453 (dpkg), 39 (snap) 
Shell: bash 5.0.17 
Resolution: 1920x1080, 1920x1080 
DE: Unity 
WM: Mutter 
CPU: 11th Gen Intel i5-1135G7 (8) @ 4.200GHz
GPU: Intel Device 9a49 
Memory: 3888MiB / 19795MiB 

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 17, 2025, 10:56pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/4 "2025-01-17T22:56:58Z")

</div>

Try with a TApplication:

```auto
#include "TCanvas.h"
#include "TGraph.h"
#include "TApplication.h"
#include <iostream>

// g++ test.cc -o test `root-config --cflags --glibs`

int main(int argc, char* argv[])
{
  TApplication a("a", 0, 0);

  double var_x[3] = {1, 2, 3};
  double var_y[3] = {1, 2, 3};

  TCanvas *can1 = new TCanvas();
  TGraph *gr1 = new TGraph(3, var_x, var_y);
  gr1->Draw("ALP");
  gPad->Modified();
  gPad->Update();
  //can1->SaveAs("b.png");

  std::cout << "Ctrl+c or choose File->Quit ROOT from TCanvas menu to end" << std::endl;

  a.Run(kTRUE);
  return 1;
}

```

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [January 17, 2025, 11:10pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/5 "2025-01-17T23:10:29Z")

</div>

Thank you very much @dastudillo. It worked!

I added `--glibs` into the makefile as you suggested. Also, I am just curious about why this worked with `TApplication`. Any comment or links for further reading will be a great help, as I will include this part in my main project.

Thanks again.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 17, 2025, 11:19pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/6 "2025-01-17T23:19:52Z")

</div>

Probably others can give you better insight, but you can read a bit here:

> **[ROOT Primer](https://root.cern/primer/#interpretation-and-compilation)**
>
> Data Analysis Framework

and also search this Forum for posts related to TApplication, you’ll find some explanations here and there.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [January 31, 2025, 11:20pm UTC](https://root-forum.cern.ch/t/tcanvas-doesnt-show-up/62694/7 "2025-01-31T23:20:48Z")

</div>

Closed on January 31, 2025, 11:20pm UTC

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