Compiling

The following source code examples show C and C++ source code files and the typical invocation commands used to compile them.

Example 1: Main program (C): validateArguments.c
#include <stdlib.h>

int lookupTable[11];

int getRC(int factor1, int factor2);

int main(int argc, char* argv[]) {
  // Validate the argument count
  if (argc < 3) {
    return 1;
  }
  if (argc > 3) {
    return 2;
  }

  // Initialize the global data
  for (int i = 0; i < 11; i++) {
    lookupTable[i] = i;
  }

  // Convert the arguments to numbers
  // Note: argv[0] is the executable name
  int factor1 = atoi(argv[1]);
  int factor2 = atoi(argv[2]); 
  return getRC(factor1, factor2); 
}
The source can be compiled as follows:
ibm-clang -m64 -c -Wall validateArguments.c                          
The ibm-clang invocation command compiles the C source code. The -m64 option enables the 64-bit compilation mode. The -Wall argument (option) to the compiler instructs it to warn of possible code issues.
Example 2: Linked function (C++): intMap.C
extern int lookupTable[11];

extern "C" {
  int getRC(int index1, int index2);
}

int getRC(int index1, int index2) {
  for (int i : lookupTable) {
    if (i == index1) {
      return lookupTable[i] * lookupTable[index2];
    }
  }
  return -1;
}
The source can be compiled with the ibm-clang++ invocation command as follows:
ibm-clang++ -m64 -c -Wall intMap.C                                     
Use the ibm-clang++ invocation command when compiling C++ code.This compiles the C++ code with the correct option defaults for C++ and links to the C++ runtime libraries.
You can then link using ibm-clang++ as follows:
ibm-clang++ -m64 validateArguments.o intMap.o -o myMultiply
The ibm-clang++ invocation command binds or links the two object files, validateArguments.o (C based) and intMap.o (C++ based), into a single executable file myMultiply, which can be invoked as follows:
myMultiply 9 5

You can use the command echo $? to view the return code of myMultiply, which is 45.

Related information