Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
KRMisha / Makefile Public template
Cross-platform C++ Makefile project template
License
KRMisha/Makefile
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A cross-platform C++ Makefile for any project!
- Cross-platform: works on Linux, macOS, and Windows (32- and 64-bit)
- Automatic: all source files are automatically found and compiled
- Efficient: only the modified files are recompiled and their dependencies are automatically generated
- Debug and release configurations
- Configurable: easily add libraries or change compilation settings
- Format source files thanks to clang-format
- Generate documentation from Doxygen comments
Alternatively, Clang can be used instead of GCC (see here). The instructions below will focus on GCC.
- Linux:
- Debian/Ubuntu: sudo apt install build-essential
- Fedora: sudo dnf install gcc-c++ make
- Arch: sudo pacman -S base-devel
- Run the following command: xcode-select —install
- In the window which pops up, click «Install» and follow the instructions.
You will need to use Git Bash over PowerShell or cmd.exe each time the make command is used in this README.
$ make help Usage: make target. [options]. Targets: all Build executable (debug mode by default) (default target) install Install packaged program to desktop (debug mode by default) run Build and run executable (debug mode by default) copyassets Copy assets to executable directory for selected platform and configuration cleanassets Clean assets from executable directories (all platforms) clean Clean build and bin directories (all platforms) format Run clang-format on source code doc Generate documentation with Doxygen help Print this information printvars Print Makefile variables for debugging Options: release=1 Run target using release configuration rather than debug win32=1 Build for 32-bit Windows (valid when built on Windows only) Note: the above options affect all, install, run, copyassets, and printvars targets
This will compile the executable and output it inside the bin directory. This is equivalent to make all .
Using a different compiler
By default, all builds use GCC. To use another compiler, override the CXX variable when invoking make . For example, to use Clang:
This will run the executable, rebuilding it first if it was out of date.
To add files to be copied next to the executable’s output location, simply add them to the assets directory. Then, use the following command:
This will copy the contents of assets to the current bin directory, preserving their folder structure.
If you have certain assets which you wish to only copy for certain platforms, you can do the following:
- Create an assets_os/ directory at the root of the project. The directory should be named either linux , macos , windows32 , or windows64 based on the desired platform for the assets.
- Inside this new directory, add all the assets to be copied only for this platform.
- Use the make copyassets command as usual. The files copied to the current bin directory will be the combination of the files in assets and assets_os , with files in assets_os overwriting those in assets in case of naming clashes.
The assets_os directory is useful for holding Windows DLLs which need to be copied next to the executable (using assets_os/windows64 or assets_os/windows32 , depending on the target version).
This will remove all the files in all bin directories except the executables.
This will remove the entire build and bin directories.
Certain options can be specified when building, running, and copying assets. These will modify the settings used to build the executable and affect what is considered the current bin directory when running a command.
By default, builds are in debug mode. To build for release (including optimizations), add the release=1 option when invoking make .
To use the release version of the executable, release=1 must also be specified when running or when copying assets. For example:
make copyassets run release=1
By default, builds on Windows target 64-bit. To build a 32-bit executable, add the win32=1 option when invoking make .
This can also be combined with the release=1 option to build for 32-bit release.
Don’t forget to also specify win32=1 when running or when copying assets!
This will format all files in the src and include directories using clang-format according to the rules found in .clang-format .
- Create a new doc directory at the root of the project.
- Generate a new Doxyfile in doc/Doxyfile :
Updating the documentation
This will generate the documentation according to the rules found in doc/Doxyfile and output it in the doc directory.
Note: steps 1-5 are not needed if the library’s development package has been installed in a default system-wide directory, such as /usr/lib and /usr/include on Linux. If this is the case, only step 6 needs to be followed to link the library (if the library is not header-only).
- Create a new libs directory at the root of the project if this is the first library to be added to your project. Conversely, if you have previously added a library, this directory will already exist.
- Inside the libs directory, create a subdirectory to contain the library’s files.
- Download the necessary files for your chosen library and add them to libs/ . Depending on the type of library (e.g. traditional vs header-only), the folder structure inside libs/ will vary. Refer to your chosen library’s documentation for more information.
- Include the library’s header files: add -Ilibs//include to the INCLUDES variable at line 21 of the Makefile. The include part of the path above refers to a directory containing all the library’s header files. Note that the actual location of the header files will depend on the layout of the library files you added in step 3 and may thus be named differently. For a header-only library, for example, the header files may be directly located in libs/ .
- Specify the library’s compiled files: add -Llibs//lib to the LDFLAGS variable at line 32 of the Makefile. The lib part of the path above refers to a directory containing all the library’s compiled files ( .so , .a , .lib , .framework , or any type of file depending on the platform). Just like in step 4, it’s important to note that the actual location of the compiled files will depend on the layout of the library you added in step 3 and may thus be named differently. For a header-only library, this step is not needed as no library files need to be linked. You may also have chosen to build the library from source in step 3. In that case, the lib directory should contain the output of the compiled library. Refer to your library’s documentation for more information.
- Add the library’s name to link it during compilation: add -l to the LDLIBS variable at line 35 of the Makefile. Depending on the library, more than one library name may need to be added with the -l flag. Refer to your library’s documentation for the names to use with the -l flag in this step. For a header-only library, this step is not needed as no library files need to be linked.
Everything should now be good to go!
Platform-specific library configuration
The steps above show how to configure a library using the common INCLUDES , LDFLAGS , and LDLIBS variables which are shared between all platforms. However, in many cases, the library may need to be linked differently by platform. Examples of such platform-specific library configurations include:
- Adding a library needed only for code enabled on a certain platform
- Using -framework over -l to link a library on macOS
- Specifying a different path for a library’s compiled files with -L
The Makefile is designed to support these kinds of platform-specific configurations alongside one another.
Lines 51-87 of the Makefile contain platform-specific INCLUDES , LDFLAGS , and LDLIBS variables which should be used for this purpose. To configure a library for a certain platform, simply add the options to the variables under the comment indicating the platform.
The common INCLUDES (line 21), LDFLAGS (line 32), and LDLIBS (line 35) variables should only contain options which are identical for all platforms. Any platform-specific options should instead be specified using lines 51-87.
Frequently changed settings
In addition to adding libraries, you may wish to tweak the Makefile’s configuration. The following table presents an overview of the most commonly changed settings of the Makefile.
Configuration Variable Line Change the output executable name EXEC 6 Select the C++ compiler (e.g. g++ or clang++ ) CXX 27 Add preprocessor settings (e.g. -D ) CPPFLAGS 24 Change C++ compiler settings (useful for setting C++ version) CXXFLAGS 28 Add/remove compilation warnings WARNINGS 29 Add includes for libraries common to all platforms (e.g. -Ilibs//include ) INCLUDES 21 Add linker flags for libraries common to all platforms (e.g. -Llibs//lib ) LDFLAGS 32 Add libraries common to all platforms (e.g. -l ) LDLIBS 35 Add includes/linker flags/libraries for specific platforms INCLUDES — LDFLAGS — LDLIBS 51-87 All the configurable options are defined between lines 1-87. For most uses, the Makefile should not need to be modified beyond line 87.
. ├── assets │ └── ├── assets_os │ └── linux | macos | windows32 | windows64 │ └── ├── bin │ └── linux | macos | windows32 | windows64 │ └── debug | release │ ├── executable │ └── ├── build │ └── linux | macos | windows32 | windows64 │ └── debug | release │ ├── **/*.o │ └── **/*.d ├── doc │ ├── Doxyfile │ └── **/*.html ├── include │ └── **/*.h ├── libs │ └── │ ├── bin │ ├── include │ └── lib ├── src │ ├── main.cpp │ └── **/*.cpp ├── .clang-format ├── .gitattributes ├── .gitignore ├── Makefile └── README.md
To comply with the terms of the MIT license in your project, simply copy-pasting the entire contents of the provided LICENSE file as a comment at the top of the Makefile is sufficient. By doing so, you do not need to include the LICENSE file directly since it is is now contained in the Makefile. You can then reuse the LICENSE filename for your own license if you wish.
- Features
- Prerequisites
- GCC & Make
- Optional dependencies
- Overview of commands
- Building
- Using a different compiler
- Copying assets
- Cleaning assets
- Release
- 32-bit (Windows only)
- First time use
- Updating the documentation
- Adding a library
- Platform-specific library configuration