Pass arguments to compiler to set defined variables? - c++

It is a possible to pass argument to compiler (command line) and set defined variables:
Example:
#define EXVALUE
and I want to define EXVALUE at compiling:
application.cpp -8
'-8' it is a command line argument to define EXVALUE. So I hope that You will understand
what I want, and will help me.
I use Visual Studio C++ 2008 Express Edition.
Thanks. (Sorry for english bads)

Visual Studio (so also Visual C++ EE) uses /D option.
Example:
/D "BOOST_ALL_STATIC_LINK"
You can do it by GUI : Project Properties->C/C++->Preprocessor->Preprocessor Definitions
First link in Google for visual studio preprocessor definitions has really nice information, if you need more.

Related

How to define preprocessed global macros in Visual Studio 2022

I want to have a global macro in my program (PI 3.14). I read that you have to go to preprocessor->preprocessor definitions->edit and from there you can add your macros. But how do you actually set what the macro is?
I've added the macro PI in the top left. It shows up in my program as equaling 1. How do I make it equal 3.14?
Please forgive me if this is a bad question, I'm a bit new to visual studio and the preprocessor in general.
Please refer to the link:/D (Preprocessor Definitions)
/D name is equivalent to /D name=1.
use the way 273K said or
in properties->C++->command line:
/D PI=3.14

Mangling and static/dynamic binding in visual studo 2019

For C++ class can I see the intermediate compilation stage/s? : variable/methods mangling?
some readable representation of generated code for static binding ? vtable?
for students
VS ILDasm is a tool that can decompile code. You could add ILDasm in VS.
Here are the steps:
Select Tools->External Tools->ILDasm
Select the path of ILDasm.exe in Command. The path is generally C:\Program Files\Microsoft SDKs\Windows\vXXX\bin\NETFX 4.0 Tools\ildasm.exe.
Type $(TargetPath) /text/item: in Arguments, then click Apply

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...

How to know where a certain macro is defined in Visual Studio

I currently have a visual studio project that uses DirectX. There are some macros in the code such as
__range(0, m_lBatchSize) LONG m_nBatched;
__field_ecount_opt(m_lBatchSize) IMediaSample ** m_ppSamples;
I wanted to know which files these macros are defined in. Normally in Visual Studio I would click goto definition and it would take me to the definitio. It is not taking me anywhere in this case. Does that feature work for macros ? Is there any way for me to find out where this macro is defined ?
my trick is to put
#define __range FOO
in my code, the preprocessor will then say
__range already defined at xxxx.nn

Define macro "disallow copy and assign" in CMake for Visual Studio 2010

An existing C++ multi project uses CMake 2.8 in QT so far. We want to continue maintaining it in Visual Studio 2010. I generated Visual Studio projects in CMake with option -G "Visual Studio 10" but now I fail to compile them for the following reason:
In the project we use a well known macro which itself is discussed for example in this question.
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
The macro is defined in CMake in order to provide it to the compiler (cl.exe) as a preprocessor definition:
add_definitions(-DDISALLOW_COPY_AND_ASSIGN\(TypeName\)=\" TypeName(const TypeName&)\; void operator=(const TypeName&)\; \")
Visual Studio does not accept CMake's output and throws compilation errors wherever the macro is used in the code. What would be the proper syntax so that CMake can properly generate it for Visual Studio 2010?
It's not possible to define function-style macros on the command line of cl. You could get around this by having the macro definition in a header file and passing this header file using cl's command-line option /FI. Or just include it manually where necessary (which is probably cleaner).
I would recommend that you don't use macros in general, and for this case in particular. If you can use boost, you can privately inherit from boost::noncopyable. If you don't, you can define your own:
class noncopyable {
noncopyable(noncopyable const &);
void operator=(noncopyable const&);
protected:
noncopyable();
};
class Use : noncopyable
{
...
If you insist on using a macro, read the compiler documentation as of the flags that are needed to dump the preprocessed code into a file and see what the macro expanded to. From there you can try to figure out what went wrong.