Posts tagged build

Minimal C project structure with SCons

:: C, scons, build

This is a possible project structure in order to have a C project using SCons as the build system. It enables you to:

  • compile most files as a library, and link that to a main file with application code.
  • separate src and include directories
  • separate unit tests using libcheck
  • the unit tests link to the library
  • Macports-installed libraries

Layout your code like this:

.
├── include
│   └── core
│       └── mylib.h
├── sconstruct
├── src
│   ├── core
│   │   └── mylib.c
│   └── main.c
└── tests
    ├── core
    │   ├── test_mylib.c
    │   ├── test_mylib.h
    │   └── tests.h
    └── main.c

where mylib should have a more descriptive name for your project.

sconstruct is:

env = Environment(CPPPATH=['include', '/opt/local/include'])
env.Library(target='mylib', source=Glob('src/core/*.c'))
env.Program(target='mylib', source=Glob('src/*.c'),
            LIBS=['mylib'], LIBPATH=['.'])
env.Program(target='test_mylib', source=Glob('tests/*.c') + Glob('tests/**/*.c'),
            LIBS=['mylib', 'check'], LIBPATH=['.', '/opt/local/lib/'])