Reputation: 143
How does the cl compiler know whether I'm compiling C or C++ code?
Do I need to tell the compiler?
I am running out of the Developer Command Prompt for VS2015. I originally started this project with c++ code that compiled and ran on a RedHat Linux PC. 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.
A constructor in a header file was one of the issues I had to work around.
Upvotes: 4
Views: 2640
Reputation: 11497
The file extensions will give that info.
.c
files for C and .cpp
files for C++. Check this link for details.
Also, I found this code here to show wether a C
or C++
compiler was used:
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. For C compilers, it also determines the version of the C language standard that the compiler implements). NOTE: Some people prefer to use STDC_HEADERS rather than 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:
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. When either the TC or Tc option is specified, any specification of the /Zc:wchar_t (wchar_t Is Native Type) option is ignored. To set this compiler option in the Visual Studio development environment
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 /Tc TEST5.Z /TP
Upvotes: 4
Reputation: 941407
As documented in MSDN, it first looks at the filename extension. If it is .cpp or .cxx then it defaults to C++ compilation. 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++. You'd use /TC and /TP to force it for all source files.
Upvotes: 6