c++ - VS2015 提示 printf 没有内联

标签 c++ visual-studio-2015 printf

我正在使用 VS2015 和/Wall 编译一个 C++ 文件。 我正在使用 printf 函数。

我得到:

warning C4710: 'int printf(const char *const ,...)': function not inlined

我了解内联。我在 MS 环境中看到了有关 __forceinline 的帮助。我只是不明白为什么 VS2015 没有通过 stdio.h 正确使用它并且仍然生成警告。

我的stdio.h 文件在这里:

C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt

有什么办法可以解决这个问题吗?

编辑:

好的示例代码如下:

int main(int argc, char** argv){
  printf("Hello World\n");
  return 0;
};

至于我是想用C还是C++,我目前是用Windows开发的,但是文件最终会在HPUX上编译成C。如果这是警告的原因,我也想知道。

最佳答案

编译器尝试内联一些函数(在本例中为 printf() 以实现性能或磁盘空间,如 here in the documentation 所述。但是,您可以通过以下方式禁用这些警告:

#ifdef _WIN32
#pragma warning (disable : 4710)
#endif

或在 CMake 中:

if (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
  set(CMAKE_C_FLAGS "/wd4710") 
endif()

编辑:

在现代惯用的 CMake 中,最好避免直接编辑 CMAKE_C_FLAGS 变量。

您可以改为执行以下操作。

add_compile_options(
  $<$<C_COMPILER_ID:MSVC>:/wd4710>
)

或者您甚至可以只针对单个目标禁用警告

target_compile_options(YourTargetName
  $<$<C_COMPILER_ID:MSVC>:/wd4710>
)

关于c++ - VS2015 提示 printf 没有内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49245647/

相关文章:

typescript - ASP.NET 核心项目 : How to keep Typescript from Compiling

visual-studio-2015 - 使用 Microsoft.Build (Visual Studio SDK) 编译 C# 6 时出错

css - Angular Material 建议使用 Visual Studio 社区版 2015 的 md- 标签

c - if (id==1) printf ("%i", id) 打印一些随机值

strstr() 中的着色匹配子字符串

c++ - 根据条件和优先级搜索容器中的项目

c++ - 为什么枚举会隐式转换为 std::string?

python - 如何使用 SWIG 在 Python 中将 operator<< 包装到 __str__?

c - c中printf()的返回类型

c++ - 在 C++ 中同时执行多个函数