2

I have some code that requires a certain gcc compiler option (otherwise it won't compile). Of course, I can make sure in the makefile that for this particular source file the required option is set. However, it would much more helpful, if this option could be set for the respective compilation unit (or part of it) from within the source_file.cpp.

I know that warning messages can be switched on or off using #pragma GCC diagnostic, but what about the -fsomething type of options? I take it from this question that this is impossible.

But perhaps there is at least a way to check from within the code whether a certain -f option is on or not?

Note I'm not interested in finding the compiler flags from the binary, as was asked previously, nor from the command line.

Community
  • 1
  • 1
Walter
  • 44,150
  • 20
  • 113
  • 196

2 Answers2

1

You can try using some #pragma. See GCC diagnostic pragmas & GCC function specific pragmas.

Otherwise, develop your GCC plugin or your MELT extension and have it provide a pragma which sets the appropriate variables or compiler state inside GCC (actually cc1plus)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    I looked into the `#pragma` option (already before asking) but couldn't find anything useful. *develop your GCC plugin or your MELT extension* I rather avoid that hack. But thanks for pointing out the possibility. – Walter Sep 04 '14 at 08:17
1

In my experience, no. This is not the way you go about this. Instead, you put compiler/platform/OS specific code in your source, and wrap it with the appropriate ifdef statements. These include:

#ifdef __GNUC__
/*code for GNU C compiler */
#elif _MSC_VER
/*usually has the version number in _MSC_VER*/
/*code specific to MSVC compiler*/
#elif __BORLANDC__
/*code specific to borland compilers*/
#elif __MINGW32__
/*code specific to mingw compilers*/
#endif

Within this, you can have version-specific requirements and code:

#ifdef __GNUC__
#  include <features.h>
#  if __GNUC_PREREQ(4,0)
//      If  gcc_version >= 4.0
#  elif __GNUC_PREREQ(3,2)
//       If gcc_version >= 3.2
#  else
//       Else
#  endif
#else
//    If not gcc
#endif

From there, you have your makefile pass the appropriate compiler flags based on the compiler type, version, etc, and you're all set.

Cloud
  • 18,753
  • 15
  • 79
  • 153