Skip to main content
KFrey.1
Associate II
December 22, 2020
Solved

how to include C code in C++ and the opposite?

  • December 22, 2020
  • 4 replies
  • 4066 views

I have a problem that I cannot include C code in C++ and the opposite. Different sites suggest to solve this problem by including the header file of C in the header file of C++ (Model.hpp) like:

#ifdef __cplusplus

extern "C"{

#endif

#include "coreTransferBufferAPI.h"

#ifdef __cplusplus

      }

#endif

In the code file Model.cpp, I try to use a function called (CB_Display_GetValues(&values) from this header.

CB_DisplayValues_t is also defined in this header, but doesn't show any errors.

float Model::getTempValue()

{

      CB_DisplayValues_t values;

      if(CB_Display_GetValues(&values)==REPORT_OK)

      {

            return values.cellTemperature;

      }

      return NULL;

}

The call of the function (CB_Display_GetValues(&values)) gives an undefined reference error:

/../TouchGFX/gui/src/model/Model.cpp:32: undefined reference to `CB_Display_GetValues'

Do you have a solution for this problem or do you know how to solve it? How would including a C++ code in C work (opposite way)?

Thank you very much and a festive holiday, K.Frey1

This topic has been closed for replies.
Best answer by TDK

Calling C code from C++ is easy. Ensure the function is declared prior to calling it (typically by including the appropriate header) and just call the function. Perhaps there is a typo or other mistake which is preventing this from working in your case.

Calling C++ code from C is more complicated. You need to ensure the functions have C linkage by enclosing them in extern "C" braces, and then you just call them.

4 replies

TDK
TDKBest answer
December 22, 2020

Calling C code from C++ is easy. Ensure the function is declared prior to calling it (typically by including the appropriate header) and just call the function. Perhaps there is a typo or other mistake which is preventing this from working in your case.

Calling C++ code from C is more complicated. You need to ensure the functions have C linkage by enclosing them in extern "C" braces, and then you just call them.

"If you feel a post has answered your question, please click ""Accept as Solution""."
KFrey.1
KFrey.1Author
Associate II
December 24, 2020

Dear TDK, thank you for your reply.

As you can see, I did declare the c-type header-file prior to calling it and then called the function. I don't think it's a typo because I checked it several times and you can see the code above. What other mistake could it possibly be?

TDK
December 24, 2020

Where is CB_Display_GetValues defined (C or CPP file name, not header)? Is that file getting compiled? Examine the console log and show proof.

"If you feel a post has answered your question, please click ""Accept as Solution""."
KFrey.1
KFrey.1Author
Associate II
December 26, 2020

TDK, you were right "CB_Display_GetValues" a function in the c-headerfile was not properly declared. Thank you, and sorry for your trouble