Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage growing C++ project

I am wondering how I should manage a growing C++ project. Now, I am developing a project with Netbeans and it's dirty work generating makefiles. The project has become too big and I have decided to split it up into a few parts. What is the best way of doing this?

I am trying to use Scons as my build system. I have had some success with it, but should I edit the build scripts every time I append or delete files. It's too dull.

So I need your advice.

P.S. By the way, how does a large project like google chrome do this? Does everybody use some kind of IDE to build scripts generated only for software distribution?

like image 466
Alex Povar Avatar asked May 14 '12 02:05

Alex Povar


3 Answers

I also use Netbeans for C++ and compile with SCons. I use the jVi Netbeans plugin which really works well.

For some reason the Netbeans Python plugin is no longer official, which I dont understand at all. You can still get it though, and it really makes editing the SCons build scripts a nice experience. Even though Netbeans doesnt have a SCons plugin (yet?) you can still configure its build command to execute SCons.

As for maintaining the SCons scripts automatically by the IDE, I dont do that either, I do that by hand. But its not like I have to deal with this on a daily basis, so I dont see that its that important, especially considering how easy to read the scripts are.

Here's the build script in SCons that does the same as mentioned previously for CMake:

env = Environment()
env.EnsurePythonVersion(2, 5)
env.EnsureSConsVersion(2, 1)

libTarget = env.SharedLibrary(target = 'foo', source = ['a.cpp', 'b.cpp', 'c.pp'])
env.Program(target = 'bar', source = ['bar.cpp', libTarget])

The SCons Glob() function is a nice option, but I tend to shy away from automatically building all the files in a directory. The same goes for listing sub-directories to be built. Ive been burned enough times by this, and prefer explicitly specifying the file/dirs to be built.

In case you hear those rumors that SCons is slower than other alternatives, the SCons GoFastButton has some pointers that can help out.

like image 58
Brady Avatar answered Sep 20 '22 16:09

Brady


Most large projects stick with a build system that automatically handles all the messy details for them. I'm a huge fan of CMake (which is what KDE uses for all their components) but scons is another popular choice. My editor (KDevelop) supposedly handles CMake projects itself, but I still edit the build scripts myself because it's not that hard.

I'd recommend learning one tool really well and sticking with it (plenty of documentation is available for any tool you'll be interested in). Be sure you also look into version control if you haven't already (I have a soft spot for git, but Mercurial and Subversion are also very popular choices).


A simple CMake example:

project("My Awesome Project" CXX)
cmake_minimum_required(VERSION 2.8)
add_library(foo SHARED a.cpp b.cpp c.cpp) #we'll build an so file
add_executable(bar bar.cpp)
target_link_libraries(bar foo) #link bar to foo

This is obviously a trivial case, but it's very easy to manage and expand as needed.

like image 27
Stephen Newell Avatar answered Sep 18 '22 16:09

Stephen Newell


I am trying to use Scons as build system. I have some success with it, but I should edit build scripts every time I append or delete file. It's too dull.

Depending on how your files are organized, you can use, for example, Scon's Glob() function to get source files as a list without having to list all files individually. For example, to build all c++ source files into an executable, you can do:

 Program('program', Glob('*.cpp'))

You can do the same in CMake using its commands.

And, if you're using SCons, since it's Python you can write arbitrary Python code to make your source file lists.

You can also organize files into multiple folders and have subsidiary SCons (or CMakeList.txt) build files that the master build script can call.

like image 45
plasma Avatar answered Sep 18 '22 16:09

plasma