- Notifications
You must be signed in to change notification settings - Fork5
🔌 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.
License
xyproto/cxx
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Make modern C++ easier to deal with.
Have you ever had a singlemain.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 havescons,python and all required libraries for your project installed.
It should be possible to compile most of the examples in theexamples directory, simply by runningcxx in each directory.
Usingcxx 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 singlemain.cpp) or have acxx-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 thetests directory. Please submit a pull request if you have improvements for your platform!
Several examples are included in theexamples 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, usingwine).
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 beforedtolnay/cxx).
Ifcxx is available by using your favorite package manager, that's usually the best way.
First installcxx, so that it is in the path. Here is one way, usinggit clone, GNU Make andsudo:
git clone https://github.com/xyproto/cxxcd cxxmakesudo make installFor 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-devFor FreeBSD, here is one way of installing only the basic dependencies and CXX:
pkg install -y bash git gmake pkgconf python3 sconsgit clone https://github.com/xyproto/cxxcd cxxgmakeThen as root:
gmake installOne 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 fcgitest -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 installInstalling 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 fcgigit clone https://github.com/xyproto/cxx && cd cxx && make installJust installcxx from AUR.
Create amain.cpp file:
#include<cstdlib>#include<iomanip>#include<iostream>#include<ostream>#include<string>usingnamespacestd::string_literals;classPoint {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;}Pointoperator+(const Point& a,const Point& b){return Point { .x = a.x + b.x, .y = a.y + b.y, .z = a.z + b.z };}Pointoperator*(const Point& a,const Point& b){return Point { .x = a.x * b.x, .y = a.y * b.y, .z = a.z * b.z };}intmain(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:
cxxRebuilding can be done with:
cxx rebuildBoth building and running can be done with:
cxx runIf you wish to optimize the program, running it in a way that also records profiling information can be done with:
cxx recThe next time the project is built, the profiling information is used to optimize the program further:
cxxcxx testcxx cleancxx clangcxx -C examples/hellosudo PREFIX=/usr cxx installEithermain.cpp or the C++ source files in the current directory will be used when building withcxx.
DESTDIR="$pkgdir" PREFIX=/usr cxx installcxx pkgcxx smallcxx optcxx strictcxx sloppycxx version- 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.
- 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.
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 animg,data orresources directory where files can be found and used both at development and at installation-time.
Seeexamples/sdl2 andexamples/win64crate for examples that usesIMGDIR.
Seeexamples/mixer for an example that usesRESOURCEDIR.
An alternative method to using defines (defined with-D when building) is to use something likeSDL_GetBasePath(). Example:res_path.h.
- No configuration files are needed, as long as theCXX directory structure is followed.
- Auto-detection of include, define and library flags, based on which files are included from
/usr/include, usingpkg-config. It also uses system-specific ways of attempting to detect which packages provides which compilation flags. Not all libraries, include files and cxxflags can be auto-detected yet, but more are to be added. - Built-in support for testing, clang, debug builds and only rebuilding files that needs to be rebuilt.
- Does not use a
builddirectory, it's okay that themainexecutable ends up in the root folder of the project.main.cppcan be placed in the root folder of the project, or in its own directory. - Should be easy to port to other systems that also has a package manager and pkg-config (or equivalent way to discover build flags).
- Your include files are expected to be found in
./includeor../include. - Source files used by multiple executables in your project are expected to be placed in
./commonor../common. - Tests are expected to end with
_test.cppand will be ignored when buildingmain.cpp. - See the
helloexample in theexamplesdirectory for the suggested directory structure. - For now,CXX is only meant to be able to build executables, not libraries.
- The dependency discovery is reasonably fast, the compilation itself still takes the longest time. Not to speak of the time it can take to discover build flags for some C++ libraries and features manually.
- For now, the generated
CMakeLists.txtfile is only meant to be used on the system it was generated on, not shipped for many different systems.
For a "Hello, World!" program that places the text-generation in astring hello() function, this is one way to structure the files, for separating the code into easily testable source files:
.├── hello/main.cpp├── hello/include/hello.h├── hello/include/test.h├── hello/common/hello.cpp└── hello/common/hello_test.cpp.└── hello/hello1/main.cpp└── hello/hello2/main.cpp└── hello/include/hello.h└── hello/include/test.h└── hello/common/hello.cpp└── hello/common/hello_test.cppmain.cpp
#include<iostream>#include"hello.h"intmain(){ std::cout <<hello() << std::endl;return0;}
hello.h
#pragma once#include<string>std::stringhello();
hello.cpp
#include"hello.h"usingnamespacestd::literals;std::stringhello(){return"Hello, World!"s;}
hello_test.cpp
#include"test.h"#include"hello.h"usingnamespacestd::literals;voidhello_test(){equal(hello(),"Hello, World!"s);}intmain(){hello_test();return0;}
test.h
#pragma once#include<iostream>#include<cstdlib>template<typename T>voidequal(T a, T b){if (a == b) { std::cout <<"YES" << std::endl; }else { std::cerr <<"NO" << std::endl;exit(EXIT_FAILURE); }}
sconsmakeg++with support for-std=c++20.pkg-config, for systems where pkg-config is available
clang++with support for-std=c++20(build withcxx clang).lldborgdbfor debuggingpkgfileon Arch Linux, for faster dependency discovery.apt-fileon Debian/Ubuntu, for faster dependency discovery.x86_64-w64-mingw32-g++ordocker, for cross-compiling executables for 64-bit Windows. The docker service must be up and running for this to work.wine, for testing executables compiled for 64-bit Windows (cxx run).valgrind, for profiling (cxx valgrind).kcachegrind, for viewing the information produced byvalgrind.gprof2dotanddot, for producing a graph from the information produced by valgrind.vagrant, for testingcxx on other operating systems.figlet, for nicer output when running thetests/build_all.shscript, for building all the examples.- Development packages for
SDL2,OpenGL,glut,glfw,sfml,GTK+4,Qt6andVulkan, for building and running the examples. x86_64-w64-mingw32-g++ordockeris needed for building thewin64crateexample.clang-formatforcxx fmt.
For installing a recent enough version of C++ on macOS, installing gcc 11 withbrew is one possible approach:
brew install gcc@11The other requirements can be installed with:
brew install scons make pkg-configg++ with support for-std=c++20 should already be installed.
Install scons and base-devel, if needed:
pacman -S scons base-devel --neededYou might need to install GCC 11 from the testing repository, or from a PPA.
Install build-essential, scons and pkg-config:
apt install build-essential scons pkg-configFreeBSD 11.1 comes with C++17 support, but you may wish to install GCC 11 or later.
gcc11 or later should provide support for C++20.
Install pkg-conf, scons and gmake:
pkg install pkgconf scons gmakeManual installation withmake andsudo:
sudo make install
On FreeBSD, usegmake instead ofmake.
If possible, installCXX with the package manager that comes with your OS/distro.
sudo make uninstall
- All include filenames should contain no spaces or special characters (a-z, A-Z, 0-9) and end with
.hor.hpp. - All C++ source filenames should contain no spaces or special characters (a-z, A-Z, 0-9) and end with
.cpp,.ccor.cxx. - The main source file could be named
main.cppormain.cc, but it does not have to. - Files ending with
_test.*are special, and will not be used when compiling the main executable(s).
Projects that already uses CMake (and need no extra command line arguments when running
cmake) are also CXX compatible and can be built with CMake + Ninja like this:cxx ninja
The generated qmake/QtCreator project files were tested with QtCreator 4.6 on Arch Linux.
cxx fmtwill format C++20 source code in a single, fixed, formatting style (clang-format "Webkit"-style), which is not configurable, on purpose. Usingcxx fmtis optional.
The goal is that every executable and project written in C++20 should be able to build withcxx on a modern Linux distro, FreeBSD or macOS system (with Homebrew), without any additional configuration.
If you have a project written in C++ that you think should be able to build withcxx, but doesn't, please create an issue and include a link to your repository.
If running CXX withparallel, make sure to use the--compress or--tmpdir flag to change the location of the temporary SQLite database.
Example build target in a Makefile, for usingparallel andcxx, while disabling warnings:
build: +CXXFLAGS='$(CXXFLAGS) -w' parallel --compress cxx opt -C ::: subdir1 subdir2 subdir3subdir1,subdir2 andsubdir3 are just examples of directory names.
For OpenBSD, install g++ 11 and build withcxx CXX=eg++.
- Only the latest version of GTK and Qt are supported. Currently, that's GTK+4 and Qt6. Please create an issue or submit a pull request if there are new releases of GTK or Qt.
- The GTK and Qt examples are currently only tested on Arch Linux.
Syntastic settings for ViM and NeoVim:
" If your compiler does not support -std=c++20, it is possible to use -std=c++2a, -std=c++2b or -std=c++17.let g:syntastic_cpp_compiler = 'g++'let g:syntastic_cpp_compiler_options = ' -std=c++20 -pipe -fPIC -fno-plt -fstack-protector-strong -Wall -Wshadow -Wpedantic -Wno-parentheses -Wfatal-errors -Wvla'let g:syntastic_cpp_include_dirs = ['../common', './common', '../include', './include']" Ignore some defines and warningslet g:syntastic_quiet_messages = { \ "!level": "errors", \ "regex": [ 'RESOURCEDIR', 'RESDIR', 'DATADIR', 'IMGDIR', 'SHAREDIR', 'SHADERDIR', 'expected .*).* before string constant' ] }- Version: 3.3.3
- License: BSD-3
- Author: Alexander F. Rødseth <xyproto@archlinux.org>
About
🔌 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.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.