Using Eclasses

An eclass is a collection (library) of functions or functionality that is shared between packages. See Eclass Writing Guide for the full story on what eclasses can do, how they work and how to write them, and the eclass reference for documentation on various commonly used eclasses. This section only explains how to use an eclass which has already been written.

The inherit Function

To use an eclass, it must be 'inherited'. This is done via the inherit function, which is provided by The inherit statement must come at the top of the ebuild, before any functions. Conditional inherits are illegal (except where the inheritance criteria are cache-constant — see The Portage Cache).

After inheriting an eclass, its provided functions can be used as normal. Here's an example ebuild, foomatic-0.1-r2.ebuild, which uses four eclasses:

# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2


inherit eutils bash-completion flag-o-matic autotools

DESCRIPTION="Tool for foo"

KEYWORDS="alpha ~amd64 ~x86 ~x86-fbsd"


src_prepare() {
	epatch "${FILESDIR}/${P}-gentoo.patch"

src_configure() {
	econf --sysconfdir=/etc/devtodo

src_compile() {
	replace-flags -O? -O1
	emake || die "emake failed"

src_install() {
	emake DESTDIR="${D}" install || die "emake install failed"
	dodoc AUTHORS ChangeLog README TODO || die "dodoc failed"
	dobashcompletion "${FILESDIR}/${PN}.bash-completion" ${PN}

Note the inherit immediately after the header.

The eutils eclass (see eutils.eclass) is needed to get the epatch function. The flag-o-matic eclass (see flag-o-matic.eclass) is needed for replace-flags, and the bash-completion eclass (bash-completion.eclass) is used to handle the bash completion file via dobashcompletion and bash-completion_pkg_postinst.