4

Is it possible to use GCC to compile one section of a code file with specific compiler flags? For example, suppose I had some functions I was testing. I want those functions to strictly adhere to standards compliance, so I want to compile them with the --pedantic flag. But the code to do the testing issues a lot of warnings on compilation. Is there any way to compile just those specific functions with --pedantic?

Alternatively, suppose I have a carefully written but extremely expensive function that needs to run as fast as possible. How could I compile just that function (and a few others) with -Ofast, and compile the rest of the program with -O2 or -O3?

Alecto Irene Perez
  • 10,321
  • 23
  • 46
  • 2
    Put those functions in separate files and build them with separate flags - would be the simple solution. "the code to do the testing issues a lot of warnings" - so fix them already - don't wait or make excuses - *fix* compiler warnings (and start building with `-Werror`). – Jesper Juhl Jul 07 '17 at 19:48
  • Possible duplicate of [GCC - Enable compiler flags only on specific functions](https://stackoverflow.com/questions/12925989/gcc-enable-compiler-flags-only-on-specific-functions) – Pyves Jul 08 '17 at 07:09

3 Answers3

8

There is, actually, using #pragma optimize statements, or using __attribute__((optimize("-O3"))) All the optimize options can be found here.

A simple example would be:

#include <stdio.h>


// Using attribute
__attribute__((optimize("-O3"))) void fast_function_attribute()
{
    printf("Now calling a slow function, compiled with -O3 flags.\n");
}


__attribute__((optimize("-O1"))) void slow_function_attribute()
{
    printf("Now calling a slow function, compiled with -O1 flags.\n");
}


// Using #pragma
#pragma GCC push_options
#pragma GCC optimize ("-O3")


void fast_function_pragma()
{
    printf("This will be another fast routine.\n");
}


#pragma GCC pop_options


#pragma GCC push_options
#pragma GCC optimize ("-O1")


void slow_function_pragma()
{
    printf("This will be another slow routine.\n");
}


#pragma GCC pop_options


int main(void)
{
    fast_function_attribute();
    slow_function_attribute();
    fast_function_pragma();
    slow_function_pragma();
}

If you are using different compilers, I would highly recommend wrapping them with macros (or using pragma statements rather than __attribute__ to avoid any compiler warnings.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
2

Yes.

#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wpedantic"
#pragma GCC push_options
#pragma GCC optimize ("O0")
void bob(){
  std::cerr<<"Sandwich maker!"<<std::endl;
}
#pragma GCC diagnostic pop
#pragma GCC pop_options

int main(){
  bob();
}

The push_options and diagnostic push saves the optimization and diagnostic flags, respectively, prior to altering those to pedantic and O0 (or O1, O2, O3, Os, Og). The pop pragmas restore the original settings.

More details on optimization pragmas are here and details on diagnostic pragmas are here.

Richard
  • 56,349
  • 34
  • 180
  • 251
0

GCC allows this in some cases, but not all.

For optimization options, you can use the optimize function attribute or equivalent #pragma directive.

For warning options, it’s less reliable. I think there’s only a pragma for it and not a function attribute, and it apparently doesn’t support all warnings.

For options other than warnings or optimizations, I don’t know of any way to do this other than using separate files.

Daniel H
  • 7,223
  • 2
  • 26
  • 41