Online Book Reader

Home Category

Professional C__ - Marc Gregoire [435]

By Root 1181 0
if your library source is in C++, at the definition site (to instruct the library code to generate a name compatible with the specified language).

The syntax of extern "language" is as follows:

extern "language" declaration1();

extern "language" declaration2();

Or:

extern "language" {

declaration1();

declaration2();

}

The C++ standard says that any language specification can be used, so in principle the following could be supported by a compiler:

extern "C" MyFunc(int i);

extern "FORTRAN" MatrixInvert(Matrix* M);

extern "Pascal" SomeLegacySubroutine(int n);

extern "Ada" AimMissileDefense(double angle);

In practice, many compilers only support "C". Each compiler vendor will inform you which language designators they support.

For example, in the following code, the function prototype for doCFunction() is specified as an external C function:

extern "C" {

void doCFunction(int i);

}

int main()

{

doCFunction(8); // Call the C function.

return 0;

}

The actual definition for doCFunction() is provided in a compiled binary file attached in the link phase. The extern keyword informs the compiler that the linked-in code was compiled in C.

A more common pattern for using extern is at the header level. For example, if you are using a graphics library written in C, it probably came with a .h file for you to use. You can write another header file that wraps the original one in an extern block to specify that the entire header defines functions written in C. The wrapper .h file is often named with .hpp to distinguish it from the C version of the header:

// graphicslib.hpp

extern "C" {

#include "graphicslib.h"

}

Another common model is to write a single header file, which is conditioned on whether it is being compiled for C or C++. A C++ compiler predefines the symbol __cplusplus if you are compiling for C++. The symbol is not defined for C compilations. So you will often see header files in the following form:

#ifdef __cplusplus

extern "C" {

#endif

declaration1();

declaration2();

#ifdef __cplusplus

} // matches extern "C"

#endif

This means that declaration1() and declaration2() are functions that are in a library compiled by the C compiler. Using this technique, the same header file can be used in both C and C++ clients.

Whether you are including C code in your C++ program or linking against a compiled C library, remember that even though C++ is essentially a superset of C, they are different languages with different design goals. Adapting C code to work in C++ is quite common, but providing an object-oriented C++ wrapper around procedural C code is often much better.

Mixing C# with C++

Even though this is a C++ book, we won’t pretend that there aren’t newer and snazzier languages out there. One example is C#. By using the Interop services from C#, it’s pretty easy to call C++ code from within your C# applications. An example scenario could be that you develop parts of your application, like the graphical user interface, in C#, but use C++ to implement certain performance-critical components. To make Interop work, you need to write a library in C++, which will be called from C#. On Windows, the library will be in a .DLL file. The following C++ example defines a FunctionInDLL() function that will be compiled into a library. The function accepts a Unicode string and returns an integer. The implementation writes the received string to the console and returns the value 42 to the caller:

#include

using namespace std;

extern "C"

{

__declspec(dllexport) int FunctionInDLL(const wchar_t* p)

{

wcout << L"The following string was received by C++:\n '";

wcout << p << L"'" << endl;

return 42; // Return some value...

}

}

Code snippet from CSharp\HelloCpp.cpp

Keep in mind that you are implementing a function in a library, not writing a program; so, you will not need a main() function. Compiling this code depends on your environment. If you are using Microsoft Visual C++, you need to go to the properties of your project and select “Dynamic Library (.dll)” as the configuration type. Note

Return Main Page Previous Page Next Page

®Online Book Reader