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.

- 143
- 1
- 1
- 9
-
3Usually by file extensions. C source files tend to have the ending `.c` while there are a couple of alternatives for C++ (`.cpp` is common). – Some programmer dude Sep 10 '15 at 15:00
-
I believe both answers could answer this question by now. – Mauker Oct 28 '15 at 20:39
2 Answers
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.

- 922,412
- 146
- 1,693
- 2,536
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
- Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.
- Click the C/C++ folder.
- Click the Advanced property page.
- Modify the Compile As property.
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

- 11,237
- 7
- 58
- 76
-
-
That's cause that code was written in 2008. So with little modification it can show if it's C11. – Mauker Sep 10 '15 at 15:20