"Rules" is a set of GNU makefile scripts for building C and C++ projects under either GCC on Linux or MSVC on Windows. It's super easy to setup, very little configuration and mostly just works out of the box.
- Automatically builds every source file in the project directory (C and C++ only, not sub-folders)
- Defaults to MSVC on Windows and GCC on Linux, other toolchains configurable
- .h dependency generation (including a custom filter script for MSVC to capture dependencies accurately)
- Support for sub-projects that are automatically built before the parent project
- Support for link-projects that are automatically built before and linked with the parent project
- Supports building executables, shared libraries (dll/so) and static libraries
- Debug and Release mode builds
- Builds to an output directory
- Automatic project name from the project directory name
- Platform independent settings for defines and include paths
- Platform dependent settings for MSVC vs GCC
- Helper targets for clean, rebuild and run
- Global configuration via optional user supplied Config.mk file
- Precompiled header support for MSVC (not GCC since it gives little benefit)
Rules was partially inspired by the makefile scripts in Rene Stange's circle project.
Here's a simple example of how to use Rules:
-
Create a project directory
-
Clone the Rules repository. Where you put it doesn't really matter, but as a sub-directory of your project is often a good idea. (if your project is under git control, as a submodule works well)
-
Create a Makefile in your project directory:
include ./Rules/Rules.mk
- Create a C or C++ file in your project directory:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
-
Additional steps for MSVC:
-
Ensure NodeJS installed and available on the path. (A simple node script is used to process the output of the MSVC cl.exe compiler to pick up the .h file dependencies)
-
Run the Visual Studio vcvarsall script before running make so the environment is correctly configured for command line builds. There's a vcvars.bat file in the Rules folder that runs the VS2019 Community edition version of these tools.
-
you'll need to have set of posix like commands available on your path, including GNU
make
,rm
etc...
-
Now, in the project directory run the following commands
make
- builds the projectmake clean
- cleans the project (and all sub-projects)make rebuild
- cleans and builds the projectmake run
- runs the project
Other targets include clean-this
, rebuild-this
, sub-projects
and clean-sub-projects
.
You can adjust your project's setting by adding the following variable declarations in the makefile before including the Rules.mk file.
PROJNAME
- changes the name of the project from the default (which is the directory name)PROJKIND
- eitherexe
for executable,so
for shared library (ie: .so or .dll) orlib
for a static librarySUBPROJECTS
- paths to a set of folders that will be made, cleaned etc... before this project. These folders should have makefiles to build the project and can be Rules based, or not.LINKPROJECTS
- paths to a set of project folders that will be not only made with this project, but also linked with this project. Generally these projects should use Rules as their build process and be eitherlib
orso
projects.INCLUDEPATH
- a set of folders to be added to the compiler's include pathDEFINE
- a set of symbols to be defined for the compileLIBS
- libraries to be passed to linker (platform independent)CONFIG
-debug
orrelease
OUTDIR
- where to place build output files (defaults tobin/$(CONFIG)
)OBJDIR
- where to place intermediate object files (defaults to$(OUTDIR)
)SRCDIR
- one or more directories where source files will be searched (defaults to.
)TOOLCHAIN
- the tool chain to be invoked for building. Typically set automatically to eithermsvc
orgcc
PCH_C
andPCH_CPP
- the precompiled header source (C and/or C++) file for MSVC builds. (eg: stdafx.cpp). The corresponding .h file is determined by changing the extension to .hMSVC_CFLAGS
andGCC_CFLAGS
- toolchain specific C compiler flagsMSVC_CPPFLAGS
andGCC_CPPFLAGS
- toolchain specific C++ compiler flagsMSVC_LDFLAGS
andGCC_LDFLAGS
- toolchain specific linker flagsMSVC_ARFLAGS
andGCC_ARFLAGS
- toolchain specific librarian (archive) flagsMSVC_LIBS
andGCC_LIBS
- toolchain specific libraries to link with
If you're using Rules to build a number of projects, you might have common settings across all projects. These can be
configured in either Config.mk
or Config2.mk
in the parent directory of the Rules sub-directory.
If present, these files will be processed after your per-project settings (in the project make files), but before most of
Rules.mk
.
Rules includes two built-in toolchains - msvc
and gcc
. You can create additional toolchains by defining a new file
named Rules-<custom_toolchain_name>.mk
in the Rules subdirectory and setting the TOOLCHAIN variable to select it.
See the existing Rules-msvc
and Rules-gcc
for how these files should be setup.