简体   繁体   中英

Is MSVC from CMake the same as _MSC_VER?

I'm currently working on removing CMake as a dependency for a library since it seems like most of the generated code could be made static. There are some environmental checks that are performed to see what kind of operating system and compiler we're using. One of those checks in CMake looks like the following:

if (MSVC)
    set (HAVE_SYS_TYPES_H 1)
    set (HAVE_STDINT_H    1)
    set (HAVE_STDDEF_H    1)
    # snip more things
else ()
    # snip more things
endif ()

Could this be replaced with the following C code?

#ifdef _MSC_VER
    #define HAVE_SYS_TYPES_H
    #define HAVE_STDINT_H
    #define HAVE_STDDEF_H
    // snip more things
#else
    // snip more things
#endif

I'm mostly worried about the condition being the same. I think they are but I haven't been able to find anything that would confirm that and I don't currently have access to a working Windows dev environment to just build it and find out.

Thank you for any possible assistance!

CMake 3.4 MSVC Documentation :

True when using Microsoft Visual C++.

Set to true when the compiler is some version of Microsoft Visual C++.

MSDN Predefined Macros Documentation :

Lists the predefined ANSI/ISO C99 and Microsoft C++ implementation preprocessor macros.

...

Microsoft-Specific Predefined Macros

_MSC_VER Evaluates to an integer literal that encodes the major and minor number components of the compiler's version number. ...

Since CMake MSVC is true when the Microsoft Visual C++ is used and _MSC_VER is a Microsoft-specific C++ macro these are interchangeable for determining if compilation is performed using the Microsoft compiler.

However, I'm not sure about the #define statements. The CMake set command is just setting CMake variables it would be useful to know how these variables are being used in the CMake script .

What you have may be sufficient or perhaps:

#define HAVE_SYS_TYPES_H 1
#define HAVE_STDINT_H 1
#define HAVE_STDDEF_H 1

The CMake add_definitions command is one way to add preprocessor definitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM