Significance of library search order

The order in which the libraries in SYSLIB are concatenated is significant.

Example: Suppose that functions f1() and f4() are resolved from SYSLIB:
  /* file: unit0.c */
  extern int f1(void);  /* from member UNIT1 of library LIB1 */
  extern int f4(void);  /* from member UNIT2 of library LIB2 */
  int main() {
    int rc1, rc4;
    rc1 = f1();
    rc4 = f4();
    if (rc1 !=  1) printf("fail rc1 is %d-n", rc1);
    if (rc4 != 40) printf("fail rc1 is %d-n", rc4);
    return 0;
  }
SYSLIB defines the libraries USERID.LIB1 with members UNIT1 and UNIT2, and USERID.LIB2 with members of the same name but different contents.
The library members are compiled from the following:
/* member UNIT1 of library LIB1 */
int f1(void) { return 1; }

/* member UNIT2 of library LIB1 */
int f2(void) { return 2; }

/* member UNIT1 of library LIB2 */
int f1(void) { return 10; }

/* member UNIT2 of library LIB2 */
int f2(void) { return 20; }
int f3(void) { return 30; }
int f4(void) { return f2()*2; /* 40 */ }

When bound with ALIASES(ALL), or when the EDCALIAS utility is used, all defined symbols are seen in a library directory as aliases that indicate the library member that contains their definition.

There are two definitions of f1(), but library search of SYSLIB for f1 searches library LIB1 first, and finds alias f1 of member UNIT1. It reads in that member, and the call to f1() returns 1. Library search of SYSLIB for f4 searches LIB1 first, and does not find a definition. It then searches LIB2, and finds alias f4 of member UNIT2 of library LIB2. So UNIT2 of library LIB2 is read in resolving not only f4, but also f2 and f3, and the call to f4() returns 40. UNIT2 of library LIB1 is not read by mistake because an alias indicates not only the member name, but also the library in which that member resides.

If the order of LIB1 and LIB2 is reversed, LIB2 is searched first, and f1() is obtained from LIB2 instead.

If changing the library search order cannot work for you, use the LIBRARY control statement. See z/OS MVS Program Management: User's Guide and Reference for further information on the LIBRARY control statement.