2

Directory Structure:

WorkSpace/root_dir
|_ SConstruct
|_ multipy
  |_ include
    |_ mul.h
  |_ src
    |_ mul.cpp
    |_ SConscript
|_ sum
  |_ include
    |_ sum.h
  |_ src
    |_ sum.cpp
    |_ SConscript
|_ src_main
  |_ main.cpp
  |_ SConscript

Contents of root_dir/SConstruct:

# This Sconstruct file should build:
# Static and Shared libraries of sum and mul for both release(build/release) and debug mode(build/debug) and place it into the directory as mentioned in parenthesis
# Finally build executable for main.cpp by linking it with sum and mul libraries
# and place it into src_main/build/release and src_main/build/debug directories
import os

env = Environment()
print 'env = ', env

debug_env   = env.Clone(CCFLAGS = '-g')
release_env = env.Clone(CCFLAGS = '-O2')

SConscript('sum/src/SConscript', variant_dir='sum/build/debug/', exports ={'env' : debug_env}, duplicate=0)
SConscript('sum/src/SConscript', variant_dir='sum/build/release/', exports = {'env' : release_env}, duplicate=0)
SConscript('multiply/src/SConscript', variant_dir='multiply/build/debug/', exports ={'env' : debug_env}, duplicate=0)
SConscript('multiply/src/SConscript', variant_dir='multiply/build/release/', exports = {'env' : release_env}, duplicate=0)

Contents of sum/src/SConscript file :

# The main purpose of this SConscript file is to create static and shared library version
# for release and debug mode
# It should also creates objects and place it into build/release and build/debug 
# directories inside multiply directory
import os
Import('env')

libs = ['libsum.a', 'libsum.so']

# list of source files
src_list = Glob(os.path.join(Dir('#').abspath, 'sum', 'src', '*.cpp'))

# Path of included files ( - not sure whether it's the correct way to do in scons ?)
env['CPPPATH'] = ['#/sum/include']

env.StaticLibrary(target = 'sum', source = src_list)
env.SharedLibrary(target = 'sum', source = src_list)

Return('libs')

Similar kind of SConscript file is present in the multiply directory for the same purpose as mentioned in comment of sum/src/SConscript file.

For src_main/SConscript:

import os
import ('env')

Program('main.cpp', LIBS=['sum', 'mul'], LIBPATHS=[])  # How should I mention LIBPATHS here ?

Running %scons -Q

g++ -o multiply/src/mul.o -c -g -Imultiply/include multiply/src/mul.cpp
ar rc multiply/build/debug/libmul.a multiply/src/mul.o
ranlib multiply/build/debug/libmul.a
g++ -o multiply/src/mul.os -c -g -fPIC -Imultiply/include multiply/src/mul.cpp
g++ -o multiply/build/debug/libmul.so -shared multiply/src/mul.os
ar rc multiply/build/release/libmul.a multiply/src/mul.o
ranlib multiply/build/release/libmul.a
g++ -o multiply/build/release/libmul.so -shared multiply/src/mul.os
g++ -o sum/src/sum.o -c -g -Imultiply/include sum/src/sum.cpp
sum/src/sum.cpp:1:17: fatal error: sum.h: No such file or directory
compilation terminated.
scons: *** [sum/src/sum.o] Error 1

I can't understand why -Imultiply/include is used while creating sum.o, Although for sum SCOnscript file I have used env['CPPPATH'] = ['#/sum/include'] -> it is taken as the similar kind of statement I used in multiply/src/SConscript file (i.e. env['CPPPATH'] = ['#/multiply/include'])

I am new user of scons build tool and I wasn't able to do this kind of simple example which is a very general practical scenario.

Please suggest me necessary details to fix my error and add optimization in my approach to fulfill the requirement/purpose.

  • Another option for getting help about a program, is the project's website. In this case www.scons.org offers links to a Wiki, UserGuide and MAN page, which all try to give examples about most of the questions you asked above. Finally, if everything else fails, SCons has a very active user mailing list where you can reach a lot of people that once were beginners, just like you now. – dirkbaechle Jun 15 '14 at 08:17

2 Answers2

0

You need to give the path to your SConscript files

SConscript('multiply/src/SConscript ', variant_dir='build/debug/', export={'env':debug_env}, duplicate=0)
SConscript('multiply/src/SConscript ', variant_dir='build/release/', export={'env':release_env}, duplicate=0)

and you'll need to call SConscript() on any other SConscript files you want to include too.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
nos
  • 223,662
  • 58
  • 417
  • 506
  • Thanks for suggestion, After updating as per your comment it now calls the SConscript, I am looking for some elegant approach to do the same example, As currently it looks more like a naive approach. – Harry Cruise Jun 15 '14 at 12:40
0

-Imultiply/include is used while creating sum.o because you have the following in an included SConscript

env['CPPPATH'] = ['#/sum/include']
bdbaddog
  • 3,357
  • 2
  • 22
  • 33