C语言 如何使用 Visual Studio 2010 进行 C 开发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3327199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:59:57  来源:igfitidea点击:

How can I use Visual Studio 2010 for C development?

cvisual-studio-2010development-environmentcompilation

提问by Jonas

I would like to do some C development in Windows environment using Visual Studio 2010. There are a few similar questions on this topic, but they are all based on that you are creating a Win32 console application, and a C++ project.

我想使用 Visual Studio 2010 在 Windows 环境中进行一些 C 开发。关于这个主题有一些类似的问题,但它们都是基于您正在创建一个 Win32 控制台应用程序和一个 C++ 项目。

How can I do C development using only .cand .hfiles as I do in Unix? without creating a C++ projects containing tons of files.

如何像在 Unix 中那样仅使用.c.h文件进行 C 开发?无需创建包含大量文件的 C++ 项目。

It is possible to do C compiling with the clcompiler from outside of Visual Studio 2010, see Walkthrough: Compiling a C Program. But how can I do this compilation and execution/debugging from inside Visual Studio 2010?

可以cl从 Visual Studio 2010 外部使用编译器进行 C 编译,请参阅演练:编译 C 程序。但是如何从 Visual Studio 2010 内部进行编译和执行/调试?

UPDATE

更新

  • I have tried to create a C++ project (Win32 Console Application) and only add .cfiles to it. It works but it creates tons of files.
  • I have tried with a C++ project (Empty project), but it also created a lot of project files.
  • Basically what I want is to only create .cand .hfiles, use the clcompiler, and use Visual Studio 2010 as a text editor. And use a command to compile from the text edior, but it seems like I have to compile in a command prompt.
  • 我试图创建一个 C++ 项目(Win32 控制台应用程序)并且只.c向其中添加文件。它可以工作,但会创建大量文件。
  • 我尝试过一个 C++ 项目(空项目),但它也创建了很多项目文件。
  • 基本上我想要的是只创建.c.h文件,使用cl编译器,并使用 Visual Studio 2010 作为文本编辑器。并使用命令从文本编辑器进行编译,但似乎我必须在命令提示符下进行编译。

采纳答案by Puppy

If you compile a file that has the .c extension, VS will use it's C compiler. However, you should be aware that said C compiler isn't C99 conformant (or even C89 for some cases, if I remember correctly). Visual Studio is not really a C compiler, it's C++ mostly. You will have to use a C++ project and simply include .c files.

如果编译具有 .c 扩展名的文件,VS 将使用它的 C 编译器。但是,您应该知道,所说的 C 编译器不符合 C99(如果我没记错的话,在某些情况下甚至不符合 C89)。Visual Studio 并不是真正的 C 编译器,主要是 C++。您将不得不使用 C++ 项目并简单地包含 .c 文件。

回答by Sam Harwell

  1. File → New → Project...
  2. Under C++, choose Empty Project. If you want to minimize the number of folders created, uncheck the box to Create Directory for Solution. Give the project a name and location and click OK.
  3. In the Solution Explorer for the new project, right click Source Files and select Add → New Item.
  4. Choose C++ File (.cpp), and give it a name like SomeName.c. Make sure to specify the .cextension.Add the following code:

    int main(int argc, char** argv)
    {
        return 0;
    }
    
  5. If necessary, disable Microsoft extensions to the C language by right clicking the project and selecting Properties. Select All Configurations at the top of the dialog. Then go to C/C++ → Language → Disable Language Extensions: Yes.

  1. 文件→新建→项目...
  2. 在 C++ 下,选择空项目。如果要尽量减少创建的文件夹数量,请取消选中“为解决方案创建目录”框。为项目指定名称和位置,然后单击“确定”。
  3. 在新项目的解决方案资源管理器中,右键单击源文件并选择添加 → 新项目。
  4. 选择 C++ 文件 (.cpp),并给它起一个类似 .cpp 的名称SomeName.c确保指定.c扩展名。添加以下代码:

    int main(int argc, char** argv)
    {
        return 0;
    }
    
  5. 如有必要,通过右键单击项目并选择属性来禁用 Microsoft 对 C 语言的扩展。选择对话框顶部的所有配置。然后转到 C/C++ → 语言 → 禁用语言扩展:是的。

Visual Studio will create the following files for your project. Just get used to having them there. Do not check items with a * into source control.

Visual Studio 将为您的项目创建以下文件。只是习惯了他们在那里。不要将带有 * 的项目检查到源代码管理中。

  • ProjectName.sln
  • ProjectName.sdf*
  • ProjectName.suo*
  • ProjectName.vcxproj
  • ProjectName.vcxproj.user*
  • ProjectName.vcxproj.filters
  • somename.c
  • 项目名称.sln
  • 项目名称.sdf*
  • 项目名称.suo*
  • 项目名称.vcxproj
  • 项目名称.vcxproj.user*
  • 项目名称.vcxproj.filters
  • 某个名字.c

回答by JC Leyba

VS actually has a very capable C compiler, somethng that people overlook all too often. The above answers will point you in the right direction, but it's by no means low quality like I've heard people say in the past.

VS 实际上有一个非常强大的 C 编译器,这是人们经常忽略的东西。以上答案将为您指明正确的方向,但这绝不是我过去听到人们所说的低质量。