How to personalize a package’s CFLAGS in Gentoo

Ok, last attempt to revive this blog ;-)

Well, there’s a little trick that I’d like to share with you guys that use Gentoo. Some time ago, I had to modify the value of the $CFLAGS environment variable for 2 packages in the Portage because they had some problems when the debugging option was enabled for configure. After some search, I’ve found the solution for this. Here goes the step-by-step (it’s pretty easy, I promise):

1) First, find the package’s path inside the Portage. This path is basically compounded by <category>/<package_name>. Let’s suppose that this path is app-editors/vim, so if your Portage is under /etc/portage, you should have a directory called /usr/portage/app-editors/vim/.

2) Now, you should create a file named /etc/portage/env/<category>/<package_name>, just like that:

#> touch /etc/portage/env/app-editors/vim

3) Edit this file with you preferred editor and put there the $CFLAGS value that you want, like that:


If you want to append some value to the existing $CFLAGS, just do it:


4) You are ready to go! :D

See? It’s pretty easy. Note that you can also add other variables (like $LDFLAGS), and emerge will handle them for you. If you have some trouble with the procedure, just let me know :-).

See ya!


6 Responses to How to personalize a package’s CFLAGS in Gentoo

  1. Donnie Berkholz says:

    You can also do other really cool stuff with those like add arbitrary code to run before or after any ebuild phase. This is called ebuild phase hooks:

  2. sergiosdj says:

    Hey Donnie,

    Thanks for your comment. I appreciated your “improvement” over my post :-).


  3. otubo says:


    I never had the need to add some extra/diferent CFLAGS to specific packages. But when I do, I’ll know how.

    See ya on monday ;-)

  4. anselmolsm says:

    sergiosdj, what a nice surprise!

    I’ve just googled about it and the first result was this post =D


  5. mike says:

    sergiosdj, for some reason i am unable to touch files in /etc/portage/var…i wonder why…

  6. sujeet says:


    I was just wondering if it was possible to specify CFLAGS like this for a _particular_ version of a package. For example, I use -march=barcelona in my CFLAGS for everything, but, I do not want this setting when compiling gcc-3.4.6-r2 since this will break the install.

    Personally, I believe this is a defect in the gcc-3.4.6-r2 ebuild, as the temporary xgcc compiler inherits the CFLAGS from make.conf, but I nonetheless would like an appropriate work around. (Creating version-specific entry in /etc/portage/env would be ideal.)


    Sujeet Akula

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: