简体   繁体   中英

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM