OpenBSD GCC Compiler - Beginner Guide

Contents
OpenBSD GCC Compiler - Beginner Guide
Step 1: Install GCC
Step 2: Your First C Program
Step 3: Compiling with Warnings and Debugging
Step 4: Compiling C++ Programs
Step 5: Making a Makefile

OpenBSD GCC Compiler - Beginner Guide

The GCC (GNU Compiler Collection) is essential for compiling C and other programs. Here's how to install and use it on OpenBSD.

Step 1: Install GCC

OpenBSD uses the pkg_add command to install software.

pkg_add gcc - installs the GCC compiler collection

What you get: The GCC compiler, assembler, and related tools for compiling C, C++, and other languages.

After installation: Verify it worked:
gcc --version - should display version information (e.g., gcc (OpenBSD) 11.4.0)

Step 2: Your First C Program

Let's compile a simple "Hello, World!" program.

Create the source file:
vi hello.c

Press i to insert, type the following, press Esc, then type :wq and press Enter:

#include 
int main() {
    printf("Hello, OpenBSD World!\n");
    return 0;
}


Compile the program:
gcc hello.c -o hello

What this does:
gcc - the compiler
hello.c - your source file name
-o hello - name the output file hello (instead of the default a.out)

Run your program:
./hello

You should see: Hello, OpenBSD World! displayed in the terminal.

Step 3: Compiling with Warnings and Debugging

For better code quality, use these useful flags:

-Wall: Show all common warnings
-Wextra: Show extra warnings beyond Wall
-g: Include debugging information (allows using gdb debugger)

Example command:
gcc -Wall -Wextra -g hello.c -o hello

This will:
- Warn about potential issues in your code
- Add debugging symbols so you can trace through the program later
- Help you write more robust code

Step 4: Compiling C++ Programs

To compile C++ programs, use g++ (which is installed with gcc):

g++ hello.cpp -o hello

Or with warnings:
g++ -Wall -Wextra -g hello.cpp -o hello

C++ specifics: C++ has more compiler warnings than C due to its complexity.
Pay attention to warnings about deprecated functions, unused variables, etc.

Step 5: Making a Makefile

For larger projects, use a Makefile to automate compilation.

Create a Makefile:
vi Makefile

Type the following, press Esc, then type :wq and press Enter:

CC = gcc
CFLAGS = -Wall -Wextra -g
 
all: hello
 
hello: hello.c
    $(CC) $(CFLAGS) -o hello hello.c
 
clean:
    rm -f hello


Now compile: Simply type:
make - compiles your program using the Makefile

To clean up: make clean - removes the compiled files

Ready for more development tools? Proceed to Lesson 62: Make Build System.

Next: OpenBSD Virtual Machines - Beginner Guide

Article author: Arthur Isaev

Related articles
UNIX
OpenBSD
OpenBSD course
Architecture Compatibility
Prepare Installation Media
BIOS Installation
UEFI Installation
Disk Partitioning
Base System Installation
Configure Network
Disable Firewall Temporarily
OpenSSH Server
Update System
Security Basics
Frequently Asked Questions
Virtualization Overview
Docker Containers
Kubernetes
PF NAT Configuration
PF Suppression
IPsec VPN
WireGuard VPN
Advanced Cron
Systemd Compatibility
PF Firewall Introduction
Shells
Environment Variables
Sudoers Configuration
/etc/fstab
Group Management
Password Security
Issue and Banner
PAM Configuration
TCP Wrappers
Intrusion Detection
PF Advanced Rules
Log Analysis
Advanced Backup
Kernel Update
Certificate Management
Access Control Lists (ACLs)
User Security Settings
Vulnerability Management
Security Audit Tools
Security Parameter Tuning
SSH Key Authentication
sudo vs doas
Users and Groups
PostgreSQL Introduction
Services Management
Web Server Overview
Install Apache Web Server
Install Nginx Web Server
Database Setup
Install PHP
Firewall Configuration
SSL/TLS Certificate
Firewall Configuration
Install PHP
Install PHP
Advanced SSL/TLS Configuration
User Management Advanced
PF Logging and Monitoring
Custom Kernel compilation
Advanced doas Configuration
Resource Limits
DHCP Server
Mail Server
SSH Hardening
System Logging
Disk Encryption
Window Managers
Text Editors
Package Management
Cron Jobs
System Monitoring
PF Firewall Advanced
SSH Configuration
Services Management
PHP and Web Stack
Advanced User Management
Swap and Memory
Kernel Parameters
Boot Process
Mounting Filesystems
UFS Filesystem
Softraid
Disk Management
DNS Configuration
Hosts File
Network Statistics
Routing
Network Bridging
System Upgrade
Backup Strategies
Log Rotation
GCC Compiler
Make Build System
Debugging
System Call Tracing
FIPS 140-2
Audit System
Security Compliance
Virtual Machines
LXC Containers
HTTP API
Advanced Monitoring
SELinux (Ported)
Course Conclusion
OpenBSD Architecture - Beginner Guide
OpenBSD Installation Media - Beginner Guide
OpenBSD Disk Partitioning - Beginner Guide
OpenBSD System Updates - Beginner Guide
OpenBSD PF Firewall - Beginner Guide
OpenBSD SSH Configuration - Beginner Guide
OpenBSD User Management - Beginner Guide
OpenBSD Web Server - Beginner Guide
OpenBSD PostgreSQL Database - Beginner Guide
OpenBSD PF Logging and Monitoring - Beginner Guide
OpenBSD SSH Hardening - Beginner Guide
OpenBSD Package Management - Beginner Guide
OpenBSD Firewall Configuration - Beginner Guide
OpenBSD Backup Strategies - Beginner Guide
OpenBSD GCC Compiler - Beginner Guide
OpenBSD Virtual Machines - Beginner Guide
OpenBSD Course Conclusion - Beginner Guide

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: