Since C++ is for the most part a superset of C, calling
between C and C++ is a normal operation. You should,
however, be aware of the following:
Using the extern "C" Linkage Specification
To handle overloaded function names the HP aC++ compiler generates
new, unique names for all functions declared in a C++ program. To do
so, the compiler uses a function-name encoding scheme that is
implementation dependent. A linkage directive tells the compiler to
inhibit this default encoding of a function name for a
particular function.
If you want to call a C function from a C++ program, you must tell
the compiler not to use its usual encoding scheme when you declare
the C function. In other words, you must tell the compiler not to
generate a new name for the function. If you do not turn off the
usual encoding scheme, the function name declared in your C++
program will not match the function name in your C module defining
the function. If the names do not match, the linker cannot resolve
them. To avoid these linkage problems, use a linkage
directive when you declare the C function in the C++ program.
Syntax:
All HP aC++ linkage directives must have either of the following formats:
extern "C" function_declaration
extern "C"
{
function_declaration1
function_declaration2
...
function_declarationN
}
Example:
The following declarations are equivalent:
extern "C" char* get_name(); // declare the external C module
and
extern "C"
{
char* get_name(); // declare the external C module
}
You can also use a linkage directive with all the
functions in a file, as shown in the following example.
This is useful if you wish to use C library functions
in a C++ program.
extern "C"
{
#include "myclibrary.h"
}
Note: Do not use extern "C" when including standard C
header files because these header files already contain extern
"C" directives.
Although the string literal following the extern keyword
in a linkage directive is implementation-dependent, all implementations
must support C and C++ string literals.
Refer to "Linkage Specifications" in The C++ Programming Language,
Third Edition for more information.
Differences in Argument Passing Conventions
If your C++ code calls functions written in C, you should
make sure that the called C functions do not use function
prototypes that suppress argument widening. If they do, your C++ code
will be passing wider arguments than your C code is expecting.
The main() Function
When mixing C++ modules with C modules, the overall
control of the program must be written in C++, with two exceptions.
In other words, the main() function should appear in some
C++ module, rather than in a C module. The exceptions are C++
programs and libraries, including HP-supplied libraries, without
any global class objects containing constructors or destructors and
C++ programs and libraries, including HP-supplied libraries, without
static objects.
Examples: HP aC++ Calling HP C
The following examples show a C++ program, calling_c.C,
that calls a C function, get_name(). The C++
program contains a main() function.
//************************************************************
// This is a C++ program that illustrates calling a function *
// written in C. It calls the get_name() function, which is *
// in the "get_name.c" module. The object modules generated *
// by compiling the "calling_c.C" module and by compiling *
// the "get_name.c" module must be linked to create an *
// executable file. *
//************************************************************
#include <iostream.h>
#include "string.h"
//************************************************************
// declare the external C module
extern "C" char* get_name();
class account
{
private:
char* name; // owner of the account
protected:
double balance; // amount of money in the account
public:
account(char* c) // constructor
{ name = new char [strlen(c) +1];
strcpy(name,c);
balance = 0; }
void display()
{ cout << name << " has a balance of "
<< balance << "\n"; }
};
int main()
{
account* my_checking_acct = new account (get_name());
// send a message to my_checking_account to display itself
my_checking_acct->display();
}
The following example shows the module
get_name.c. This function is called by the C++ program.
/****************************************************/
/* This is a C function that is called by main() in */
/* a C++ module, "calling_c.C". The object */
/* modules generated by compiling this module and */
/* by compiling the "calling_c.C" module must be */
/* linked to create an executable file. */
/****************************************************/
#include <stdio.h>
#include "string.h"
char* get_name()
{
static char name[80];
printf("Enter the name: ");
scanf("%s",name);
return name;
}
/****************************************************/
Running the Example Program:
Here is a sample run of the executable file that results
when you link the object modules generated by compiling
calling_c.C and get_name.c:
Enter the name: Joann
Joann has a balance of 0
|