$ tree
.
|-- external.c
|-- sc.h
`-- serial_comm.cpp
$ cat external.c
#include "sc.h"
int main() {
struct MyStruct* sc;
sc->myFunction1();
}
$ cat sc.h
#ifndef SC_H
#define SC_H
struct MyStruct {
int myVariable;
void myFunction1();
void myFunction2();
};
#endif
$ cat serial_comm.cpp
#include <iostream>
#include "sc.h"
extern "C" void MyStruct::myFunction1() {
std::cout << "Function 1 called" << std::endl;
}
extern "C" void MyStruct::myFunction2() {
std::cout << "Function 2 called" << std::endl;
}
$ gcc external.c serial_comm.cpp -o my_program -lstdc++ -std=c++17
cc1: warning: command-line option '-std=c++17' is valid for C++/ObjC++ but not for C
In file included from external.c:1:
sc.h:6:10: error: field 'myFunction1' declared as a function
6 | void myFunction1();
| ^~~~~~~~~~~
sc.h:7:10: error: field 'myFunction2' declared as a function
7 | void myFunction2();
| ^~~~~~~~~~~
I have a project now that requires calling a C++17 structure’s function within C language. I expect to receive ‘Function 1 called’.
The error message I’m getting is as follows. How should I correct it? Thank you.