Using C-specific structures in C ++ code

I am trying to use C-sourced functions in my C ++ code. And I have some difficulties when I try to create an instance in my C ++ code structure, which is declared in the C-header, and then pass it to the C-function by value. Example:

dummyc.h:

#ifndef _DUMMY_C_H_
#define _DUMMY_C_H_

typedef struct {
    int a;
    int b;
    int c;
    int d;
}DUMMYS;

int dummyFunction(unsigned int a, unsigned int b, unsigned short c, DUMMYS dummy);

#endif

dummyc.c:

#include "dummyc.h"

int dummyFunction(unsigned int a, unsigned int b, unsigned short c, DUMMYS dummy){
    return 1;
}

dummycpp.cpp:

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

int main(){
    DUMMYS s = {0,0,0,0};
    return dummyFunction(50,50,1,s);
}

At runtime of dummyFunction, I see that the data on the stack is incorrect. It seems they have moved where ??? How can i do it right? Im using GCC 4.3.4 for ARM.

+4
source share
1 answer

'extern' C '' . , ++ . , '#ifdef __cplusplus'.

#ifndef _DUMMY_C_H_
#define _DUMMY_C_H_

typedef struct {
int a;
int b;
int c;
int d;
}DUMMYS;

#ifdef __cplusplus
extern "C" {
#endif

int dummyFunction(unsigned int a, unsigned int b, unsigned short c, DUMMYS dummy);

#ifdef __cplusplus
}
#endif

#endif

, C ++ ( , C, ++).

dummyc.h, "extern" C "' #include dummycpp.cpp.

+1

Source: https://habr.com/ru/post/1662643/


All Articles