1

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.

DrYap
  • 6,525
  • 2
  • 31
  • 54
ProDev7
  • 75
  • 7
  • 3
    Your title mentions precedence while your body has nothing to do with precedence. You need to be a bit more in focus on things. – mah Aug 12 '13 at 11:40
  • I didn't understand. could you explain it a bit more? – ProDev7 Aug 17 '13 at 22:48

1 Answers1

1

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"
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • Thnk you very much for your help that enlightened me after being so tired after a long research. so you mean it is not possible to define a function-like macro from command line?. what you said about the macro definition from preprocessor definitions seems to me completely correct – ProDev7 Aug 17 '13 at 22:42
  • @ProDev7 After the tests I did and the searches on the internet, I am pretty sure that it is not possible for now with any version of Visual Studio. Maybe you should define your *function-like* MACRO inside a Header file that you include only inside the projects you want it to be included ? – Pierre Fourgeaud Aug 17 '13 at 23:00
  • Thank you too much Pierre. in fact I was hopeless to find a convincing reply even on MSDN. until I got you reoly. – ProDev7 Aug 17 '13 at 23:03
  • But Pierre what is the point to define a function-like macro in a header file? – ProDev7 Aug 17 '13 at 23:17
  • @ProDev7 You have a great answer [here](http://stackoverflow.com/questions/1616802/when-to-use-function-like-macros-in-c). After, the fact that the macro is defined inside your command line or inside an header file is of your taste I think. Personally I prefer inside the Header because it is more obvious for developers (I think, but it is only my opinion here :) ) – Pierre Fourgeaud Aug 17 '13 at 23:20
  • ok Pierre. I'm new hewre could you show me how to validate the answer? – ProDev7 Aug 17 '13 at 23:31
  • @ProDev7 Also, if you search for *function-like macro* around the web, you will see that everyone use `#define` in a header instead of the command line argument ;) – Pierre Fourgeaud Aug 17 '13 at 23:36
  • ok. thank you. and now I got it that it is not possible to define function-like macros on command line. – ProDev7 Aug 17 '13 at 23:40
  • Pierre I want to validate your answers but I don't see any checkbox. could you explain more? – ProDev7 Aug 17 '13 at 23:49
  • Pierre I finally found the arrows you told me about. but when I click on it it says to me that "Vote up requires 15 reputations" – ProDev7 Aug 17 '13 at 23:59
  • if i got enough reputations I promise you to vote up on all your answers. – ProDev7 Aug 18 '13 at 00:00
  • Pierre coud you answer this question: – ProDev7 Aug 21 '13 at 15:10
  • Hello everybody I am still confused with defining degug macros;I want some explanation why do we use #define DEBUG, #define NDEBUG #define _DEBUG could you explain it to me showing the point in using them? I hear we use them to remove some debugging code whne compilation?? als if use in program: #define _DEBUG I got warning telling me Macro _DEBUG redifibition. what about turning on and off Debug????? I do recognize your help and making it cleared up to me. – ProDev7 Aug 21 '13 at 15:11
  • @ProDev7 `#define DEBUG` and `#define _DEBUG` are defined by Visual Studio when you are compiling in debug mode. It permits to you to have some code only in debug (for debugging purpose). Look : http://ideone.com/NS0FXD. – Pierre Fourgeaud Aug 21 '13 at 17:56
  • I paid a visit to the link and I found the example. but I still don't know the real reason people use #define DEBUG or _DEBUG. please explain more in details as you did a lot of efforts for me with the command line macros. thank you for your time and effort – ProDev7 Aug 21 '13 at 21:25