0

Using GCC to compile C++ code: there's a big list of options (warnings, optimisations etc) I want to always compile with. It's tiresome to keep pasting them into the command line. Can I do something like

g++ test.cpp -o test.o -myoptions

where "-myoptions" would enable a list of options like

-pedantic
-Wall
-O2
-Wshadow
-...

that I've defined somewhere?

I've tried searching but couldn't find a guide for this. Is it possible or do I have to use make files? System is windows 7 if that's relevant. Thanks


edit: in case someone searches for this in the future, windows specific answer: go to control panel -> system -> advanced system settings -> environment variables, create new system variable named "myoptions" and value "-option1 -option2 -option3 ...". Then compile with "g++ test.cpp -o test.o %myoptions%"

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
badger5000
  • 650
  • 7
  • 17

1 Answers1

2

You can set an environment variable:

export CXXFLAGS="-Wall -O3 -std=c++11"

and use

g++ $CXXFLAGS test.cc -o test

Note that some variables are more-or-less standardized and when you set them, several build system will automatically use them. Among others, google for CFLAGS, CPPFLAGS, CXXFLAGS, ...

Here's also a starting point on SO.

Community
  • 1
  • 1
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 1
    Assuming a Unix-y OS, or Cygwin on the Windows OP is using of course. – fvu Oct 15 '13 at 10:09
  • Thanks, environment variable route works great! Though, windows doesn't find CXXFLAGS, so edited in a windows specific answer – badger5000 Oct 15 '13 at 10:47
  • @fvu The usual Windows shells also support environment variables, albeit with a different syntax (`SET CXXFLAGS=...` to set, `g++ %CXXFLAGS% ...` to use). – James Kanze Oct 15 '13 at 11:42