簡體   English   中英

無法在Visual Studio 2012中編譯C

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

我有一個較舊的c項目,該項目使用許多變量名,導致其無法在c ++, newthis等中進行編譯。

因此,嘗試查看是否可以進行編譯,我這樣做:

  1. 新的空C ++項目
  2. 添加了一個新類,將文件重命名為.c (下面的代碼)
  3. 清空頭文件
  4. 項目屬性-> C / C ++->高級->編譯為=編譯為C代碼(/ TC)

Test.c:

#include "Test.h"

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

但是它仍然抱怨new ,因此它沒有將其編譯為純C語言。我缺少什么?

編輯

我知道, newthis等在保留的名稱c++ 但是我試圖將其編譯為c並且我試圖避免在大型項目中重命名。 如果我告訴它編譯為c ,為什么它仍然強制執行這些保留名稱?

在這里查看答案:

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

還有一些其他標志可以禁用Microsoft擴展。

我知道它不能回答問題,但是您可能會發現更改變量名稱的工作量較小,搜索並替換名為“ this”和“ new”的變量僅需5分鍾。

new是用於分配內存的保留標識符,例如

int* i = new int(123);

您不能使用它。 切換到變量的其他名稱,例如

#include "Test.h"

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

C ++的保留字可以方便地分為幾組。 在第一組中,我們將那些也存在於C編程語言中並被帶入C ++的語言。 其中有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

還有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

這里帶走的。

您不是將C源代碼編譯為C代碼,而是需要將代碼遷移到C ++,這涉及替換變量名稱,這些名稱在c ++中用作關鍵字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM