简体   繁体   中英

Embed a string of compiler options in an executable (Visual Studio)

Using gcc and Makefiles, I am able to embed some compiler options into a string for an executable to access as follows:

Makefile:

CXX=/usr/bin/g++
CXXFLAGS=-ansi -Wall -c
CXXDEFINES='-DCXXCOMMAND="$(CXX) $(CXXFLAGS)"'

C++ file:

#ifdef CXXCOMMAND
    std::cout << "C++ Command: " << CXXCOMMAND << std::endl;
#endif

And then I am able to print it to the console from the executable.

Is it possible to do this in Visual Studio? Specifically I am looking to put in the optimization flags (/O2, /Od, etc...) , but the more I have access to, the better things will work for me.

Microsoft Visual Studio Professional 2012
Intel C++ Composer XE 2013 SP1

Yes, sort of. You can define a macro in the project properties that contains a string, and embed within it any project property values you want. However, these properties, while used to construct the command line, are not the actual command line flags themselves.

For example, add the following as a Preprocessor Definition in the C/C++ Preprocessor project properties:

CXXCOMMAND="%(Optimization)"

Then CXXCOMMAND is "MinSpace" for a build set to compile with /O1.

Just be careful to have only an even number of quotes in your preprocessor define...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM