Skip to main content
ET.2
Associate II
March 1, 2023
Solved

TouchGFX showing undefined reference to global variable

  • March 1, 2023
  • 1 reply
  • 2234 views

I am new to TouchGFX, I ran into a problem that using TouchGFX 4.21.1 on STM32H743Eval 2 , I add a global variable (e.g. counter) within "USER CODE BEGIN 0", , in StartDefaultTask, add counter++ in the main.c file, follow MVP design procedure, just want to dispaly counter value in TextTArea widget, CubeIDE code compiling is ok, but when click "run simulator", it appers "C:\TouchGFXProjects\STM32H743\TouchGFX/gui/src/model/Model.cpp:13: undefined reference to `counter'

I am also using 4.18.0 of touchgfx, both have same symptom, is there any setting I missed, please help, thanks in advance.

This topic has been closed for replies.
Best answer by SKacp.1

Hi

You can't use with simulator global variables from Your main.c.

If You won't check Your program with global variables in simulator You must use #ifndef SIMULATOR in Model.cpp

#ifndef SIMULATOR
 
extern "C"
{
	#include "main.h"
}
 
#endif

1 reply

SKacp.1
Associate
March 1, 2023

Hello,

Please use extern "C" and include Your "h" file with global variables in Model.cpp.

extern "C"
{
	#include "main.h"
}

ET.2
ET.2Author
Associate II
March 1, 2023

Thanks for the reply, I add exter C as you suggest as below:

#include <gui/model/Model.hpp>

#include <gui/model/ModelListener.hpp>

extern "C"

{

#include "main.h"

}

extern int counter;

Model::Model() : modelListener(0)

{

}

void Model::tick()

{

modelListener->indxpresenter(counter);

}

but, ​it shows no main.h as following...

Compile

    make -f simulator/gcc/Makefile -j8

    Reading ./application.config

    Compiling gui/src/model/Model.cpp

    gui/src/model/Model.cpp:6:11: fatal error: main.h: No such file or directory

     #include "main.h"

          ^~~~~~~~

    compilation terminated.

SKacp.1
SKacp.1Best answer
Associate
March 1, 2023

Hi

You can't use with simulator global variables from Your main.c.

If You won't check Your program with global variables in simulator You must use #ifndef SIMULATOR in Model.cpp

#ifndef SIMULATOR
 
extern "C"
{
	#include "main.h"
}
 
#endif