Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining macros in Visual Studio - /D or #define?

Recently, when porting some STL code to VS2008 I wanted to disable warnings generated by std::copy by defining the new _SCL_SECURE_NO_WARNINGS flag. You can do this in two ways:

  • Using the /D compiler switch, which can be specified in the project properties. You need to ensure it is defined for both Release and Debug builds, which I often forget to do.
  • By defining it macro style before you include the relevant STL headers, or, for total coverage, in stdafx.h:

    #define _SCL_SECURE_NO_WARNINGS

Both of these methods work fine but I wondered if there was any argument for favouring one over the other?

like image 338
Rob Avatar asked Nov 30 '22 13:11

Rob


2 Answers

The /D option is generally used when you want to define it differently on different builds (so it can be changed in the makefile)

If you will "always" want it set the same way, use #define.

like image 77
James Curran Avatar answered Dec 05 '22 05:12

James Curran


By putting them in your project file you maintain a close association between the platform specific warnings and the platform, which seems correct to me.

If they're in the code, they're always in the code whether or not it's appropriate for the platform. You don't need it for GCC or possibly future versions of Visual C++. On the other hand, by having it in the code, it's more obvious that it's there at all. If you move (copy) the code, it'll be easier to remember to move that define with it.

Pros and Cons each way. YMMV.

like image 26
luke Avatar answered Dec 05 '22 07:12

luke