PrevUpHomeNext

Create and Manage c++ Project with b2 build


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

Read first: Build and Install B2 Build

Anchor List:

Create an executable c++ 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 build, almost every rule has five paramters, splitted by keyword :

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 pgram:

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: can not be overrided on commandline (in the 3rd paramter);

<include>./include in the requirements list: build current library requires including ./inlcude (in the 3ird parameter);

The 4th parameter is empty;

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

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 already 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

Thu Jun 19 02:12:53 AM UTC 2025

Back

Up

Deck

c++ Toolchain

@cppfx.xyz


PrevUpHomeNext