Skip to content

This bash script serves as a quick alternative to tools like Make and CMake.

License

Notifications You must be signed in to change notification settings

jason80/alter-bash-build

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Alternative Build Script for C and C++

This bash script serves as a quick alternative to tools like Make and CMake.

Usage

For the following example project structure, simply copy the build.sh file.

  • 📂 Project root
    • 📂 src
      • 📄 main.c
      • 📄 test.c
    • 📂 include
      • 📄 test.h
      • 📄 tools.h
    • 📄 build.sh

Then, simply modify the first lines of the script:

SRC_DIR="src"				# Source code dir.
SRC_EXT="*.c"				# *.c  *.cpp extensions.
TYPE="executable"			# "executable" target, "static" library or "shared" library.
BUILD_DIR="build"			# Target build directory.
TARGET="test"				# Name of the executable or library.
CXX="gcc"					# "gcc" or "g++" compiler selection.
CFLAGS="-Wall -I include"	# Compiler options.
LFLAGS=""					# Linker options.
MODULE_DEPS=""				# Module lib dependences.

Running

Mark build.sh as executable using:

chmod +x build.sh

and run the script in the following ways:

  • ./build.sh or ./build.sh build: build the project.
  • ./build.sh clean: clean the built files.
  • ./build.sh run: runs the target (only executables).

Static and Shared Libraries

Setting the option TYPE="static", the script will generate a static library inside the build/ directory.

Then: if target="Example", it will generate libExample.a

On the other hand, the option TYPE="shared" generates a shared library (.so).

Dependencies

Only the translation files that have been modified compared to the compiled objects will be compiled. If any object is compiled, the target is relinked.

The script enables the compiler option to generate dependency files within the build directory. It then checks if they have been modified to regenerate the object.

Modules

Projects can be created with modules as follows:

  • 📂 Project root
    • 📂 ExtendLib

      • 📂 src
        • 📄 utils.cpp
      • 📂 include
        • 📄 utils.h
      • 📄 build_1.sh
    • 📂 main

      • 📂 src
        • 📄 main.cpp
      • 📄 build_2.sh
    • 📄 build.sh

build_1.sh (lib):

SRC_DIR="src"
SRC_EXT="*.cpp"
TYPE="static"			# Static library
BUILD_DIR="build"
TARGET="ExtendLib"		# Generates: "build/libExtendLib.a"
CXX="g++"
CFLAGS="-Wall -I include"
LFLAGS=""
MODULE_DEPS=""

build_2.sh (main module):

SRC_DIR="src"
SRC_EXT="*.cpp"
TYPE="executable"		# Static library
BUILD_DIR="build"
TARGET="test"			# Executable name
CXX="g++"
CFLAGS="-Wall -I include -I ../ExtendLib/include"	# Compiler options
LFLAGS="-L ../ExtendLib/build -l ExtendLib"			# Linker options
MODULE_DEPS="../ExtendLib/build/libExtendLib.a"		# Add module dependence to rebuild project

build.sh (simple root build script):

#!/bin/bash
./ExtendLib/build_1.sh $1
./main/build_2.sh $1

Consider ExtendLib and main as modules. main depends on ExtendLib. The following line makes the dependency more robust:

MODULE_DEPS="../ExtendLib/build/libExtendLib.a"

If anything in ExtendLib changes, the main target is rebuilt.

When using the run option, it checks if the target is available. It also recompiles if there are changes.

About

This bash script serves as a quick alternative to tools like Make and CMake.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages