9

I plan to write application in C using Microsoft Visual Studio 2012. The problem is that I can't find a way to compile it right in the editor. I found this solution http://msdn.microsoft.com/en-us/library/bb384838.aspx but I dont like it. Can you recommend me a way to compile C program right in the Visual Studio 2012?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
ddrisit
  • 93
  • 1
  • 1
  • 3
  • Did you try to compile your project as C++ project? Do you get any error messages if you try it? – Alexander Galkin Dec 20 '11 at 18:08
  • Can you explain why you didn't like the solution you found? Otherwise, you're asking people to guess what you would like more. – Drew Dormann Dec 20 '11 at 18:11
  • check that out http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/bce36f1a-2044-40df-b83c-567ce5ca7c0c – K'' Mar 12 '13 at 20:25

6 Answers6

19

It is a little bit tricky to compile plain C90 and C++x0 (only partially supported) projects in VS2010 (and probably Visual Studio 11, I haven't tried native development in it yet).

What you have to do is to create a new C++ project without precompiled header -- this is the primary requirement if you want to compile a platform-independent code (library, console application).

There are several ways to do it. One way is to create a normal Win32 C++ console application, in the opened wizard you should go to the second page (by clicking "Next") and then uncheck the option "Include precompiled header". Then you can compile C++ (and C) projects directly in VS.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
  • Precompiled headers are a compiler optimization. They don't make code platform-dependent in any way. – James McNellis Dec 20 '11 at 18:13
  • 1
    @JamesMcNellis: That is true, but they hinder the compilation process of the plain C++ and C code: the "include" statements are disregarded, the inclusion of the header is mandatory and so on. For compiling the small programms for learning purposes or for C++ snippets from internet it is the best way not to use the header at all, there is no much performance loss due to it. – Alexander Galkin Dec 20 '11 at 18:16
16

Also check "properties -> c/c++ -> advanced -> Compile as" make sure it says "c code"

or on the command line use /TC

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
10

Create an empty C++ project in Visual Studio (File -> New Project -> Visual C++ -> Empty Project), then add a source file with a .c extension to it (right click the project, select Add -> New Item -> C++ File (.cpp), and change the name of the file).

When you build, the compiler will compile it as C code (note that Visual C++ supports only C90, not C99, so newer C language features are not usable).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

By default, the Visual C++ compiler treats all files that end in .c as C source code, and all files that end in .cpp as C++ source code. To force the compiler to treat all files as C regardless of file name extension, use the /Tc compiler option.

Source: MSDN Visual Studio 2013, http://msdn.microsoft.com/en-us/library/bb384838.aspx walkthrough

Go to Menu command to open "Project -> Properties" dialogue. Then in "Configuration Properties" select "C/C++ -> Advanced" pane. Change "Compile As" drop-down box from "Default" to "Compile as C Code (/TC). Click on "Apply" button and then "OK" to close the dialogue.

Any Body
  • 133
  • 6
0

select new project,win 32console application ,uncheck precompiled header,select empty project.. Then add a new item,select..cpp file then rename the file with .c.extension and you're done to compile c codes..

Amith
  • 1
0

It's also same for newer VS versions, what you have to do is as mentioned by @Alexander Galkin,

  1. Right Click to Project Properties
  2. C/C++
  3. Advanced
  4. Set "Compile As" to "Compile as C Code(/TC)"

To demonstrate it visually, check the screen-shot below;

Steps

enter image description here

Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106