# GUI Issues in Visual C++

**URL:** <https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011>\
**Category:** Newbie\
**Created:** [July 5, 2019, 12:13pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011 "2019-07-05T12:13:17Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 12:13pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/1 "2019-07-05T12:13:17Z")

</div>

Hi,

I am using VS 2019 with ROOT 6.18 to generate a GUI that should plot a sinx/x graph when the Draw button is clicked and close when the Exit button is clicked however I am having issues. This is an example found in p. 570 of the ROOT guide. I have looked at other posts that have similar problems and a solution that worked but I haven’t been able to understand it. I believe the solution involves dictionaries (which I am unfamiliar with).

In **myFrame.h** , when the line ClassDef(MyMainFrame, 0) is commented out, the GUI opens and the Exit button works but when it is included, I get the following errors:

1. myFrame.obj : error LNK2019: unresolved external symbol “public: static class TClass \* \_\_cdecl MyMainFrame::Class(void)” (?Class@MyMainFrame@@SAPAVTClass@@XZ) referenced in function "public: virtual class TClass \* \_\_thiscall MyMainFrame::IsA(void)const " (?IsA@MyMainFrame@@UBEPAVTClass@@XZ)

2. myFrame.obj : error LNK2019: unresolved external symbol “public: virtual void \_\_thiscall MyMainFrame::Streamer(class TBuffer &)” (?Streamer@MyMainFrame@@UAEXAAVTBuffer@@@Z) referenced in function “[thunk]:public: virtual void \_\_thiscall MyMainFrame::Streamer`adjustor{48}’ (class TBuffer &)” (?Streamer@MyMainFrame@@WDA@AEXAAVTBuffer@@@Z)

**ex2aLinkDef.h**

```auto
#pragma link C++ class MyMainFrame;

```

**myFrame.h**

```auto
#include <TQObject.h>
#include <TApplication.h>
#include <TGClient.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom3.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TRootEmbeddedCanvas.h>
#include <RQ_OBJECT.h>
#include "ex2aLinkDef.h"

class MyMainFrame : public TGMainFrame {

private:

	TRootEmbeddedCanvas* fEcanvas;

public:

	MyMainFrame(const TGWindow* p, UInt_t w, UInt_t h);

	virtual ~MyMainFrame();

	void DoDraw();

	void doClose(void);

	//ClassDef(MyMainFrame, 0)

};

```

**myFrame.cpp**

```auto
#include "myFrame.h"
#include "ex2aLinkDef.h"

MyMainFrame::MyMainFrame(const TGWindow* p, UInt_t w, UInt_t h)

	: TGMainFrame(p, w, h) {

	// Creates widgets of the example

	fEcanvas = new TRootEmbeddedCanvas("Ecanvas", this, 200, 200);

	AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,

		10, 10, 10, 1));

	TGHorizontalFrame* hframe = new TGHorizontalFrame(this, 200, 40);

	TGTextButton* draw = new TGTextButton(hframe, "&Draw");

	draw->Connect("Clicked()", "MyMainFrame", this, "DoDraw()");

	hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));

	TGTextButton* exit = new TGTextButton(hframe, "&Exit ",

		"gApplication->Terminate()");

	hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));

	AddFrame(hframe, new TGLayoutHints(kLHintsCenterX, 2, 2, 2, 2));

	// Sets window name and shows the main frame

	SetWindowName("Simple Example");

	MapSubwindows();

	Resize(GetDefaultSize());

	MapWindow();

}

void MyMainFrame::DoDraw() {

	// Draws function graphics in randomly chosen interval

	TF1* f1 = new TF1("f1", "sin(x)/x", 0, gRandom->Rndm() * 10);

	f1->SetFillColor(19);

	f1->SetFillStyle(1);

	f1->SetLineWidth(3);

	f1->Draw();

	TCanvas* fCanvas = fEcanvas->GetCanvas();

	fCanvas->cd();

	fCanvas->Update();

}

MyMainFrame::~MyMainFrame() {

	// Clean up used widgets: frames, buttons, layouthints

}

void MyMainFrame::doClose()

{

	gApplication->Terminate(0);

}

```

**RootProject.cpp**

```auto
#include <TApplication.h>
#include <TF1.h>
#include <TRandom3.h>
#include "myFrame.h"
#include "ex2aLinkDef.h"

int main(int argc, char** argv)
{
	TApplication theApp("App", &argc, argv);

	new MyMainFrame(gClient->GetRoot(), 200, 200);

	theApp.Run();

	return 0;

}

```

Thanks!

---

<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:** [July 5, 2019, 12:17pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/2 "2019-07-05T12:17:07Z")

</div>

And how do you compile it?

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 12:19pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/3 "2019-07-05T12:19:44Z")

</div>

I am running it in debug in VS.

---

<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:** [July 5, 2019, 12:24pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/4 "2019-07-05T12:24:56Z")

</div>

That was not my question… Anyway, you have to generate a dictionary for your project and include it in the list of source files. Something like:

```auto
rootcling -f exa2aDict.cxx -c myFrame.h ex2aLinkDef.h

```

(you can probably add this command in some custom build step in Visual Studio)  
Then add `exa2aDict.cxx` in the list of source files in your project.

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 12:32pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/5 "2019-07-05T12:32:00Z")

</div>

Thank you. I just put the line in cmd and it generated a exa2aDict.cxx. I then added this to the source files and ran the program. It is throwing me exceptions now in the exa2aDict.cxx file.

---

<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:** [July 5, 2019, 12:53pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/6 "2019-07-05T12:53:10Z")

</div>

OK, I’ll take a look and let you know.

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 12:55pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/7 "2019-07-05T12:55:37Z")

</div>

Alright thanks 😄

---

<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:** [July 5, 2019, 1:08pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/8 "2019-07-05T13:08:22Z")

</div>

OK, so doing it by hand works for me, if you keep (un-comment) `ClassDef(MyMainFrame, 0)` in `myFrame.h`, and if you export myMainFrame::DoDraw(). So in the command prompt, if I do:

```auto
C:\Users\bellenot\rootdev\Aaron>rootcling -f exa2aDict.cxx -c myFrame.h ex2aLinkDef.h

C:\Users\bellenot\rootdev\Aaron>cl -MD -GR -EHsc -I%ROOTSYS%\include myFrame.cpp exa2aDict.cxx RootProject.cpp /link -LIBPATH:%ROOTSYS%\lib libCore.lib libRIO.lib libHist.lib libGraf.lib libGpad.lib libGui.lib libMathcore.lib /export:?DoDraw@MyMainFrame@@QAEXXZ /out:ex2a.exe

```

then it works.

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 1:13pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/9 "2019-07-05T13:13:53Z")

</div>

> [@bellenot](#):
>
> ```auto
> C:\Users\bellenot\rootdev\Aaron>cl -MD -GR -EHsc -I%ROOTSYS%\include myFrame.cpp exa2aDict.cxx RootProject.cpp /link -LIBPATH:%ROOTSYS%\lib libCore.lib libRIO.lib libHist.lib libGraf.lib libGpad.lib libGui.lib libMathcore.lib /export:?DoDraw@MyMainFrame@@QAEXXZ /out:ex2a.exe
> 
> ```

Thank you. However when I put that second line in cmd it says " ‘cl’ is not recognised as an internal or external command, operable program or batch file."

---

<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:** [July 5, 2019, 1:53pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/10 "2019-07-05T13:53:43Z")

</div>

You have to use a `x86 Native Tools Command Prompt for VS 2019`

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 2:20pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/11 "2019-07-05T14:20:06Z")

</div>

Thank you.

I have done this in my Developer Command Prompt but there are still exceptions thrown in the exa2aDict.cxx whenever I run the program in VS.

This is the output I receive from the export in the VS Command Prompt.

```auto
C:\VS\myRootProject\myRootProject>rootcling -f exa2aDict.cxx -c myFrame.h ex2aLinkDef.h

C:\VS\myRootProject\myRootProject>cl -MD -GR -EHsc -I%ROOTSYS%\include myFrame.cpp exa2aDict.cxx myRootProject.cpp /link -LIBPATH:%ROOTSYS%\lib libCore.lib libRIO.lib libHist.lib libGraf.lib libGpad.lib libGui.lib libMathcore.lib /export:?DoDraw@MyMainFrame@@QAEXXZ /out:ex2a.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.21.27702.2 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

myFrame.cpp
C:\root_v6.18.00\include\ROOT\libcpp_string_view.h(193): warning C4068: unknown pragma
C:\VS\myRootProject\myRootProject\ex2aLinkDef.h(1): warning C4068: unknown pragma
C:\VS\myRootProject\myRootProject\ex2aLinkDef.h(1): warning C4068: unknown pragma
exa2aDict.cxx
C:\root_v6.18.00\include\ROOT\libcpp_string_view.h(193): warning C4068: unknown pragma
C:\VS\myRootProject\myRootProject\ex2aLinkDef.h(1): warning C4068: unknown pragma
myRootProject.cpp
C:\root_v6.18.00\include\ROOT\libcpp_string_view.h(193): warning C4068: unknown pragma
C:\VS\myRootProject\myRootProject\ex2aLinkDef.h(1): warning C4068: unknown pragma
C:\VS\myRootProject\myRootProject\ex2aLinkDef.h(1): warning C4068: unknown pragma
Generating Code...
Microsoft (R) Incremental Linker Version 14.21.27702.2
Copyright (C) Microsoft Corporation. All rights reserved.

/out:myFrame.exe
-LIBPATH:C:\root_v6.18.00\lib
libCore.lib
libRIO.lib
libHist.lib
libGraf.lib
libGpad.lib
libGui.lib
libMathcore.lib
/export:?DoDraw@MyMainFrame@@QAEXXZ
/out:ex2a.exe
myFrame.obj
exa2aDict.obj
myFirstRootProject.obj
   Creating library ex2a.lib and object ex2a.exp

```

---

<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:** [July 5, 2019, 2:25pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/12 "2019-07-05T14:25:51Z")

</div>

> [@Aaron](#):
>
> I have done this in my Developer Command Prompt but there are still exceptions thrown in the exa2aDict.cxx whenever I run the program in VS.

What do you mean? What kind of exception? Can you run (execute) `ex2a.exe` from that command prompt? If it works, then you have to adapt your project to match this simple example (in particular the compiler flags)

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 2:35pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/13 "2019-07-05T14:35:46Z")

</div>

I can execute the example ex2a.exe from the command prompt and it works.

The exception is:

Unhandled exception at 0x759A2C92 in myRootProject.exe: Microsoft C++ exception: std::bad\_array\_new\_length at memory location 0x012FF590.

thrown at line 173 of the exa2aDict.cxx which looks something like:

…

```auto
#undef _BACKWARD_BACKWARD_WARNING_H
)DICTPAYLOAD";
    static const char* classesHeaders[]={
"MyMainFrame", payloadCode, "@",
nullptr};

    static bool isInitialized = false;
    if (!isInitialized) {
      TROOT::RegisterModule("exa2aDict", //LINE 173
        headers, includePaths, payloadCode, fwdDeclCode,
        TriggerDictionaryInitialization_exa2aDict_Impl, {}, classesHeaders, /*has no C++ module*/false);
      isInitialized = true;
    }
  }
  static struct DictInit {
    DictInit() {
      TriggerDictionaryInitialization_exa2aDict_Impl();
    }
  } __TheDictionaryInitializer;
}
void TriggerDictionaryInitialization_exa2aDict() {
  TriggerDictionaryInitialization_exa2aDict_Impl();
}

```

I’m not sure what you mean by adapting my project to match the example.

---

<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:** [July 5, 2019, 2:39pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/14 "2019-07-05T14:39:20Z")

</div>

> [@Aaron](#):
>
> I’m not sure what you mean by adapting my project to match the example.

You have a Visual Studio solution, right? Then you have to set the options to have the same compiler flags than the ones used in the command prompt. or don’t use Visual Studio solution projects, but simply the command prompt…  
And you confirm that `ex2a.exe` doesn’t throw any exception, right?

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 2:43pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/15 "2019-07-05T14:43:58Z")

</div>

Yes I can confirm that running from the command prompt that ex2a.exe works correctly.

I do have a VS solution however I’m not sure how to set the compiler flags.

---

<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:** [July 5, 2019, 2:54pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/16 "2019-07-05T14:54:02Z")

</div>

Look in your solution’s properties

---

<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:** [July 5, 2019, 2:56pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/17 "2019-07-05T14:56:34Z")

</div>

see for example:

> **[Set C++ compiler and build properties in Visual Studio](https://docs.microsoft.com/en-us/cpp/build/working-with-project-properties?view=vs-2019)**
>
> Use the Visual Studio IDE to change C++ compiler and linker options and other build settings.

---

<div class="post-metadata">

**Author:** ![Aaron](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aaron/32/19136_2.png) [@Aaron](https://root-forum.cern.ch/u/Aaron)\
**Post date:** [July 5, 2019, 3:10pm UTC](https://root-forum.cern.ch/t/gui-issues-in-visual-c/35011/18 "2019-07-05T15:10:18Z")

</div>

Thanks. I have looked at that a bit now but I am unsure of where I should add compiler flags and where to find the compiler flags I need to add.
