PrevUpHomeNext

Create and Manage c++ Project with b2 build


Jump in this page:
Start
c++ executable project
c++ library project

Back: c++ Build System

B2 build is a perfect build system to create, manage, upgrade c++ project.

Read first: Build and Install B2 Build .

Create, manage c++ executable project

Start: create project directory

mkdir my-prj
cd my-prj

Write Jamfile

File: jamroot

exe hello : hello.cpp : <define>cpp_hot : <cxxstd>26 ;

exe is a rule to define an executable target: hello

In b2 jamfiles, many rules have five parameters, splitted by a colon keyword : , (A colon is a keyword in b2 jamfiles.)

Note that both the left side and right side of : and ; must be blank characters.

Write c++

File: hello.cpp

#include <iostream>

int main()
{
	std::cout << "Hello, c++!" << std::endl;
}

Build project

Just run command b2:

> b2

Run program:

The compiled program is generated at folder bin, you have to find it.

> ./bin/gcc-16/debug/cxxstd-26-iso/hello
Hello, c++!

Create, manage c++ library project

Init project folders:

mkdir myprj
cd myprj
mkdir include src

File: jamroot

lib foo : src/foo.cpp : <cxxstd>26 <include>./include : : <include>./include ;

exe prog : prog.cpp : <library>foo ;

rule lib defines a target foo.

Because foo is a lib target, the b2 build will generate libfoo.so by default on linux, or related shared library on windows.

<cxxstd>26 in the requirements list: it can not be overrided on commandline (in the 3rd paramter);

<include>./include in the requirements list of third parameter: building current library requires using ./inlcude as include path,
which is translated to -I./include
;

The 4th parameter is empty;

<include>./include in the list of 5th parameter: if another target depends on this target, <include>./include will be applied to another target;

File: include/foo.hpp

#pragma once

namespace dir
{
	class foo
	{
	public:
		void print() const;
	};
};

File: src/foo.cpp

#include <foo.hpp>
#include <iostream>

void dir::foo::print() const
{
	std::cout << "Hello, c++ world!" << std::endl;
}

File: prog.cpp

#include <foo.hpp>

int main()
{
	dir::foo object;
	object.print();
}

Build project:

Just run command b2

b2

Run program:

The program prog is generated in bin folder, you have to find where it is.

> ./bin/gcc-16/debug/prog
Hello, c++ world!
>

See Also

https://www.bfgroup.xyz/b2/manual/release/index.html

Date

Date: Thu Jun 19 02:12:53 AM UTC 2025

Last Update: Sun Sep 6 04:20:33 AM UTC 2026

Back

Back: c++ Build System

Helpful

Spaceship 50 Years Alienated

Role

+

Powered by:
B2 Build | boost quickbook

+

Donate

+

@cppfx.xyz


















PrevUpHomeNext