Skip to content

Customisable Makefile to build executable, shared or static library.

License

Notifications You must be signed in to change notification settings

Hazurl/Makefile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Makefile

Can be use to compile an executable, a static library or a shared library. See the top of the Makefile to set up it.

Rules

X can be replace by executable, shared or static in the following rules.

make : build the excutable, the static library and the shared library

make clean : destroy the build folder

make clean-X : destroy the build/X folder

make X : make the X target

make re : clean then build all three targets

make re-X : clean then build the target

make run : build then run the executable

make re-run : clean, build then run the executable

make valgrind : similar to make run but execute it with valgrind (valgrind $(executable_path))

make re-valgrind : similar to make re-run but execute it with valgrind (valgrind $(executable_path))

where-X : output the path to the target

How to use

Just copy the Makefile in your project and set it up. The minimal setup consist of PROJECT_NAME, TARGET_ALL, SRC_MAINS and SRC_MAIN.

Folder settings

SRC_FOLDER is the directory of your sources files (relative to the project directory).

INC_FOLDER is the directory of your header files (relative to the project directory).

Note: You can set the same folder for both.

BUILD_*_FOLDER is just the name of the build folder. BUILD_FOLDER is destroy with the make clean command. BUILD_X_FOLDER is destroy with the make clean-X command.

Note: If you want three directories for each target you can write BUILD_FOLDER := executable shared static and set each BUILD_X_FOLDER to their respective folder (i.e. BUILD_STATIC_FOLDER := static).

General settings

PROJECT_NAME is only use for targets.

CXX is the compiler used to build and link the final executable and the shared library.

SXX is the linker used to archive the static library.

TARGET_X is the file to build.

TARGET_ALL is all the targets build when the command make or make all is executed.

Files settings

EXT_SRC_FILE, EXT_INC_FILE are just the extension used in your project(.cpp and .hpp by default).

header-of is a function that given a source file return his header. Both the parameter and the return value or relative to SRC_FOLDER and INC_FOLDER. $(1:%$(EXT_SRC_FILE)=%$(EXT_INC_FILE)) will just replace the source extension by the header extension with a pattern match. ex: folder/source.cpp is given %$(EXT_SRC_FILE) is the pattern (%$(EXT_SRC_FILE) == %.cpp) so % will be equal to folder/source, the return value is %$(EXT_INC_FILE) so %.hpp, i.e. folder/source.hpp.

Note: If your sources files are offset by some folders you can use $(1:sub/folder/%$(EXT_SRC_FILE)=%$(EXT_INC_FILE)). And $(1:%$(EXT_SRC_FILE)=sub/folder/%$(EXT_INC_FILE)) if the headers are offset by some folders. Note: If the header doesn't exist for a source file, it is ignored. This function is here just to checks if the header has been modified since the last build.

SRC_EXCLUDE_FILE will not build all source files in this list, separate them with a space.

SRC_MAINS is the list of all source file that have a main function. They are excluded for any build.

SRC_MAIN is the source file used for the executable only.

Note: example of multiple main here

Flags

FLAG is used for each build command. STATIC_LINK_FLAG is used with SXX.

INC_FLAG is used to specify different path to headers file, by default -I $(INC_FOLDER). -I must be before each path. This argument affect the directive #include <...>.

LIBRARY

LIBS_PATH is used by the linker to search libraries. -L must be before each path.

Note: example here

LIBS are all the libraries to link with the targets.

Note: -lxyz will search for libxyz.so or libxyz.a in the default system path and the paths in LIBS_PATH Note: example here

LIB_TO_BUILD is used to specify all libraries to build. You need to create a rule for all of them, here is an example of a library that also use make:

LIB_TO_BUILD := lib/libxyz.so

lib/libxyz.so:
  cd lib/ && make libxyz.so

If you like to log what's going on you can use @$(call _special,BUILDING $@) before the build command. $@ is a variable containing the target's rule so libxxx.so. _special can be replaced by _header, _sub-header or _build-msg.

Note: example here (sed '/s/^/\t/' is used to offset each output message by one tabulation).

About

Customisable Makefile to build executable, shared or static library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published