1

When invoking GCC, one provides an option in the form of "-mcpu=cortex-m4" to specify the CPU type.

It there a way at run time to get the -m CPU string that GCC was invoked with??

Thanks

user928108
  • 21
  • 1
  • 2
  • 3

2 Answers2

1

You can achieve this with some clever compiling. If, for example, you use a Makefile you can put your flags into a variable and then use that variable to set a macro using the GCC -D flag a bit like this:

COMPILER_FLAGS=-std=c++11 -thread -O3

program: program.cpp
    $(CXX) -DEXTERNAL_COMPILER_FLAGS="\"$(COMPILER_FLAGS)\"" $(COMPILER_FLAGS) -o $@ $< 

Then in the C++ source file you can have:

#include <iostream>

#ifndef EXTERNAL_COMPILER_FLAGS
#define COMPILER_FLAGS "not set"
#else
#define COMPILER_FLAGS EXTERNAL_COMPILER_FLAGS
#endif

int main()
{
    std::cout << COMPILER_FLAGS << '\n';
}

If you don't use a Makefile you should be able to find an equivalent method in whatever build system you use.

Galik
  • 44,976
  • 4
  • 80
  • 109
  • Why not simply use `-frecord-gcc-switches`? – Jesper Juhl Jul 12 '20 at 15:10
  • @JesperJuhl Well the GCC documentation says it is not available on all architectures and that the format depends on the binary target. Also I could not find how to extract the information from within the source file. – Galik Jul 12 '20 at 15:12
0

GCC has the -frecord-gcc-switches option for that. It will cause your binary to record/contain the compiler options used, in a special section that can then be extracted later.

Without that option, there's no way to know.

Look up details at https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html

Jesper Juhl
  • 28,933
  • 3
  • 44
  • 66
  • It takes some extra work to access this from within the program itself, however, does it not? You'll either need to find the binary and parse its system-specific format, or else play some linker games to get a usable symbol pointing to the recorded switches. – Nate Eldredge Jul 12 '20 at 15:11
  • @NateEldredge Sure, you need to do a bit of work to extract the information, but it's not rocket science. – Jesper Juhl Jul 12 '20 at 15:13
  • I dunno. Finding an executable at runtime is not so easy to do portably and reliably, and you'll have to bring in libelf or something to parse it. And as for linker games, does this section even get loaded into memory with the rest of the program? – Nate Eldredge Jul 12 '20 at 15:30
  • To answer my own question, no it doesn't. So that approach really would require some linker scripting: to set the section to be loaded, and to provide symbols pointing to its start and end so that it can be traversed. That could be a question of its own, I suppose. – Nate Eldredge Jul 12 '20 at 15:40
  • 1
    Another problem with this is that it appears that the linker will merge duplicate strings in this section, so that if you have different source files compiled with different options, you cannot sort out which was which. There may again be ways to prevent the linker from doing this, but I feel like it's rapidly approaching "rocket science". – Nate Eldredge Jul 12 '20 at 15:57