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

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/"

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

Define macro from a file using cmake and use it later on the code

How can I define a macro and its value from a file, using cmake, and then use it later on the code in the proper way?. Here what i have done:
CmakeList.txt
file(STRINGS server.txt _SERVERS)
add_definitions(-DSERVERS=${_SERVERS})
File server.txt
http://192.168.1.150:8080
After running cmake i find in the command line the macro:
/D "SERVERS=http://192.168.1.150:8080"
But when i put in the code main.cpp
QString test = QString(SERVERS);
Visual studio marke it as error and it give me the following error, once I pass mouse over the variable:
Error: Identifier "http://192.168.1.150:8080" is undefined
Any idea how to proceed?
Error is in the file
The value must be using quotes like
"http://192.168.1.150:8080"
sorry for the question

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"

How-to find issues concerning includes - visualize inclusion tree [duplicate]

This question already has answers here:
Displaying the #include hierarchy for a C++ file in Visual Studio
(9 answers)
Closed 9 years ago.
Scenario
From time to time I get compiler errors in system headers. Currently for example:
c:\Programme\Microsoft Visual Studio 9.0\VC\ce\include\streambuf(55) : error C2143: syntax error : missing ')' before 'string'
As always, this is a false error message and of course there is a problem in my code. However, the compiler is not capable to tell me where it hurts. So I was looking for the usual suspects, e.g.
"using namespace XXX" in some header files or something like that.
My problem is, I do not even know via which way the file streambuf was included into my code. At least I do not include it directly.
Concrete question
Is there a way to get some kind of "inclusion tree"?
Something like
myClass.cpp
+ myClass.h
+ ios
...
+ streambuf
so I would be able to determine the error location a bit better.
I typically use
cl /c /P /d1PP file.cpp
This creates a file.i. This is the preprocessed file - it contains all the headers as processed i.e. if a particular part of the header is under ifdef something & you haven't defined that something, it will not contain that block. The /d1PP (undocumented, I think) also show you where the macros are actually defined. You also will see who included streambuf in your code and at what point.
I then compile file.i as
cl /c /Tp file.i (or cl /c /Tc file.i - if it's C and not C++)
For visualisation, try this - http://www.codeproject.com/Articles/3478/Include-File-Hierarchy-Viewer

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.