Precedence of -D MACRO and #define MACRO 5 - c++

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"

Related

Macro redefinition problem in C++ header file

When I try to run the following header
https://github.com/marmalade/glib/blob/master/glibconfig.h
in Microsoft Visual Studio, I get the following error: 'G_CAN_INLINE': macro redefinition. What is the reason for this? Any help is appreciated. Thanks.
There's actually a trick for this which might work for you. What you can do is this:
Comment out the #define(s) for G_CAN_INLINE in glibconfig.h (make a copy first!).
After the relevant #include, add (temporarily) the following line to the source file that is generating the compiler error:
int any_old_variable_name = G_CAN_INLINE;
Give Intellisense a moment to catch up.
Right-click on G_CAN_INLINE and select 'Go To Definition'.
With a bit of luck, VS will then show you where that macro is (also) being defined and that should help you figure out what to do.
Having said which, G_CAN_INLINE doesn't look to be all that important. You will probably find that you can just #undef it before you include glibconfig.

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.

MSVC 2010 project-wide macro with parameters

I'm trying to create a Visual Studio project for code that contains
DL_EXPORT(void) initlua(void);
So I basically need a macro like
#define DL_EXPORT(retVal) __declspec(dllexport) retVal
Which works, but is OS/Compiler-specific, so I want to put that in the project*. But I can't figure out what to put in Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions (or in the Command Line) to do that. I'd think either of these would work:
DL_EXPORT(retVal) __declspec(dllexport) retVal
DL_EXPORT(retVal)=__declspec(dllexport) retVal
I'm leaning towards the latter, but neither seems to work - when compiling I get these errors:
error C2061: syntax error : identifier 'initlua'
error C2059: syntax error : ';'
error C2059: syntax error : 'type'
And compiling with /P to get the preprocessor result explains why: Nothing happened, so the compiler interpreted it as int DL_EXPORT(void) and expects a ;.
What is the right syntax for the definition? Or is there none, as people in this question assumed?
Thanks.
* I'm not using a simple #ifdef-check for MSVC because I'm just trying to create a Visual Studio project for an existing library (lunatic python) with existing build scripts that I don't want to break. Although I could admittedly use #ifndef DL_EXPORT - but I'd still like to know if I'm missing something or if this is just impossible to do in Visual Studio.
I think it should be possible to use /FI on the commandline to specify an include file to include automatically in every source file. In that file you put the #define statements you need.

c++ pragma #error giving an error

I recently came across a piece of code which uses
the pragma directive
#error Error ! Define [_HOS_ | _HOS_OV_ | _HOV_].
This code is from the ADOC-C jacobian computation routine.
The problem is that in C++ visual studio 2010 there is a curly red line below the #error indicating there is some problem within that line.
The code also fails to compile giving error message at that line
Is there a syntax issue or are those [_HOS_ | _HOS_OV_ | _HOV_] not defined at that point, and intelliSense knows that resulting in curly red line indicating some problem?
The author of that code intended to have an error when not at least one of the tags named in the error message is defined. You should have a look at the documentation of the ADOC-C stuff if s.th. is mentioned there about these tags.
The intent of the #error directive is to create compile errors. It's usually a way for the programmer who wrote the code to tell the programmer that's trying to compile and use it "You did something wrong, this won't work!". The string following the directive is the message that should be shown to the programmer trying to compile the code.
You should check what directives are around this one, for example are there any #ifdefs that cause it to be executed. Then you should lookup the conditions in which they executed (e.g. not defining the things the error lists) and look for a way to make them go away.
The problem is that in C++ visual studio 2010 there is a curly red line below the #error indicating there is some problem within that line.
Visual Studio 2010 does not recognize #error preprocesor syntax anymore. It only recognize #pragma warning

Unexpected in macro formal parameter list Error

I'm an intern student and my boss told me to do porting from Linux c to Visual C++.
When I built the coding, I found this error "unexpected in macro formal parameter list", here is the code
#define cache_info(format, msg...)
do { \
;\
} while (0)
I don't know what is wrong and what the coding is for .
I can't also ask the Linux programmer since he is out. Can someone help me ???
Sounds like your version of Visual C++ doesn't support variadic macros.
you might need to try something like this to get it to work.
#define FUNC(foo) ThisFunc foo
void ThisFunc(int, ...);
int main()
{
FUNC((123, 456));
}
or you could just be missing a comma?....
#define cache_info(format, msg,...)
I think that the problem could be from one of two things.
First, your macro is defined as
cache_info(format, msg...)
But you probably meant to write
cache_info(format, msg, ...)
Though this could just be a typo in your original post.
More importantly, though, macros with variable numbers of arguments ("variadic macros") are not supported in C++; they exist only in C. If you're trying to compile this C code with a C++ compiler, the compiler should give you an error here because the code isn't legal C++.
if using Windows 64 bit OS, & visual studio, try after running this bat file : \Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat
It will register env settings. It worked for me..