Cxx
:electric_plug: Configuration-free utility for building, testing and packaging executables written in C++. Can auto-detect compilation flags based on includes, via the package system and pkg-config.
Install / Use
/learn @xyproto/CxxREADME
CXX
Make modern C++ easier to deal with.
NOTE: This project now continues here, as a Go project, without depending on SCons: xyproto/orchideous
Have you ever had a single main.cpp file that you just want to compile, without having to make sure the order of flags are correct and ideally without having to provide any flags at all?
cxx may fit your use case, provided you have scons, python and all required libraries for your project installed.
It should be possible to compile most of the examples in the examples directory, simply by running cxx in each directory.
Using cxx is simple:
cxxbuilds a projectcxx fmtformats the source codecxx debugperforms a debug buildcxx cmakegenerates a (platform specific)CMakeLists.txtfile that is compatible with many IDEs.cxx progenerates a project file that is compatible with QtCreator.cxx cmake ninjagenerates aCMakeLists.txtfile and then builds the project usingninja(andccache, if available).cxx ninjajust builds the project using aCMakeLists.txtfile andninja(andccache, if available).cxx exportgenerates build files for users withoutcxx.
No configuration files are needed, but the projects needs to either be very simple (a single main.cpp) or have a cxx-friendly directory structure.
The auto-detection of external libraries and headers relies on them being included in the main source file.
Tested on Arch Linux, FreeBSD, Ubuntu, macOS w/Homebrew, Void Linux and NetBSD. Docker images and Vagrant configuration files are available in the tests directory. Please submit a pull request if you have improvements for your platform!
Several examples are included in the examples directory. These mostly center around everything you would need to create a game in C++20: OpenGL, SDL2, Vulkan, Audio etc, but also includes examples for GTK 4, Qt 6, X11 and Windows (the example should build and run on Linux, using wine).
The target audience is programmers that don't want to fiddle with makefiles, CMake etc, but want to either try out a feature in C++20, learn modern C++ or create a demoscene demo or a game.
As much as possible is auto-detected. As long as the right packages are installed, and includes are specified in the main source file, all dependencies, libraries and build flags should be handled automatically.
cxx provides a way to structure your C++ code, test and debug your source files. It also makes it easy for Linux (or Homebrew) packagers to package your project, and for users to build and install it.
If you're an experienced C or C++ user and wish to write and distribute a C++ library (as opposed to an executable), just using CMake might be a better fit.
(This repository was created three years before dtolnay/cxx).
Packaging status
Installation
If cxx is available by using your favorite package manager, that's usually the best way.
Manual installation
First install cxx, so that it is in the path. Here is one way, using git clone, GNU Make and sudo:
git clone https://github.com/xyproto/cxx
cd cxx
make
sudo make install
Debian-based distros
For Debian or Ubuntu, these dependencies are recommended, for building CXX and most of the examples:
build-essential figlet freeglut3-dev g++-mingw-w64-x86-64 git gtk+3-dev libboost-all-dev libc-dev libglew-dev libglibmm-2.4-dev libsdl2-dev libsfml-dev make mesa-common-dev qtbase5-dev qt5-default qtdeclarative5-dev scons python3 apt-utils apt-file libconfig++-dev libconfig++ libopenal-dev libglfw3-dev libvulkan-dev libglm-dev libsdl2-mixer-dev libboost-system-dev libfcgi-dev
FreeBSD
For FreeBSD, here is one way of installing only the basic dependencies and CXX:
pkg install -y bash git gmake pkgconf python3 scons
git clone https://github.com/xyproto/cxx
cd cxx
gmake
Then as root:
gmake install
NetBSD
One way of installing CXX and also the libraries needed by most of the example projects:
pkgin -y install bash git gmake pkgconf python37 SDL2 SDL2_image SDL2_mixer SDL2_net SDL2_ttf docker freeglut gcc7 glew glm glut openal qt5 scons boost fcgi
test -d cxx && (cd cxx; git -c http.sslVerify=false pull origin main) || git -c http.sslVerify=false clone 'https://github.com/xyproto/cxx'
gmake -C cxx install
Void Linux
Installing CXX and the libraries needed by most of the example projects:
xbps-install -v -Sy SDL2-devel SDL2_mixer-devel SFML-devel boost-devel figlet gcc git glew-devel gtk+3-devel libconfig++-devel libfreeglut-devel libopenal-devel make pkg-config python3 qt5-devel scons fcgi
git clone https://github.com/xyproto/cxx && cd cxx && make install
Arch Linux
Just install cxx from AUR.
Example Use
Try out CXX and a small program that uses features from C++20
Create a main.cpp file:
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <string>
using namespace std::string_literals;
class Point {
public:
double x;
double y;
double z;
};
std::ostream& operator<<(std::ostream& output, const Point& p)
{
using std::setfill;
using std::setw;
output << "{ "s << setfill(' ') << setw(3) << p.x << ", "s << setfill(' ') << setw(3) << p.y
<< ", "s << setfill(' ') << setw(3) << p.z << " }"s;
return output;
}
Point operator+(const Point& a, const Point& b)
{
return Point { .x = a.x + b.x, .y = a.y + b.y, .z = a.z + b.z };
}
Point operator*(const Point& a, const Point& b)
{
return Point { .x = a.x * b.x, .y = a.y * b.y, .z = a.z * b.z };
}
int main(int argc, char** argv)
{
// designated initializers
Point p1 { .x = 1, .y = 2, .z = 3 };
Point p2 { .y = 42 };
using std::cout;
using std::endl;
cout << " p1 = " << p1 << endl;
cout << " p2 = " << p2 << endl;
cout << "p1 + p2 = " << p1 + p2 << endl;
cout << "p1 * p2 = " << p1 * p2 << endl;
return EXIT_SUCCESS;
}
Then build the project with just:
cxx
Rebuilding can be done with:
cxx rebuild
Both building and running can be done with:
cxx run
If you wish to optimize the program, running it in a way that also records profiling information can be done with:
cxx rec
The next time the project is built, the profiling information is used to optimize the program further:
cxx
Other commands
Building files ending with _test.cpp, then running them
cxx test
Cleaning
cxx clean
Building with clang++ instead of g++:
cxx clang
Building a specific directory
cxx -C examples/hello
Installing on the local system, using sudo:
sudo PREFIX=/usr cxx install
Either main.cpp or the C++ source files in the current directory will be used when building with cxx.
Packaging a project into $pkgdir:
DESTDIR="$pkgdir" PREFIX=/usr cxx install
Packaging a project into a directory named pkg:
cxx pkg
Build a small executable:
cxx small
Build an executable with optimization flags turned on:
cxx opt
Strict compilation flags (complains about all things):
cxx strict
Sloppy compilation flags (will ignore all warnings):
cxx sloppy
Get the current version:
cxx version
Directories
- The top level directory, or
src/, or a custom directory can contain at least one source file containing amainfunction. - The name of the produced executable will be the same as the name of the parent directory, or
mainif the parent directory issrc. include/can contain all include files belonging to the project.common/can contain all source code that can be shared between multiple executables.img/can contain images.shaders/can contain shaders.data/can contain all other data files needed by the program.shared/can contain all files optionally needed by the program, like example data.
Testing
- All source files, except the one containing the
mainfunction, can have a corresponding_testfile. For instance:quaternions.ccandquaternions_test.cc. - When running
cxx test, the_test.*files will be compiled and run. *_test.*files must each contain amainfunction.
Defines
These defines are passed to the compiler, if the corresponding paths exist (or will exist, when packaging):
DATADIRis defined as./dataor../data(when developing) and$PREFIX/share/application_name/data(at installation time)IMGDIRis defined as./imgor../img(when developing) and$PREFIX/share/application_name/img(at installation time)SHADERDIRis defined as./shadersor../shaders(when developing) and$PREFIX/share/application_name/shaders(at installation time)SHAREDIRis defined as./shareor../share(when developing) and$PREFIX/share/application_name(at installation time)RESOURCEDIRis defined as./resourcesor../resources(when developing) and$PREFIX/share/application_name/resources(at installation time)RESDIRis defined as./resor../res(when developing) and$PREFIX/share/application_name/res(at installation time)
(application_name is just an example).
This makes it easy to have an img, data or resources directory where files can be found and used both at development and at installation-time.
See examples/sdl2 and examples/win64crate for examples that uses IMGDIR.
See examples/mixer for an example that uses RESOURCEDIR.
An alternative metho
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
