user313885
user313885

Reputation:

MSVC vs GCC: variable declaration in function

As everybody knows, C standard require to declare variables at the top of function body.

C++ allows to declare it anywhere, just before it will be used.

MSVC follow standard when work with .c and .cpp source files.

GCC allows to declare variables anywhere in function body even if source file is .c

I have a huge number .c files compiling well in GCC and not in MSVC due to this. I can't rename all them to .cpp.

Is there any simple way to tell MSVC compiler to treat .c file as C++ ones?

Upvotes: 0

Views: 1366

Answers (4)

Cat Plus Plus
Cat Plus Plus

Reputation: 129934

C89 standard required that. Current C99 one doesn't. Anyway, the switch is /TP to treat all the input files as C++ and /Tp<file> to treat a select one — keep in mind that this might not fix anything, but also introduce new errors and it will mangle the names, so interop might be hurt.

The best solution would be to simply compile the C part with GCC.

Upvotes: 4

Steve-o
Steve-o

Reputation: 12866

/Tp

http://msdn.microsoft.com/en-us/library/032xwy55(v=vs.80).aspx

MSVC only supports C89.

Upvotes: 1

Dima
Dima

Reputation: 39419

In VisualStudio 2010, go to the properties for your project. Go to Configuration Properties -> C/C++ -> Advanced

For "Compile As" select "Compile as C++ Code (/TP).

This appears to apply to all .c and .cpp files in the project.

Upvotes: 3

user852830
user852830

Reputation:

The setting /TP allows to compile any file as C++ File and /TC as C-File. But unfortunately, this is a per-file-setting. So you would have to set this for every file. Maybe you can do that with a small script and parse the .vcproj file and replace /TC with /TP.

Upvotes: 2

Related Questions