Multithreaded libs and compiler settings through pragma comments

I noticed you can adjust compiler and linker settings in the code using pragma comments rather than having to mess about with the IDE like going to

Project  -> <Project Name> Properties  -> Configuration Properties  -> C/C++  -> Code Generation  -> Runtime Library.

I had a little problem first trying to figure out how to simulate /MTd though till I found out it was done by defining _DEBUG and _MT together.

But now what I am wondering is, is it okay to do something like this below so that the compiler automatically compiles the right code for me, depending on if it is a debug build or a release build.

 

-------------------------

#ifndef _MT
#define _MT
#ifdef WIN32
#pragma comment(lib, "libcmt.lib")
#endif
#ifdef _DEBUG
#pragma comment(lib, "libcmtd.lib")
#endif
#endif

-------------------------

Or perhaps this here

-------------------------

#ifdef WIN32
#ifndef _MT
#define _MT
#pragma comment(lib, "libcmt.lib")
#endif
#endif
#ifdef _DEBUG
#ifndef _MT
#define _MT
#pragma comment(lib, "libcmtd.lib")
#endif
#endif

-------------------------

Is that okay to do Providing process.h is included at the top of course.



Answer this question

Multithreaded libs and compiler settings through pragma comments

  • SunilK007

    Okay thank you :)
  • drinkingredvodka

    Yep, if you put it into a file like stdafx.h, it should be alright. But in future, if someone adds a new build configuration (other than the normal Debug and Release configs), then you'd have to take care of that too in your include file.

    _John_ wrote:
    What about if I put this code into a header file, and then included it at the very top of all .cpp files I use in the project



  • Lukan

    What about if I put this code into a header file, and then included it at the very top of all .cpp files I use in the project
  • P.D.

    A problem in doing this is that, any new cpp file added to your project will have to include this header. If you forget to do so, you'll run into trouble.

  • Multithreaded libs and compiler settings through pragma comments