简体   繁体   English

无法在Visual Studio 2012中编译C

[英]Can't compile C in visual studio 2012

I have an older c project that uses many variable names that cause it to not compile in c++, new , this etc. 我有一个较旧的c项目,该项目使用许多变量名,导致其无法在c ++, newthis等中进行编译。

So to try and see if I can get it compiling I have done this: 因此,尝试查看是否可以进行编译,我这样做:

  1. New empty C++ project 新的空C ++项目
  2. Added a new class, renamed the file .c (code below) 添加了一个新类,将文件重命名为.c (下面的代码)
  3. Emptied the header file 清空头文件
  4. Project properties->C/C++->Advanced->Compile As = Compile as C Code (/TC) 项目属性-> C / C ++->高级->编译为=编译为C代码(/ TC)

Test.c: Test.c:

#include "Test.h"

int test()
{
    int new = 123;
    return new;
}

But it still complains about new , so it's not compiling it as pure C. What am I missing? 但是它仍然抱怨new ,因此它没有将其编译为纯C语言。我缺少什么?

EDIT 编辑

I'm aware that new , this etc are reserved names in c++ . 我知道, newthis等在保留的名称c++ But I am trying to compile this as c And I'm trying to avoid going though renaming in a massive project. 但是我试图将其编译为c并且我试图避免在大型项目中重命名。 If I tell it to compile as c , why does it still enforce these reserved names? 如果我告诉它编译为c ,为什么它仍然强制执行这些保留名称?

See the answer here: 在这里查看答案:

https://stackoverflow.com/a/5770919/1191089 https://stackoverflow.com/a/5770919/1191089

There are some additional flags to disable Microsoft extensions which might be applicable. 还有一些其他标志可以禁用Microsoft扩展。

I know it doesn't answer the question, but you might find that it's less effort to change your variable names, a search and replace on variables called "this" and "new" will only take 5 minutes. 我知道它不能回答问题,但是您可能会发现更改变量名称的工作量较小,搜索并替换名为“ this”和“ new”的变量仅需5分钟。

new is a reserved identifier for assigning memory like in new是用于分配内存的保留标识符,例如

int* i = new int(123);

You can't use it. 您不能使用它。 Switch to another name for your variable, like 切换到变量的其他名称,例如

#include "Test.h"

int test()
{
    int i = 123;
    return i;
}

The reserved words of C++ may be conveniently placed into several groups. C ++的保留字可以方便地分为几组。 In the first group we put those that were also present in the C programming language and have been carried over into C++. 在第一组中,我们将那些也存在于C编程语言中并被带入C ++的语言。 There are 32 of these, and here they are: 其中有32个,它们是:

auto   const     double  float  int       short   struct   unsigned
break  continue  else    for    long      signed  switch   void
case   default   enum    goto   register  sizeof  typedef  volatile
char   do        extern  if     return    static  union    while

There are another 30 reserved words that were not in C, are therefore new to C++, and here they are: 还有30个不在C中的保留字,因此对于C ++来说是新的,它们是:

asm         dynamic_cast  namespace  reinterpret_cast  try
bool        explicit      new        static_cast       typeid
catch       false         operator   template          typename
class       friend        private    this              using
const_cast  inline        public     throw             virtual
delete      mutable       protected  true              wchar_t

taken from here . 这里带走的。

您不是将C源代码编译为C代码,而是需要将代码迁移到C ++,这涉及替换变量名称,这些名称在c ++中用作关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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