4

I'm trying to use C-sourced functions in my C++ code. And I have some difficulties, when I try to instantiate in my C++ code structure which is declared in C-header, and then transfer it to 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);
}

During dummyFunction execution I see that data on stack is incorrect. It seems like they where shifted?? How I can do this correctly??? Im using GCC 4.3.4 for ARM.

Ivan Pankov
  • 178
  • 1
  • 7
  • 5
    Could you please print out the result in the console to confirm what you are saying. At my end everything looks right. – Md Monjur Ul Hasan Dec 02 '16 at 14:00
  • 3
    With the example code you show, the only thing that's "bad" is that you use a symbol with a leading underscore followed by an upper-case letter (the header include guard). [Such symbols are reserved](http://stackoverflow.com/a/228797/440558). Other than that I see nothing wrong as long as you compile both source files with compilers targeting the same ABI. – Some programmer dude Dec 02 '16 at 14:00
  • 5
    Also, can you please explain how you "see that data on stack is incorrect"? – Some programmer dude Dec 02 '16 at 14:02
  • You can build constructors for structures in C++, if that could perhaps help. – Rorschach Dec 02 '16 at 14:11
  • @Rorschach: How does that help? – Lightness Races in Orbit Dec 02 '16 at 14:30
  • @LightnessRacesinOrbit just making OP aware of that. – Rorschach Dec 02 '16 at 14:33
  • It is the kind of mishap you'd see if the declaration of the function does not match the actual definition of the function. Either because it doesn't have the same number of arguments or because an argument type changed. Easy mistake if, say, the function is included in a library. Rebuild the library. – Hans Passant Dec 02 '16 at 16:00

1 Answers1

1

The 'extern "C"' directive really only matters for the function declarations. This is because C++ has function overloading. I've always embedded the directive around the function signatures in my header files, using '#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

There's no difference in how C and C++ puts members into structs (when the struct is entirely composed of things that are legal in both C and C++).

When you make this change in dummyc.h, you can remove the 'extern "C"' around the #include directive in dummycpp.cpp.

dembones
  • 331
  • 3
  • 6