C++ Programming
C++ Support
The WM IoT SDK is primarily written in C and provides C language APIs, but it also supports application development in C++.
Exception handling mechanism
iostream
C/C++ Mixed Programming
The WM IoT SDK supports mixed C/C++ programming, meaning code written in C can call code written in C++, and vice versa.
When different parts of an application are developed in C and C++, special attention must be paid to the calling rules of mixed C/C++ programming.
Calling C++ Code from C
To call a C++ function from C code, the C++ function must be declared and defined with C linkage using extern "C"
:
// Declaration in .h file:
#ifdef __cplusplus
extern "C" {
#endif
void my_cpp_func(void);
#ifdef __cplusplus
}
#endif
// Definition in .cpp file:
extern "C" void my_cpp_func(void)
{
// ...
}
Calling C Code from C++
To call a C function from C++, the C function must be declared with C linkage:
// Declaration in .h file:
#ifdef __cplusplus
extern "C" {
#endif
void my_c_func(void);
#ifdef __cplusplus
}
#endif
// Definition in .c file:
void my_c_func(void)
{
// ...
}
Defining main
in C++
The WM IoT SDK expects the application entry point main
to be defined with C linkage. When main
is defined in a .cpp source file, it must be marked with extern "C"
:
extern "C" void main()
{
// ...
}