BREW Forums  
Go Back   BREW Forums > BREW Extensions > FlashLite
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 03-22-2006, 01:48 AM
srhandore srhandore is offline
Registered User
 
Join Date: Feb 2006
Posts: 15
Rep Power: 0
srhandore is on a distinguished road
extern "c"

In the following code snippet if I remove the the extern "c" specifier then also my code works fine. Then can someone please tell me why do we use this identifier.
It is used when we want to merge some code of c to c++. But in our case as in brew this is not intended.Kindly help


extern "C"
{

int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
{
*ppObj = NULL;

if(ClsId == AEECLSID_HELLOWORLD)
{
if(AEEApplet_New(sizeof(HelloWorld), ClsId, pIShell,po,(IApplet**)ppObj,
(AEEHANDLER)HelloWorld::HandleEvent,(PFNFREEAPPDAT A)HelloWorld::freeAppData))
{
if(HelloWorld::initAppData((IApplet *) *ppObj))
{
return (AEE_SUCCESS);
}
}
}
return (EFAILED);
}

}
Reply With Quote
  #2  
Old 03-22-2006, 02:38 AM
Skavenger's Avatar
Skavenger Skavenger is offline
Registered User
 
Join Date: May 2004
Posts: 460
Rep Power: 6
Skavenger is off the scale
The underlying brew interface when it calls the function "AEEClsCreateInstance" uses the "C" calling convention as opposed to the "C++" one. This is just to specify to the compiler and linker how this function expects its parameters.
__________________
- Skavenger
Reply With Quote
  #3  
Old 03-22-2006, 09:50 PM
srhandore srhandore is offline
Registered User
 
Join Date: Feb 2006
Posts: 15
Rep Power: 0
srhandore is on a distinguished road
extern "c"

First of all thank you very much for your reply.
But sir, my question is if I remove the extern "c" keyword then also my program is successfully excecuting. Then why to use it.
Is it like we don't want the functions in the code under extern "c" to be overloaded. And to avoid them getting overloaded we are using extern "c".
or
Is it like, in the case where I am getting the intended results without extern "c" but may be there would be some situations where it'll fail to do so.
Kindly help.
Reply With Quote
  #4  
Old 03-23-2006, 02:17 AM
Skavenger's Avatar
Skavenger Skavenger is offline
Registered User
 
Join Date: May 2004
Posts: 460
Rep Power: 6
Skavenger is off the scale
If your program is compiled via a c compiler (or C++ compiler with C++ extensions turned off) or from a .c as opposed to a .cpp file then the compiler will not require that extern "c". The reason that its there is if your program is developed in C++ as opposed to standard c.
__________________
- Skavenger
Reply With Quote
  #5  
Old 03-23-2006, 12:48 PM
rabidcow's Avatar
rabidcow rabidcow is offline
Registered User
 
Join Date: May 2005
Posts: 157
Rep Power: 5
rabidcow is on a distinguished road
extern "c" doesn't specify a calling convention so much as it instructs the C++ compiler to use C name mangling rather than C++ (ie, leave out the data types, it can't be overloaded across compilation units and C code can find it.)

You never need to wrap the actual function body. Since these functions must be visible in more than one compilation unit, they will almost universally have a prototype in a header file (in this case AEEAppGen.h, IIRC). Having an extern on the prototype is sufficient, assuming you include that file. (Also you don't need it if AEEClsCreateInstance is in a .c source file, but I think the extern "c" would be an error in that case...)
Reply With Quote
  #6  
Old 03-23-2006, 09:37 PM
deepti1 deepti1 is offline
Registered User
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0
deepti1 is on a distinguished road
C++ brew project

sir,
I want to know how to create C++ project in brew.when i create an empty project it generates .c file .but i want to generate .cpp file.
Reply With Quote
  #7  
Old 03-24-2006, 02:32 AM
Skavenger's Avatar
Skavenger Skavenger is offline
Registered User
 
Join Date: May 2004
Posts: 460
Rep Power: 6
Skavenger is off the scale
Quote:
extern "c" doesn't specify a calling convention so much as it instructs the C++ compiler to use C name mangling rather than C++ (ie, leave out the data types, it can't be overloaded across compilation units and C code can find it.)


Correct.

Quote:
You never need to wrap the actual function body. Since these functions must be visible in more than one compilation unit, they will almost universally have a prototype in a header file (in this case AEEAppGen.h, IIRC). Having an extern on the prototype is sufficient, assuming you include that file.


Surely you'd need to extern "c" the actual function if its contained within a cpp file, otherwise you'd end up with two function declarations (Once names have been mangled).

Quote:
(Also you don't need it if AEEClsCreateInstance is in a .c source file, but I think the extern "c" would be an error in that case...)


I'd have said more of a warning as to most compilers its just as a switch.
__________________
- Skavenger
Reply With Quote
  #8  
Old 03-24-2006, 12:34 PM
rabidcow's Avatar
rabidcow rabidcow is offline
Registered User
 
Join Date: May 2005
Posts: 157
Rep Power: 5
rabidcow is on a distinguished road
Quote:
Originally Posted by Skavenger
Surely you'd need to extern "c" the actual function if its contained within a cpp file, otherwise you'd end up with two function declarations (Once names have been mangled).


extern "c" void f(void);
void f(void) { }

is fine, so long as the compiler sees both of them. The extern from the declaration carries over to the actual function definition. After all, they're exactly the same function: same name, same type.

Now if you don't have the prototype included in the compilation unit, then you would need to have:

extern "c" void f(void) { }

But it's probably a better idea to just make sure to include a header, so you don't end up with mismatched type signatures. (get a link error instead of a crash or general strange runtime behavior)

Quote:
Originally Posted by Skavenger
I'd have said more of a warning as to most compilers its just as a switch.


I dunno, just ISTM that extern "c" would be generate an error in a C source file... haven't tried it recently. But there must be some reason why they're always wrapped in #ifdef __cplusplus...
Reply With Quote
  #9  
Old 03-27-2006, 03:14 AM
Skavenger's Avatar
Skavenger Skavenger is offline
Registered User
 
Join Date: May 2004
Posts: 460
Rep Power: 6
Skavenger is off the scale
Quote:
I dunno, just ISTM that extern "c" would be generate an error in a C source file... haven't tried it recently. But there must be some reason why they're always wrapped in #ifdef __cplusplus...


Thats true, haven't tried it in ages but i think VC6 used to just warn you about it. (Dependant on error and warning levels of course). As for .net or 2005 I haven't tried it.

Quote:
is fine, so long as the compiler sees both of them. The extern from the declaration carries over to the actual function definition. After all, they're exactly the same function: same name, same type.

Exactly. But if your not going to call it from outside of your source file, I don't see the point in adding it to your header file, just seems a waste of time and effort to me.
__________________
- Skavenger
Reply With Quote
  #10  
Old 03-27-2006, 12:42 PM
rabidcow's Avatar
rabidcow rabidcow is offline
Registered User
 
Join Date: May 2005
Posts: 157
Rep Power: 5
rabidcow is on a distinguished road
Quote:
Originally Posted by Skavenger
Exactly. But if your not going to call it from outside of your source file, I don't see the point in adding it to your header file, just seems a waste of time and effort to me.


Sure, but if you're not going to call it from outside of your source file why do you need extern "c" at all? Might as well make it static even.

In the specific case of AEEClsCreateInstance it's already declared in a header, you just have to include it. And you need that header anyway for the generic applet structure.
Reply With Quote
  #11  
Old 03-28-2006, 01:19 AM
Skavenger's Avatar
Skavenger Skavenger is offline
Registered User
 
Join Date: May 2004
Posts: 460
Rep Power: 6
Skavenger is off the scale
Quote:
Sure, but if you're not going to call it from outside of your source file why do you need extern "c" at all? Might as well make it static even.

If you "static" the function it will NOT be available outside of the current source file. Brew needs access to this funciton therefore static won't work. But because the function is already pre-defined (in their Brew Code) then you don't need to put it in a header.
__________________
- Skavenger
Reply With Quote
  #12  
Old 03-28-2006, 01:47 PM
rabidcow's Avatar
rabidcow rabidcow is offline
Registered User
 
Join Date: May 2005
Posts: 157
Rep Power: 5
rabidcow is on a distinguished road
Quote:
Originally Posted by Skavenger
If you "static" the function it will NOT be available outside of the current source file. Brew needs access to this funciton therefore static won't work. But because the function is already pre-defined (in their Brew Code) then you don't need to put it in a header.


That's my point. It's just that I consider AEEAppGen to be part of your code and not part of BREW, because it's just another compilation unit that gets linked into your binary.
Reply With Quote
  #13  
Old 03-29-2006, 01:33 AM
Skavenger's Avatar
Skavenger Skavenger is offline
Registered User
 
Join Date: May 2004
Posts: 460
Rep Power: 6
Skavenger is off the scale
Well it is and it isn't. If you rewrote aeeappgen and aeemodgen then you could call the function whatever you wanted to.

However from experience I know a lot of companies that would prefer you not to rewrite those modules.
__________________
- Skavenger
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump




All times are GMT -7. The time now is 10:46 PM.

BREW 3.x Technical Documentation BREW Developer - Application Development Home BREW Customer Support Portal
BREW Training BREW Developer Labs



REGISTER | USER CP | CALENDAR | MEMBERS | FAQ | SEARCH | FORUMS HOME
QUALCOMM HOME | CONTACT US | SEARCH | SITE MAP | LEGAL | PRIVACY



Powered by: vBulletin Version 3.0.13
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.