Skip to content
Ryan Gonzalez edited this page Apr 2, 2015 · 7 revisions

Requirements.

Download and Install Fbuild

$ git clone https://github.com/felix-lang/fbuild.git
$ cd fbuild
$ python3 setup.py install # prefix this command with `sudo` if it fails because of permissions

Using Fbuild to compile a C file

Here's a simple example of using Fbuild. Say you have a simple c file, named test.c:

#include <stdio.h>

int main(int argc, char** argv) {
    printf("%s\n", argv[0]);
    return 0;
}

To compile this code with Fbuild, create a Python source file named fbuildroot.py:

import fbuild.builders.c

def build(ctx):
    # Create a c builder. Fbuild can automatically determine
    # the default system's compiler. On Windows, it will default
    # to Visual Studio C++. On Unix machines, it uses Gcc.
    builder = fbuild.builders.c.guess_static(ctx)

    # Compile a test file.
    test = builder.build_exe('test', ['test.c'])

    # Run the test file.
    ctx.execute([test])

Then to compile the code, simply run:

$ fbuild
determining platform     : {'bsd', 'darwin', 'macosx', 'posix'}
looking for program gcc   : ok /usr/bin/gcc
checking gcc              : ok
looking for program ar    : ok /usr/bin/ar
looking for program ranlib : ok /usr/bin/ranlib
checking if gcc can make objects : ok
checking if gcc can make libraries : ok
checking if gcc can make exes      : ok
checking if gcc can link lib to exe : ok
 * gcc                              : test.c -> build/test.o
 * gcc                              : build/test.o -> build/test
build/test

Fbuild will cache the results, so if you run it again it won't recompile the code:

$ fbuild
build/test

Compiling a C library

Fbuild also supports cross platform library building. Here's how to use this. It follows a similar pattern. Here are the files:

lib.c:

double square(double x) {
    return x * x;
}

exe.c:

#include <stdio.h>
#include "lib.h"

double square(double x);

int main(int argc, char** argv) {
    printf("%f\n", square(5));
    return 0;
}

fbuildroot.py:

import fbuild.builders.c

def build(ctx):
    # Create a builder that can build shared libraries.
    builder = fbuild.builders.c.guess_shared(ctx)

    # Create a shared library. On Windows it creates a .dll, on Linux it
    # creates a .so, and on OS X it creates a .dylib.
    lib = builder.build_lib('lib', ['lib.c'])

    # Compile an executable and link in the library.
    exe = builder.build_exe('exe', ['exe.c'], libs=[lib])

    # Run the executable.
    ctx.execute([exe])

This is what happens when you run fbuild:

$ fbuild
determining platform     : {'bsd', 'darwin', 'macosx', 'posix'}
looking for program gcc   : ok /usr/bin/gcc
checking gcc              : ok
checking gcc with -fPIC   : ok
checking gcc with -dynamiclib : ok
checking if gcc -fPIC can make objects : ok
checking if gcc -fPIC can make libraries: ok
checking if gcc -fPIC can make exes     : ok
checking if gcc -fPIC can link lib to exe: ok
 * gcc -fPIC                            : lib.c -> build/lib.os
 * gcc -dynamiclib                      : build/lib.os -> build/liblib.dylib
 * gcc -fPIC                            : exe.c -> build/exe.os
 * gcc                                  : build/exe.os lib -> build/exe
25.000000