简体   繁体   English

在VS2015中使用cl编译器的C vs C ++

[英]C vs C++ using cl compiler in VS2015

How does the cl compiler know whether I'm compiling C or C++ code? cl编译器如何知道我是在编译C还是C ++代码?
Do I need to tell the compiler? 我需要告诉编译器吗?
I am running out of the Developer Command Prompt for VS2015. 我正在用完VS2015的Developer Command Prompt。 I originally started this project with c++ code that compiled and ran on a RedHat Linux PC. 我最初使用在RedHat Linux PC上编译和运行的c ++代码启动了这个项目。 I moved it to WIN7 and it would not compile under the cl compiler unless I got rid of the c++ constructs and implemented C equivalents. 我将它移动到WIN7并且它不会在cl编译器下编译,除非我摆脱了c ++构造并实现了C等价物。
A constructor in a header file was one of the issues I had to work around. 头文件中的构造函数是我必须解决的问题之一。

As documented in MSDN , it first looks at the filename extension. MSDN中所述 ,它首先查看文件扩展名。 If it is .cpp or .cxx then it defaults to C++ compilation. 如果是.cpp或.cxx则默认为C ++编译。 Almost always good enough to get the job done. 几乎总是足以完成工作。 That same page also shows how to force the selection, use /Tc for C and /Tp for C++. 同一页面还显示了如何强制选择,使用/ Tc表示C和/ Tp表示C ++。 You'd use /TC and /TP to force it for all source files. 您将使用/ TC和/ TP强制它为所有源文件。

The file extensions will give that info. 文件扩展名将提供该信息。

.c files for C and .cpp files for C++. 用于C ++的C和.cpp文件的.c文件。 Check this link for details. 请查看此链接了解详情。

Also, I found this code here to show wether a C or C++ compiler was used: 此外,我在这里发现此代码显示使用了CC++编译器:

The code below contains standard, pre-defined macros used at runtime to determine whether a C or C++ compiler was used to compile the code. 下面的代码包含运行时使用的标准预定义宏,用于确定是否使用C或C ++编译器来编译代码。 For C compilers, it also determines the version of the C language standard that the compiler implements). 对于C编译器,它还确定编译器实现的C语言标准的版本。 NOTE: Some people prefer to use STDC_HEADERS rather than STDC .. 注:有些人喜欢使用STDC_HEADERS而非STDC ..

#ifdef __cplusplus
 printf("C++ compiler\n");
#else
#ifdef __STDC__
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||/*continue..
    ...on the next line */ (defined(__GNUC__) && !defined(__STRICT_ANSI__))
 printf("ANSI X3.159-1999 / ISO 9899:1999 (C99) compiler\n");
#else
 printf("ANSI X3.159-1989 (C89) / ISO/IEC 9899:1990 (C90) C compiler\n");
#endif
#else
 printf("Pre-ANSI (K&R) C compiler\n");
#endif
#endif

The code below determines whether a C or C++ compiler was used to compile the code at runtime: 下面的代码确定是否使用C或C ++编译器在运行时编译代码:

if (sizeof('c') != sizeof(int)) printf("C++ compiler\n");
else if ((unsigned char)1 < -1) printf("Pre-ANSI (K&R) C compiler\n");
else { int i;   i = 1 //* */
 +1;
if (i == 2) printf("ANSI X3.159-1999 / ISO 9899:1999 (C99) compiler\n");
else printf("ANSI X3.159-1989 (C89) / ISO/IEC 9899:1990 (C90) C compiler\n");}

Just not to rely entirely on a link, here's the info from the docs: 只是不要完全依赖链接,这是来自文档的信息:

Remarks 备注

By default, CL assumes that files with the .c extension are C source files and files with the .cpp or the .cxx extension are C++ source files. 默认情况下,CL假定扩展名为.c的文件是C源文件,扩展名为.cpp或扩展名为.cxx的文件是C ++源文件。 When either the TC or Tc option is specified, any specification of the /Zc:wchar_t (wchar_t Is Native Type) option is ignored. 如果指定了TC或Tc选项,则忽略/ Zc:wchar_t(wchar_t是本机类型)选项的任何规范。 To set this compiler option in the Visual Studio development environment 在Visual Studio开发环境中设置此编译器选项

  1. Open the project's Property Pages dialog box. 打开项目的“ 属性页”对话框。 For details, see How to: Open Project Property Pages. 有关详细信息,请参见如何: 打开项目属性页。
  2. Click the C/C++ folder. 单击C / C ++文件夹。
  3. Click the Advanced property page. 单击“ 高级”属性页。
  4. Modify the Compile As property. 修改Compile As属性。

And an example, also taken from the docs. 还有一个例子,也来自文档。

The following CL command line specifies that TEST1.c, TEST2.cxx, TEST3.huh, and TEST4.o are compiled as C++ files, and TEST5.z is compiled as a C file. 以下CL命令行指定将TEST1.c,TEST2.cxx,TEST3.huh和TEST4.o编译为C ++文件,并将TEST5.z编译为C文件。

CL TEST1.C TEST2.CXX TEST3.HUH TEST4.O /Tc TEST5.Z /TP

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

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