DEV Community

Cover image for Single Script Can Install a C/C++ Compiler in no time on a Mac
Krunal Kanojiya
Krunal Kanojiya

Posted on

Single Script Can Install a C/C++ Compiler in no time on a Mac

I recently started working on some of the C and C++ related projects, but installation and compiler setup took too much time. I searched on Google related to installation but everyone told me about GCC and G++ Compiler. Also, I am a Mac user so by default Xcode came with the Clang library, But want to use it specifically GCC or G++.

As you know Jetbrains Clion IDE is not freely available. So, we developers heavily depend on VSCode. At last, I decided I had to make something that could do the setup of the C & C++ Compiler automatically.

The one thing I know is Mac supports Homebrew and it can do this thing. So, I started developing a script with basic idea called mac_script.sh


Script do-nothings, just I have to automate some things and I did. Let me share the script with you So, will get an idea of how the script works behind the scenes.

C/C++ Compiler Installation Script.

#!/bin/bash

# Function to check if a command exists
command_exists() {
  command -v "$1" >/dev/null 2>&1
}

# Function to display help
show_help() {
  echo "Usage: $0 [--help]"
  echo
  echo "This script installs Homebrew and GCC on macOS if they are not already installed."
  echo "It also creates a script to compile and run C/C++ code."
  echo
  echo "Options:"
  echo "  --help    Show this help message and exit"
}

# Check for --help argument
if [ "$1" == "--help" ]; then
  show_help
  exit 0
fi

# Detect the operating system
OS="$(uname -s)"

# Install Homebrew on macOS if not installed
if [ "$OS" == "Darwin" ]; then
  if ! command_exists brew; then
    echo "Homebrew not found. Installing Homebrew..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  else
    echo "Homebrew is already installed."
  fi

  # Install GCC if not installed
  if ! command_exists gcc; then
    echo "GCC not found. Installing GCC..."
    brew install gcc
  else
    echo "GCC is already installed."
  fi

  # Create a script to compile and run C/C++ code
  cat << 'EOF' > compile_and_run.sh
#!/bin/bash

# Check if a file is provided
if [ -z "$1" ]; then
  echo "Usage: $0 <source-file>"
  exit 1
fi

# Get the file extension
EXT="${1##*.}"

# Compile and run based on the file extension
if [ "$EXT" == "c" ]; then
  gcc "$1" -o "${1%.c}" && "./${1%.c}"
elif [ "$EXT" == "cpp" ]; then
  g++ "$1" -o "${1%.cpp}" && "./${1%.cpp}"
else
  echo "Unsupported file extension: $EXT"
  exit 1
fi
EOF

  # Make the script executable
  chmod +x mac_script.sh
  echo "Script to compile and run C/C++ code has been created as mac_script.sh"

  # Run the compile_and_run.sh script
  ./mac_script.sh <source-file>
else
  echo "This script is intended to run on macOS."
  exit 1
fi
Enter fullscreen mode Exit fullscreen mode

Let me show you. How to run this script.


Installation

To install the compiler, run the following command in the terminal:

sudo chmod -x mac_script.sh
Enter fullscreen mode Exit fullscreen mode

This will download the compiler and install it on your system. You may need to enter your password to allow the installation.

Usage

Once the compiler is installed, you can use it to compile and run programs. To compile a program, run the following command:

sudo mac_script.sh program.c
Enter fullscreen mode Exit fullscreen mode
sudo mac_script.sh program.cpp
Enter fullscreen mode Exit fullscreen mode

That's it!!!

Conclusion

After running the script everything worked fine as expected. Why I am sharing this is not a script. My main motto is that you can do so many things with shell or bash scripts. Just try it once, You will enjoy it. Or maybe you can contribute to this repo or just upgrade this script and make it for Windows and Linux and add more features.

Top comments (0)