这是我第一次使用 Visual Studio (2010) 进行开发,我注意到:
int *x = malloc(sizeof(int) * 50);
当我尝试构建“main.c”时出现错误。我假设这是因为 VS 使用其 C++ 编译器来构建 C 程序。
VS 为 C 生成的代码是否与 Windows 下一样好,还是我应该寻找更好的和/或 C 独有的代码?
最佳答案
I'm assuming this is because VS is using its C++ compiler to build C programs.
您的假设是不正确的:该行代码没有任何问题,事实上 Visual C++ 会毫无错误地编译它:
c:\dev>type stubby.c
#include <stdlib.h>
int main(void)
{
int *x = malloc(sizeof(int) * 50);
}
c:\dev>cl stubby.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
stubby.c
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:stubby.exe
stubby.obj
c:\dev>
Visual C++ 是否适合您的需求取决于您的需求是什么。不支持C99;如果对 C99 的支持对您来说很重要,那么您将需要寻找另一个编译器。它不支持过多的 GNU 语言扩展;如果这些对您很重要,您会想要找到另一个编译器。
关于c++ - 使用 Visual Studio C++ 编译器编译 C 代码是否有任何(与性能相关的)缺点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5590459/