Search code examples
c++buildmakefilesconssubdirectory

Writing scons script to handle compilation of files in many subdirectories


I've got a project which consists of many files in subdirectories. I have a simple Makefile which handles the compilation. It looks like this:

CC = g++ -Wall -ansi -pedantic 
all:
$(CC) -O2 engine/core/*.cpp engine/objects3d/*.cpp engine/display/*.cpp engine/io  /*.cpp engine/math/*.cpp engine/messages/*.cpp  *.cpp -o project1 -lGL -lGLU -lX11 `sdl-config --cflags --libs`

clean:
@echo Cleaning up...
@rm project1
@echo Done.

However I need to migrate to SCons. I have no idea how to write a script that would automatically handle finding all the *.cpp files in subdirectories and included them in the compilation process.


Solution

  • This is how to do what you have in your Makefile in SCons. You should put this Python code in a file at the root of the project called SConstruct, and just simply execute scons. To clean, execute scons -c

    env = Environment()
    env.Append(CPPFLAGS=['-Wall', '-ansi', '-pedantic', '-O2', '-lGL', '-lGLU', '-lX11'])
    
    # Determine compiler and linker flags for SDL
    env.ParseConfig('sdl-config --cflags')
    env.ParseConfig('sdl-config --libs')
    
    # Remember that the SCons Glob() function is not recursive
    env.Program(target='project1',
                source=[Glob('engine/core/*.cpp'),
                        Glob('engine/objects3d/*.cpp'),
                        Glob('engine/display/*.cpp)',
                        Glob('engine/io/*.cpp'),
                        Glob('engine/math/*.cpp'),
                        Glob('engine/messages/*.cpp'),
                        Glob('*.cpp')])
    

    Here is a link for using SDL with SCons.

    And here is info on the SCons ParseConfig() function.