Use the GNU C Compiler in PASE

Article ID: 54311

Q: I've read several articles that discuss installing AIX programs to run in PASE. I think this is great, because there's a lot of open-source software that I can use on my System i! But, what's the good of open-source software if I have to find pre-compiled versions of it? Is there some way to compile AIX C source within PASE?

A: Most of the open-source C software that I've encountered uses the GNU C compiler, since that compiler is included with most distributions of Linux. You can download that compiler and run it in PASE to compile various open-source utilities. You can also use it to compile your own code if you have some C programming expertise.

This article assumes that you already have PASE installed on your system. If not, you need to install it from the i5/OS CDs. It's included with the operating system in V5R1 and is free of charge in V5R2 and later. You can install it with GO LICPGM, option 11. It's licensed program 5722-SS1, option 33, "OS/400 - Portable App Solutions Environment"

The iSeries Tools for Developers (5799-PTL) PRPQ

Most of the open-source tools out there need the GNU make command. IBM provides that command, along with a plethora of other useful tools, in the iSeries Tools for Developers (5799-PTL) PRPQ. If you don't already have it, you can download it at the following link:
http://www-03.ibm.com/servers/enable/site/porting/tools/index.html

Installation instructions are available, as well. Click the "Installation/Setup" link on the right-hand side of the page.

Installation of the GNU C Compiler

Follow these instructions to install the GNU C compiler in PASE on i5/OS:

  1. Download the "binary" (pre-compiled) version of gcc (GNU C compiler) from UCLA's excellent AIX public domain software library:
    http://aixpdslib.seas.ucla.edu/packages/gcc.html

    Note: The versions of AIX listed on that site might or might not be supported in PASE, depending on your version of i5/OS. For more information, please see the table on the following page:
    http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/rzalf/rzalfplanning.htm

  2. The name of the file you download should have a name like gcc.4.1.1.tar.Z. The numbers in the middle of the file name indicate the version of the compiler you downloaded. In my example, it's version 4.1.1, but yours might be a different version.

  3. I downloaded the file to my PC, then uploaded it with FTP to the IFS on my System i. If you do the same, make sure you use "binary" mode for the FTP transfer. Copying it from your PC to the System i using a mapped drive should also work, if that's more convenient.

  4. Start an interactive PASE session by typing the following command:

    CALL QP2TERM
  5. Using the cd command, switch to the directory where you put the gcc file that you just downloaded. For example, if you downloaded the file into the /tmp/downloads directory, type the following command:

    cd /tmp/downloads
  6. When a file ends in .Z, it means it was compressed with the Unix compress utility. To install the file, you need to uncompress it with the uncompress utility. To do that, type the following command:

    uncompress gcc.4.1.1.tar.Z

    Naturally, you'll need to change the "4.1.1" in the preceding example to the version number that you downloaded. The result will be a larger file that ends in ".tar" (because the .Z was removed when it was uncompressed.)

  7. Files that end in .tar are Unix "tape archiver" files. The PASE environment comes with a utility named tar for working with those archives.

    You should extract the contents of the archive into the /QOpenSys directory. To do that, first switch to the right directory, then use the tar utility to extract the archive. For example:

    cd /QOpenSys
    tar xvf /tmp/downloads/gcc.4.1.1.tar.Z
  8. The gcc program should now be installed in the /QOpenSys/usr/local/bin directory of your IFS. There should also be a lot of objects added to the /QOpenSys/usr/local/lib/gcc directory of your IFS.

    If this were an actual Unix system, the compiler would expect to be in the /usr/local area of the disk instead of the /QOpenSys/usr/local area. Consequently, I suggest creating a symbolic link between the two to prevent the compiler from becoming confused.

    If you already have a /usr/local directory in your IFS, type the following command to move its contents to the /QOpenSys area.

    cd /usr
    cp -rp local /QOpenSys/usr
    rm -r /usr/local

    Then, create the symbolic link so that /usr/local and /QOpenSys/usr/local point to the same place:

    ln -s /QOpenSys/usr/local /usr/local

    At this point, the gcc program should be visible as /usr/local/bin/gcc. To verify that, type the following command:

    ls -l /usr/local/bin/gcc
  9. Now the program has been installed. As the next step, I recommend adding /usr/local/bin to your PATH environment variable. You can do that by typing the following command:

    export PATH=$PATH:/usr/local/bin

    The preceding command sets up the PATH in the current PASE session. If you'd like that command to be run for you automatically each time you start PASE, you can put it into a special file named ".profile" that's run automatically each time you start PASE. To do that, type the following commands:

    mkdir -p /home/your-userid/.profile
    echo export PATH=\$PATH:/usr/local/bin >> /home/your-userid/.profile

    The preceding code should create a file named ".profile" in your home directory (if it didn't already exist), and add a line to the end of that file containing the command to add /usr/local/bin to your PATH.

  10. At this point, the GNU C Compiler should be installed and working. You can verify that it's installed by running the command with the -v switch. If all is well, it should print its version number, as well as some other information, on the screen.

    gcc -v

A More Significant Test

To provide a better test that all is working well, I suggest creating a simple "Hello World" program. To do that, create a subdirectory for the test, and create an empty file to contain the source code. I did this by typing the following commands into the PASE shell:

mkdir -p $HOME/gcctest
cd $HOME/gcctest
touch gcctest.c

A few notes on the preceding commands:

  • The commands are case-sensitive. Please type them with the exact same upper and lower case that you see above.
  • Where it says $HOME, that's an environment variable. PASE will insert the correct home directory for your user-id.
  • The touch command creates an empty file. I'm creating an empty file from within PASE to make sure it's an ASCII file, the way PASE likes it.
  • If I created the file from i5/OS or QShell, it might end up as an EBCDIC file, which would cause problems.

Now that you have the empty file, hit F21 to get traditional i5/OS command prompt, and type the following command:

edtf stmf('/home/your-userid/gcctest/gcctest.c')

This invokes the EDTF tool to edit the source code. EDTF is an awkward tool, and WDSc would do a better job. But because what I'm typing today is very simple, EDTF is adequate.

In EDTF, type the following

  1. On the blank on the left-hand side, type I5 to insert 5 more lines to type code into.

  2. Type the code for a simple Hello World program in the space under "Beginning of Data." The result should look like the following screen:

     Edit File: /home/klemscot/gcctest/gcctest.c                                    
     Record :       1   of       6 by   8            Column :    1     59 by  74    
     Control :                                                                      
    CMD ....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+ 
         ************Beginning of data**************                                
        #include                                                           
        int main(void) {                                                            
           printf("Hello World\n");                                                 
           return 0;                                                                
        }                                                                           
         ************End of Data********************                                
     F2=Save   F3=Save/Exit   F12=Exit   F15=Services   F16=Repeat find             
     F17=Repeat change  F19=Left  F20=Right
  3. Press F2 to save your changes, F3 to exit the EDTF command and F12 to return to the PASE command line.

  4. Compile the program by typing the following commands:

    cd $HOME/gcctest
    gcc -o gcctest gcctest.c

    If there are any errors, they will be printed on the screen in PASE. If there are problems, review the preceding steps to try to fix them. If there are no messages printed on the screen, then the code compiled.

  5. To run the program, type the following into PASE:

    ./gcctest

    It should print "Hello World". Congratulations, you've just compiled and run an AIX program on your System i!

If you get stuck and need help, the best place to find it is in the C/C++ and Java forum at SystemiNetwork.com. Here's a link to that resource:
http://www.systeminetwork.com/isnetforums/forumdisplay.php?f=12

ProVIP Sponsors

ProVIP Sponsors