11

I have generated .h and .c files which must be compiled as C++ files with Visual Studio. I heard I could do something like set_source_files_properties( ${SOURCES} PROPERTIES LANGUAGE "CXX" ). In parallel, I have read that I should not do that as it should used only in exotic situations.

So, what is the best way to force CMake to generate VS projects which compiles C files as C++ files?

PS: renaming the generated files is not an option.

Korchkidu
  • 4,908
  • 8
  • 49
  • 69

1 Answers1

14

I have been using the individual properties (most likely you can also set directory properties - but this is not as fine grained) without any problems. Not for setting the language though (but why should it be any different?):

SET_SOURCE_FILES_PROPERTIES( file1.c PROPERTIES LANGUAGE CXX )

This works under Linux and Windows and doesn't seem to pose any problems. What have you heard that makes you hesitate?

Anonymous
  • 18,162
  • 2
  • 41
  • 64
  • Edited - I've been using `SET_SOURCE_FILES_PROPERTIES` for specific compile flags and definitions and it worked as a charm. – Anonymous Apr 13 '12 at 09:42
  • What is the modern method to do so? – Royi Sep 02 '18 at 20:32
  • 1
    @Royi I think that it didn't changed too much in modern CMake. You probably can try to change target property, but thing for files will likely stay the same. – val - disappointed in SE Jun 11 '19 at 16:29
  • Clang prints a **deprecated** message: `clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated]` – Salva Corts Aug 12 '20 at 10:16
  • Is it possible to force it from the command line and not from the _CMakeLists.txt_ files? – Sandburg Feb 02 '23 at 10:20