The extern storage class specifier

A function that is declared with the extern storage class specifier has external linkage, which means that it can be called from other translation units. The keyword extern is optional; if you do not specify a storage class specifier, the function is assumed to have external linkage.

C++ only. An extern declaration cannot appear in class scope.

C++ only

You can use the extern keyword with arguments that specify the type of linkage.

Read syntax diagramSkip visual syntax diagramextern function storage class specifier syntax
 
>>-extern--"--linkage_specification--"-------------------------><
 

All platforms support the following values for linkage_specification:

IBM extension See chapter 25 "Working with Multi-Language Applications" in the ILE C/C++ Programmer’s Guide for additional language linkages supported by ILE C++.

The following fragments illustrate the use of extern "C" :

extern "C" int cf();      //declare function cf to have C linkage

extern "C" int (*c_fp)(); //declare a pointer to a function,
                          // called c_fp, which has C linkage

extern "C" {
  typedef void(*cfp_T)(); //create a type pointer to function with C
                          // linkage
  void cfn();             //create a function with C linkage
  void (*cfp)();          //create a pointer to a function, with C
                          // linkage
}

Linkage compatibility affects all C library functions that accept a user function pointer as a parameter, such as qsort. Use the extern "C" linkage specification to ensure that the declared linkages are the same. The following example fragment uses extern "C" with qsort.

#include <stdlib.h>

// function to compare table elements
extern "C" int TableCmp(const void *, const void *); // C linkage
extern void * GenTable();                            // C++ linkage

int main() {
  void *table;

  table = GenTable();               // generate table
  qsort(table, 100, 15, TableCmp);  // sort table, using TableCmp
                                    // and C library routine qsort();
}

While the C++ language supports overloading, other languages do not. The implications of this are:

End of C++ only

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]