Gcc Development on Linux or MacIntosh OS/X, including for SAPC and Android

Of course you can do all your development for CS444 by remote login to our Solaris UNIX servers, notably ulab.cs.umb.edu, which has the connected SAPCs needed for running your programs.  However, you might want to develop on Linux or Mac and transfer the files to ulab for running. This file discusses that possibility.

Mac comes with gcc and its tools. Linux provides packages for add-ons like this, but different distributions of Linux have different package-handling software. For Ubuntu, see CompilingSoftware. For other distributions, do a Google search.  You need make as well as gcc, but I think they usually come together.

In a shell window, test your setup:

$ gcc test1.c      (simple hello program)

$ a.out

hello world!


To run this program on SAPC, transfer it to ulab.cs.umb.edu, copy the makefile from $pcex, and “make C=test1”, then use mtip to execute it.


For C/C++ eclipse, download the C/C++ Eclipse IDE.  If you already have the Java or JEE eclipse IDE, you may want to specify a different workspace location when you first run the IDE, although you can access Java projects from the C/C++ Eclipse IDE.

For cs444, we will be using makefiles to build programs. To set up a new C project, do New>C Project, give it a name and location, and choose project type Makefile>"Empty Project.  The makefile that will be running the project lives in the base directory of the project.  So the simplest arrangement is put all the .c files in the base directory as well. (Note that this is different from what we usually do for Java projects, where we have a "src" directory.) If you do use a src directory, just remember that the makefile that matters is in the base directory.

 

What makefile to use?  Note that $pcex has makefile.offsite, the offsite-version makefile. To set up to compile the examples directory on Linux/Mac:

1.      Create directories include and examples within a pcdev directory on your system

2.      Transfer $pcinc to the local include directory:  when cd’d to that directory:

scp user@linux1.cs.umb.edu:/groups/ulab/pcdev/include/* .

3.      Transfer $pcex to the local examples directory: when cd’d to that directory:

4.      scp user@linux1.cs.umb.edu:/groups/ulab/pcdev/examples/* .

5.      Make the offsite makefile the default makefile:

cp makefile.win makefile

6.      make clean   This should run the make tool.

7.      make C=timer   This should compile timer.c and create timer.opc. We can’t build timer.lnx off-site

8.      make test.exe    This should compile and load test.c creating test.exe, a Linux/Mac executable. Note that test.c is a portable program, i.e., it can run both on a real OS and also on SAPC.

Android Native C Programs

You can write C programs and run them on Android Linux with help from gcc and its tools.  Android development uses cross-compilation to the CPU of the Android device, typically an AMD processor system. gcc is creating x86 machine code, not AMD, but it is still useful for compilation of tools that themselves produce AMD machine code.

Download the Android SDK and NDK (native development kit) from the Android website and install them (i.e., unzip the NDK to a convenient place like /android-ndk.) This gives you the needed cross-compilation tools, or “toolchain”, for producing AMD machine code, once they are compiled from .c files.  As long as the C tools are available, and the NDK tools are on the PATH (see below), the Android build command ndk-build will work fine.

Most articles and documentation on Android only cover the case of calling C from Java programs, but it’s perfectly possible to run a normal C program on the underlying Linux system of Android.  The NDK has a sample “hello world” program under tests\device\static-executable in the NDK area. The program itself and the needed .mk files are in the jni subdirectory. To compile this, use “ndk-build” in the upper directory. The resulting Android executable is in static-executable\libs\armabi\static_main.  You can copy this to your Android device or AVD (Android virtual device, via the emulator) using adb (assuming the SDK tools are also on your PATH):

$ adb push static_main /data/local    (a writable location on the Android Linux filesystem)

$ adb shell                           (start a shell on the Android device)

# cd /data/local

# ./static_main

Hello static world!

#

You can write C programs and run them on Android Linux with help from MinGW.  Android development uses cross-compilation to the CPU of the Android device, typically an AMD processor system. MinGW is creating x86 machine code, not AMD, but it is still useful for compilation of tools that themselves produce AMD machine code.

Download the NDK (native development kit) from the Android website and install it (i.e., unzip it to a convenient place like /android-ndk.) This gives you the needed cross-compilation tools, or “toolchain”, for producing AMD machine code, once they are compiled from .c files. 

Most articles and documentation on Android only cover the case of calling C from Java programs, but it’s perfectly possible to run a normal C program on the underlying Linux system of Android.  The NDK has a sample “hello world” program under tests\device\static-executable in the NDK area. The program itself and the needed .mk files are in the jni subdirectory. To compile this, use “ndk-build” in the upper directory. Of course you need the NDK build tools on your PATH for this (see below). The resulting Android executable is in static-executable\libs\armabi\static_main.  You can copy this to your Android device or AVD (Android virtual device, via the emulator) using adb:

C:\...\libs\armabi\> adb push static_main /data/local    (a writable location on the Android Linux filesystem)

C:\...\libs\armabi\> adb shell                           (start a shell on the Android device)

# cd /data/local

# chmod 777 static_main                                  (make it executable)

# ./static_main

Hello static world!

#

 

Changing your PATH setup

 

      You need to make the Android SDK and NDK tools appear on your PATH, so “adb” and “ndk-build” work as commands. The ndk-build script is at the top level of the NDK distribution, while adb is in subdirectory platform-tools of the SDK area.

            Linux: It should work to add the environment variables to your .profile or .bashrc (try both ways), for example,

        export PATH=$PATH:/android-sdk/platform-tools:/android-ndk

 

      Mac: Unfortunately, although the Mac uses the same shell as Linux, it doesn’t always execute .bashrc or .profile on login. You can use the above export command interactively in a Terminal shell to set things up for a session. There are several ways to set up environment variables persistently but the following seems easiest (requires OSX v 10.5 or later):

First find the old PATH:

        $ echo $PATH           (look at old PATH, in Terminal)

        /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Then add the following to /etc/launchd.conf:  add /android-ndk (the top-level directory of your NDK) to the old PATH.

        setenv PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/android-sdk/platform-tools:/android-ndk  

       

      Probably there is no launchd.conf file in /etc yet, so you can simply create a new one by for example:

               $ cd /etc

               $ sudo cat > launchd.conf

        setenv PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/android-sdk/platform-tools:/android

        Control-D

 

               Then reboot and check your PATH by echo $PATH, and make sure adb and ndk-build work.