Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
GCC (GNU Compiler Collection) is a vital tool for developers who need to compile code written in languages such as C, C++, and Fortran. While macOS comes with its own compiler, clang
, GCC is still widely used and sometimes preferred for its extensive features and compatibility with various open-source projects. This article will guide you through installing and using GCC on macOS.
Examples:
Installing GCC via Homebrew: Homebrew is a popular package manager for macOS that simplifies the installation of software. To install GCC using Homebrew, follow these steps:
First, ensure you have Homebrew installed. If not, you can install it by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is installed, you can install GCC by running:
brew install gcc
After the installation is complete, you can verify the installation by checking the version of GCC:
gcc-11 --version
Note: Replace gcc-11
with the version number installed.
Compiling a Simple C Program: Once GCC is installed, you can use it to compile C programs. Here’s a simple example:
Create a C source file named hello.c
:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
To compile the program, navigate to the directory containing hello.c
and run:
gcc-11 hello.c -o hello
Again, replace gcc-11
with the appropriate version number.
Run the compiled program:
./hello
Compiling a C++ Program: GCC also supports C++. Here’s how you can compile a simple C++ program:
Create a C++ source file named hello.cpp
:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
To compile the program, navigate to the directory containing hello.cpp
and run:
g++-11 hello.cpp -o hello
Run the compiled program:
./hello