Thursday, May 13, 2021

.C as a file extension for C++ is not portable

Some projects use .C as a file extension for C++ source code. This is ill-advised, because it is can't really be made to work automatically and reliably. Suppose we have a file source.C with the following contents:

class Foo {
    int x;

Let's compile this with the default compiler on Linux:

$ cc -c -o /dev/null source.C

Note that that command is using the C compiler, not the C++ one. Still, the compiler will autodetect the type from the extension and compile it as C++. Now let's do the same thing using Visual Studio:

$ cl /nologo /c source.C
source.C(1): Error C2061 Syntax error: Identifier 'Foo'
<a bunch of other errors>

In this case Visual Studio has chosen to compile it as plain C. The defaults between these two compilers are the opposite and that leads to problems.

How to fix this?

The best solution is to change the file extension to an unambiguous one. The following is a simple ZSH snippet that does the renaming part:

for i in **/*.C; do git mv ${i} ${i:r}.cpp; done

Then you need to do the equivalent in your build files with search-and-replace.

If that is not possible, you need to use the /TP compiler switch with Visual Studio to make it compile the source as C++ rather than C. Note that if you use this argument on a target, then all files are built as C++, even the plain C ones. This is unreliable and can lead to weird bugs. Thus you should rename the files instead.


  1. Anything which relies on capitalisation is ill conceived. Case-insensitive filesystems and/or operating systems come to mind.

  2. With Visual Studio it's actually even worse. It's not widely known, but NTFS is optionally case sensitive, and the last time I tried to use Git from Linux subsystem, it checked out files in the case sensitive mode. Visual Studio itself at the time assumed that file system is case insensitive, and converted all file names in some internal project file to lowercase. The final compiler step then couldn't open the source files. It was fun to figure out that, for very specific definition of fun.