Prerequisites
# Install essential build tools
pkg install git
pkg install autoconf automake libtool
pkg install cmake
pkg install gmake
pkg install pkgconf
# Install compiler (usually included in base)
# FreeBSD uses clang by default
clang --version
Generic Build Process
# Download source
git clone https://github.com/project/software.git
cd software
# Standard autotools build
./configure --prefix=/usr/local
make
sudo make install
# Or with cmake
mkdir build && cd build
cmake ..
make
sudo make install
FreeBSD-Specific Notes
Compiler Differences
# FreeBSD uses clang, not gcc
cc --version # Shows clang
# If software requires gcc
pkg install gcc12
export CC=gcc12
export CXX=g++12
# Use gmake instead of make for GNU makefiles
gmake
Library Paths
# FreeBSD library locations
/usr/lib # Base system libraries
/usr/local/lib # Ports/pkg installed libraries
/usr/local/include # Headers
# Set library path
export LDFLAGS="-L/usr/local/lib"
export CPPFLAGS="-I/usr/local/include"
# Or use pkg-config
pkg install pkgconf
pkg-config --libs openssl
./configure \
--prefix=/usr/local \
--sysconfdir=/usr/local/etc \
--localstatedir=/var \
--mandir=/usr/local/man \
LDFLAGS="-L/usr/local/lib" \
CPPFLAGS="-I/usr/local/include"
Building Example: nginx
# Install dependencies
pkg install pcre2 openssl
# Download source
fetch https://nginx.org/download/nginx-1.24.0.tar.gz
tar xzf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# Configure
./configure \
--prefix=/usr/local \
--conf-path=/usr/local/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-pcre
# Build
make
# Install
sudo make install
Building from Git
# Clone repository
git clone https://github.com/example/project.git
cd project
# Check out specific version
git checkout v1.0.0
# Initialize submodules if needed
git submodule update --init --recursive
# Generate configure script (if not present)
autoreconf -i
# Build
./configure && make && sudo make install
Troubleshooting Builds
# Find package containing header
pkg search -o filename.h
# Or search ports
cd /usr/ports
make search name=filename
Linker Errors
# Check library is installed
ldconfig -r | grep libname
# Add library path
export LD_LIBRARY_PATH=/usr/local/lib
ldconfig -m /usr/local/lib
BSD vs Linux Differences
# Common fixes for Linux-centric code:
# 1. Use gmake instead of make
# 2. Include <sys/types.h> before other headers
# 3. Use ${PREFIX} instead of hardcoded paths
# 4. Check for __FreeBSD__ define in code
Creating Your Own Port
# Basic port Makefile structure
PORTNAME= myapp
PORTVERSION= 1.0
CATEGORIES= misc
MASTER_SITES= https://example.com/
MAINTAINER= [email protected]
COMMENT= My application description
LICENSE= MIT
.include <bsd.port.mk>
advanced | Package Management | Updated 2025-01-15
- freebsd
- compile
- source
- make
- build