SCons on Ubuntu for C++ and Java builds

Hi,

Here are my notes from playing around with SCons – I’m using it to build both C++ and Java projects, and thought I’d share my notes using a simple example of the setup and approach used for anyone else new to SCons.

This files for this project can be cloned from GitHub:
https://github.com/DonaldSimpson/java_and_c_with_scons

It’s very easy to install scons on Ubuntu via apt-get, just:

sudo apt-get install scons

You can then check your scons install & version info:

scons --version
SCons by Steven Knight et al.:
 script: v2.2.0.issue-2856:2676:d23b7a2f45e8[MODIFIED], 2012/08/05 15:38:28, by garyo on oberbrunner-dev
 engine: v2.2.0.issue-2856:2676:d23b7a2f45e8[MODIFIED], 2012/08/05 15:38:28, by garyo on oberbrunner-dev
 engine path: ['/usr/lib/scons/SCons']
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 The SCons Foundation

Creating a new project is trivial – it’s all relative to this directory:

mkdir newscons
cd newscons

and for the simplest example I could think of , let’s use Hello World in C++:

vim hello.c

#include <stdio.h>
int
main()
{
 printf("Hello, from C world!n");
}

that’s it – :wq from vim, then create a new SConstruct file and add in this one line:

Program('hello.c')

– that’s that done, :wq again.

so now we have just two files; the source for a tiny c program, and the one-line SConstruct file defining it

don@ubuntuserver:~/newscons$ ls -al
total 16
drwxrwxr-x 2 don don 4096 Oct 1 11:33 .
drwxr-xr-x 42 don don 4096 Oct 1 11:26 ..
-rw-rw-r-- 1 don don 51 Oct 1 11:31 hello.c
-rw-rw-r-- 1 don don 40 Oct 1 11:32 SConstruct

you can now kick off the scons build process by simply typing “scons”…

don@ubuntuserver:~/newscons$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello hello.o
scons: done building targets.

and then test the compiled C program…

don@ubuntuserver:~/newscons$ ./hello
Hello, from C world!

So, scons has just created the binary “hello”, a “hello.o” Object file and “.sconsign.dblite”, which is what scons uses to store file signatures so that if you now do “scons” again (without making any changes), you will see something like the following:

don@ubuntuserver:~/newscons$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `.' is up to date.
scons: done building targets.

as there have been no changes, but if you

don@ubuntuserver:~/newscons$ rm .sconsign.dblite

then try again…

don@ubuntuserver:~/newscons$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello hello.o
scons: done building targets.

that will make scons recompile regardless.

Now for the Java SCons example, let’s create a src/ dir and add in a simple Java example…

mkdir src

vim src/HelloWorld.java

class HelloWorld
{
 public static void main(String args[])
 {
 System.out.println("Hello, Java World!");
 }
}

Then edit the SConstruct to tell scons about the Java example – using “build” as the output destination for the built Java class file, and “src” as the source dir – I’m also leaving in the C++ example here too…

don@ubuntuserver:~/newscons$ cat SConstruct
Program('hello.c')
Java('build','src')
don@ubuntuserver:~/newscons$

run that, and you get:

don@ubuntuserver:~/newscons$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
javac -d build -sourcepath src src/HelloWorls.java
scons: done building targets.

so we now have the following files in our scons project:

don@ubuntuserver:~/newscons$ find .
.
./hello
./build
./build/HelloWorld.class
./hello.c
./hello.o
./.sconsign.dblite
./SConstruct
./src
./src/HelloWorls.java

and the new Java class can be tested like so:

don@ubuntuserver:~/newscons$ java -classpath build HelloWorld
Hello, Java World!

– that’s it for this simple example – there’s tons more that SCons can do, and here’s a good place to look: http://www.scons.org/

Cheers,

Don

Pin It on Pinterest

%d bloggers like this: