Compiling C as C++ in CMake 3.18

I’m (semi-)maintaining a private copy of pthreads4w, which is one of those libraries which uses one “.c” file and different definitions to output several variants of the library. One variant uses C++ exceptions to clean up threads, and when I last used 3.14, I could use:

target_compile_options(${targ} PRIVATE "/TP" "/EHs")

This would allow the one specific target to compile pthread.c as if it were C++ code. CMake 3.18 seems to really insist on having the MSVC “/TC” flag on the command line, completely ignoring my target_compile_options.

Is this a new feature?

1 Like

@brad.king This is another instance of this regression. See this issue as well. The latest 3.18 should have this fixed.

The regression, issue, and fix Ben mentioned were all in the 3.19 series.

To answer the original question, don’t try to use /TP directly. You need to tell CMake that you want the file treated as C++ by setting the LANGUAGE source file property to CXX. There is also ongoing work to make that work in more cases.

Meanwhile, I suggest creating a pthread.cxx that just has #include "pthread.c", and list pthread.cxx as the source instead.

Brad: I think we resolved this on the gitlab issue tracker and I decided to take the #include route.

One problem with the LANGUAGE approach: It’s a property that’s inspected at generation time. Consequently, if one sets LANGUAGE for foo.c in target_a as “C”, then set LANGUAGE as “CXX” in target_b, then target_a will compile foo.c as “CXX”.

That’s the reason why I suggested making LANGUAGE a target-specific property.

I agree that /TP is a hack, but it was one that worked for a long time. :slight_smile: