c++ - 来自 CMake 的 MSVC 是否与 _MSC_VER 相同?

标签 c++ c cmake

我目前正在努力删除 CMake 作为库的依赖项,因为它似乎可以将大部分生成的代码设为静态。执行一些环境检查以查看我们使用的是哪种操作系统和编译器。 CMake 中的其中一项检查如下所示:

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 ()

可以用下面的 C 代码代替吗?

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

我最担心的是情况是否相同。我认为它们是,但我无法找到任何可以证实这一点的东西,而且我目前无法访问工作的 Windows 开发环境来构建它并找出答案。

感谢您提供的任何可能的帮助!

最佳答案

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. ...

由于 CMake MSVC 在使用 Microsoft Visual C++ 时为真,而 _MSC_VER 是 Microsoft 特定的 C++ 宏,因此在确定编译是否使用 Microsoft编译器。

但是,我不确定 #define 语句。 CMake set命令只是设置 CMake 变量了解如何在 CMake 脚本中使用这些变量会很有用

您拥有的可能就足够了,或者可能:

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

CMake add_definitions命令是添加预处理器定义的一种方法。

关于c++ - 来自 CMake 的 MSVC 是否与 _MSC_VER 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33001659/

相关文章:

c++ - Unordered_map 在访问元素时有奇怪的行为

带有指向自身的指针的 C 结构

cmake - 将外部源目录添加到 CMake 构建

python - Cython 编译正常,但找不到符号 : __ZNSs4_Rep20_S_empty_rep_storageE when running on Mac OS

c++ - 如何在 Windows 中获取指向硬件驱动程序的指针?

c++ - QWidget::grab 视频功能:无法捕捉

c - Linux 内核编程 : "Unable to handle kernel NULL pointer dereference"

c - 这个简单的 C 程序的输出

c++ - CMake 找不到 NVIDIA 的 opencl sdk

c++ - 无法为编译 SFML 项目创建 cmake 规则