C/C++ Programmer's Guide (G06.25+)

Compiling and Linking TNS/R Native C and C++
Programs
HP C/C++ Programmer’s Guide for NonStop Systems—429301-008
16-18
Linking a Module
5. Compiling PIC (Position-Independent Code) using the default native C++ dialect
(VERSION3 ). The example program (MEXE) uses a DLL (named NDLL) compiled
from a library file named NC, which contains the getnum() function. MEXE
imports getnum() and prints the result (31).
Note the use of the import$ and export$ keywords, the SHARED pragma (to
compile the library) and CALL_SHARED pragma (to compile the main module), and
the ld and rld utilities (the PIC linker and loader). The result is a dynamic-link
library (DLL) named NDLL:
Source file (named MC):
import$ extern int getnum();
int main()
{
int x = -99;
x = getnum();
printf (ā€œx was -99; is now %dā€, x);
return 0;
}
Library for DLL (file name NC):
export$ int getnum()
{
return 31;
}
Compiler and Linker Commands:
nmc / in NC, out NLST / NDLL; shared
== Compile NC with shared (as a DLL); linkfile is PIC
nmc / in MC, out MLST / MOBJ; call_shared
== Compile MC module with call_shared; linkfile is PIC
ld / out LLST / mobj $SYSTEM.SYSTEM.CCPPMAIN -obey &
$SYSTEM.SYSTEM.LIBCOBEY -libvol $myvol.svol -lib NDLL &
-o MEXE
== Build MEXE, specifying CCPPMAIN (CRE component).
== LIBCOBEY specifies standard SRLs to be searched.