Name scope and name space

In programming languages, the name scope is defined as the portion of an application within which a particular declaration applies or is known. Name space is defined as the portion of a load module within which a particular declaration applies or is known. These two concepts determine whether a particular declaration in one language will map to a reference in another language.

In C, the name space is determined by compiler options or specific compiler directives. Programs compiled with NORENT place variable declarations in CSECTs. Programs compiled with RENT refer to a writable static area that contains static, external, and string literal variables. For more information about the writable static area, see z/OS XL C/C++ Programming Guide.

The XL C++ compiler acts as if everything was compiled with RENT.

The two techniques for creating an executable program are:
  • When the executable program is to be stored in a PDSE or HFS, use the binder to combine the output from the XL C/C++ compiler.
  • When the executable program is to be stored in a PDS, use the Language Environment Prelinker Utility to combine the output from the XL C/C++ compiler and pass the prelinker output to the binder.

To map data in C to C++ ILC, there are 3 major cases:

  1. C compiled with the RENT option statically bound to C++

    C++ and C storage areas that result from compiling with RENT are mapped together at prelink time or at bind time when the Prelinker is not used and the output from the binder goes to the HFS or to a member in a PDSE. The name scope is the module boundary and external data will map to each other. The LONGNAME compiler option or the #pragma map preprocessor directive may be required to allow mixed-case, or long named external references, to resolve.

  2. C compiled without the RENT option statically bound to C++
    C++ storage is placed in a different location than C NORENT storage. Therefore, by default, C++ will not look for NORENT C storage in the right place unless you give the XL C++ compiler the #pragma variable(varname,norent) directive.
    Sample C usage C++ subroutine
    #include <stdio.h>
    
    float mynum = 2;
    main() {
      int result, y;
      int *x;
    
      y=5;
      x=&y;
    
      result = cppfunc(x);
        /* by reference */
      if (y==6 && mynum==3)
      printf("It worked!\n");
    }
    #include <stdio.h>
    
    #pragma variable(mynum,norent)
    extern float mynum;
    extern "C" {
      int extern cppfunc( int * );
    }
    
    cppfunc( int *newval )
    {      // receive into pointer
      *newval = *newval + 1;
      mynum = mynum + 1;
      return *newval;
    }
    Alternatively, you can specify to the XL C++ compiler that a specific variable is RENT by specifying #pragma variable(varname,rent) in C.
    Sample C usage C++ subroutine
    #include <stdio.h>
    #pragma variable(mynum,rent)
    
    float mynum = 2;
    main() {
      int result, y;
      int *x;
    
      y=5;
      x=&y;
    
      result = cppfunc(x);
        /* by reference */
      if (y==6 && mynum==3)
      printf("It worked!\n");
    }
    #include <stdio.h>
    
    extern float mynum;
    extern "C" {
      int extern cppfunc( int * );
    }
    
    cppfunc( int *newval )
    {      // receive into pointer
      *newval = *newval + 1;
      mynum = mynum+ 1;
      return *newval;
    }
  3. A C to C++ DLL application

    A DLL application can access any declaration in its own module as well as referencing, implicitly or explicitly, any declaration from another DLL in the same enclave. For further information about what DLLs are and how to use them, see z/OS XL C/C++ Programming Guide.