+ Reply to Thread
Results 1 to 2 of 2

Thread: What is extern "C"?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    49

    What is extern "C"?

    What is extern "C"?

    How to use it in C++?

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Function implemented in C++ have name mangling issue.

    Most of the time, you can't use them directly in c code.

    Using extern "C" in C++ code, specifies the compiler that it should follow the c linkage.

    This will allow you to use the function in "C" client code.

    Example:

    Code:
    //yoursoure.h header file...
    #ifdef __cplusplus
    extern "C"
    {
    #endif
    
    	char* GiveMeDigestForThisString(char *pszInputstring) ;
    
    
    #ifdef __cplusplus
    }
    #endif
    
    //yoursource.cpp file...
    char* GiveMeDigestForThisString(char *pszInputstring)
    {
    	//process pszInputstring and return char * to the caller
    	return "Hi, this is your digest" ;
    }
    
    //client C code
    void UseExternC()
    {
    	char szInputstring[] = "Hello World!!!" ;
    	char* pszDigest = GiveMeDigestForThisString(szInputstring) ;
    }
    Note:
    This is very useful while exporting a function from a DLL and using C++. C Client code can easily use them.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts