_Cdecl
I want to write a function in a .cpp file, which compiled according to C patterns. I thought I should use this:
_Cdecl MyFunc(){} or __Cdecl or __cdecl or... I have tried every combination of underscore(s) and C|c and decl, but there seems that the word is undefined to g++. What should I do? |
i think you mean you want to be able to access a function in a cpp file from a c file. this requires that the function is linked C-style so must be defined extern "C" if that isnt what you want please ask again.
|
Thanks,
No, I want to define a function in a .cpp file not a .c file. I thick that something like cdecl should be defined for g++ that it behave with such a function: cdecl int MyFunc() {} according to c style functions. there should be also some others like stdcall, fastcall, _Pascal, and so on. |
you can declare a function to use the cdecl(or stdcall or any other) calling convention with gcc like this:
Code:
void function() __attribute__ ((cdecl)) Quote:
Code:
extern "C" int a() { return 1; } a _Z1bv notice a() has C style linkage and b() has C++ style linkage. if neither of the above was what you want can you post some example code so i can understand what you mean. |
OK,
Thanks a lot. |
just out of interest can i ask which solution you chose because im still not sure what your actual problem was.
|
I wanted to use a C program in my cpp code, so I tried to use the C code as is wihtout any changes (e.g. changing c to cpp), so that the C program work in my cpp code. So I wrote iit - as you mentioned - like this:
extern "C" { // Here is my C code } ____________________________ I thought that 'extern "C" ' is for when we have an object code and want to link it to a cpp code, so I thought that the only use of extern "C" is for something like this (only protoype): extern "C" int printf (char* format, ...); And now I leaned that it's not the only usage ;) --Sorry if I could not express my meaning-- |
Another thing, shouldn't C style linkage be like this:
int MyFunct(){return 1;} ==> _MyFunc ?? The function name with a underscore behind. Am I right? |
it is with most windows compilers but it doesnt seem to be in unix.
|
All times are GMT -5. The time now is 09:08 AM. |