LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   _Cdecl (https://www.linuxquestions.org/questions/programming-9/_cdecl-75517/)

Mohsen 07-24-2003 10:32 AM

_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?

kev82 07-24-2003 11:33 AM

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.

Mohsen 07-25-2003 04:08 AM

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.

kev82 07-25-2003 07:09 AM

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))
{
...
}

but all that does is say that the caller will be responsible for popping all pushed arguments off the stack when the function returns. i dont think this is what you want though, as im pretty sure this is the normal calling convention anyway.

Quote:

I want to define a function in a .cpp file not a .c file.
yes and thats what i said. in your cpp file any function you want to call from anything non-C++ should be prefixed extern "C"

Code:

extern "C" int a() { return 1; }
int b() { return 1; }

i put the above in a cpp and compiled it, then running nm on the output gives me 2 exported functions

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.

Mohsen 07-25-2003 11:20 PM

OK,
Thanks a lot.

kev82 07-26-2003 05:47 AM

just out of interest can i ask which solution you chose because im still not sure what your actual problem was.

Mohsen 07-26-2003 07:49 AM

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--

Mohsen 07-26-2003 07:57 AM

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?

kev82 07-26-2003 08:33 AM

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.