LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2003, 11:32 AM   #1
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
_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?
 
Old 07-24-2003, 12:33 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 07-25-2003, 05:08 AM   #3
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Original Poster
Rep: Reputation: 30
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.
 
Old 07-25-2003, 08:09 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 07-26-2003, 12:20 AM   #5
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Original Poster
Rep: Reputation: 30
OK,
Thanks a lot.
 
Old 07-26-2003, 06:47 AM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
just out of interest can i ask which solution you chose because im still not sure what your actual problem was.
 
Old 07-26-2003, 08:49 AM   #7
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Original Poster
Rep: Reputation: 30
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--
 
Old 07-26-2003, 08:57 AM   #8
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Original Poster
Rep: Reputation: 30
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?
 
Old 07-26-2003, 09:33 AM   #9
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
it is with most windows compilers but it doesnt seem to be in unix.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration