How to define preprocessed global macros in Visual Studio 2022 - c++

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

Related

Visual Studio throws "undeclared identifier" from predefined string macro [duplicate]

This question already has answers here:
How to make a string preprocessor definition from command-line in VC 2005 (C++)?
(5 answers)
Closed 6 months ago.
I have the following preprocessed macro defined under C/C++ -> Command Line -> Additional Options:
/D NV_WORKING_DIRECTORY="C:/foo"
An image from the project settings:
I have the following code:
std::string path = NV_WORKING_DIRECTORY;
When I compile I get this error:
'C': undeclared identifier.
Weirdly, with certain strings the code works. For example if I do /D NV_WORKING_DIRECTORY="foo" everything is good. But "garbage" for some reason gives the same error.
Hardcoding#define NV_WORKING_DIRECTORY "C:/foo/" makes the code above work, but because I need a global macro that is not an option.
Is there something I am doing wrong here? This is a brand new project, and I have not messed around with anything yet.
When you use /D in the C/C++ -> Command Line -> Additional Options, the value is expected to be wrapped in a string.
E.g. the following means the value of XXX will be YYY:
/D XXX="YYY"
Therefore if you need the value of the preprocessor definition to contain the quotes, you need to add and escape them:
/D NV_WORKING_DIRECTORY="\"C:/foo/\""
Alternatively you can add the NV_WORKING_DIRECTORY defintion under C/C++ -> Preprocessor Definitions.
There you can simply put the string value that you need:
NV_WORKING_DIRECTORY="C:/foo/"

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

Precedence of -D MACRO and #define MACRO 5

Hello everybody I'm programming on Visual C++ 6.0 IDE my problem is: I tried to define macros from the command line at first I did this: project->settings c++ command definitions and i entered this macro: -DHELLO="HELLO!" when I use it from my source code I entered:
#ifdef HELLO
HELLO;
#endif
Until this everything is OK.
But my problem is with macros those takes arguments, so how I set a macro with arguments and the second question is how to expand it from source code?
Any help is really appreciated. I spent a lot of time googling and searching, reading ebooks but this didn't help.
It seems that it is not possible...
If you take a look at the Microsoft documentation, the /D option is constructed like :
/Dname[= | # [{string | number}] ]
As it seems to be impossible to add parantheses, I don't it is possible to create function-like macro with this command line option...
NB: It's weird that I tried on Visual Studio, my intellisense see it as function-like macro, so no error visible in the code (no red line under it), but when it's time to compile I get :
error C3861: 'MACRO_TEST': identifier not found
With a definition of type :
/D"MACRO_TEST( tst )= tst" // or -D"MACRO_TEST( tst )= tst"

Pass arguments to compiler to set defined variables?

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.

Visual C++ 2010 solution-wide macros with parameters

I'm trying to compile some source code with Visual C++ 2010 Express. The code was written for GCC, and contains attributes like this:
struct something {
...
} __attribute__((packed));
Since this is not standard C++ syntax, Visual C++ doesn't recognize it. With this macro prior to the struct declaration, it works fine:
#define __attribute__(p)
But I don't want to alter the files. I created a new property sheet (GccCompat), and went to Preprocessor Definitions, and added the macro, like this:
__attribute__(p)
or like this:
__attribute__(p)=
But it doesn't work. It's simply not called. If I define just __attribute__ (without parameters) in the same location, the macro is correctly defined. Note that the command line that is generated looks fine (the macros with parameters are passed exactly the same as the ones without), but the compiler seems to ignore it. So, how can I globally define my macro with a parameter?
It might be impossible, at least that way. Notice that Microsoft's documentation of the /D option doesn't specify a syntax for macros that take arguments.
Defining macros in the IDE is generally focused on the nature of creating a /D:CPP_TOKEN flag for the compiler, i.e. #define CPP_TOKEN.
In the advanced compiler settings you may be able to define such a macro as /D:"attributes(p)=/nothing/" or something like that. Just open the VS command prompt and see what it says. GCC 4.2 will allow something like that (using it's -D switch), but I don't have MSVC10 handy.