4

By default CLion C and C++ files has united file type in Settings->Editor->File Types But in inspect code .c file recognize as plain C (for example type bool requires the inclusion < stdbool.h >) but actually it must be compiled as C++

Help->About: CLion CL-140.2310.6 Build #CL-140.2310, built on February 12, 2015 ...

Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
  • Are you really using a lowercase `.c` extension for C++ source code? Or it really is C code, but you want to run it through the more comprehensive C++ type checker? – Ben Voigt Mar 04 '15 at 19:59
  • Yes, lowercase .c extension is a heavy heritage of more than a decade of project which now contain C++ code. – Alexey Nikitin Mar 05 '15 at 11:19
  • There is a standard associating some extensions with specific languages. Forcing tools to ignore this standards will create lots of problems, spatially for future maintainer. It is easier and safer to just rename those files. – Marek R Feb 18 '22 at 22:11

3 Answers3

3

for CMakeLists.txt project (without clangd completion)

Put

SET_SOURCE_FILES_PROPERTIES(${SOURCE_FILES} PROPERTIES LANGUAGE CXX)

in your CMakeLists.txt.

As commented by user7610 below this seems not to work when clangd completion is enabled (though I haven't confirmed it yet).

for compilation database project

insert ""-xc++"," to [].arguments[1] in compile_commands.json.

--- compile_commands_original.json  2022-03-10 12:08:05.025572787 +0900
+++ compile_commands.json   2022-03-10 12:09:16.250045217 +0900
@@ -12663,6 +12663,7 @@
     {
         "arguments": [
             "g++",
+            "-xc++",
             "-c",
             "-std=c++11",
             "-fno-PIE",

(This happens when I build gcc with bear.)

wataash
  • 415
  • 7
  • 13
  • 1
    Wow this is great. That should be the answer lol – kaptsea Dec 11 '21 at 12:13
  • 1
    This no longer works with the Clangd autocompletion enabled. I had to go to Settings in `Languages & Frameworks` -> `C/C++` -> `Clangd` and `Disable Clangd completion`. Only then the autocompletion appeared for me. – user7610 Feb 18 '22 at 20:23
1

Rename the .c files to a proper suffix for C++, for example .cpp:

for i in `find . -name '*.c'`; do j=`echo $i | sed s/\.c/\.cpp/` ; mv $i $j; done

Seriously, if it is C++ code, it doesn't make sense to have it in .c files.

Once you have a working CMakeLists.txt, there is no reason to preserve the .c extension, since you can use cmake to generate build configuration for almost any build system on earth :-) See the cmake documentation for "generators": http://www.cmake.org/cmake/help/v3.2/manual/cmake-generators.7.html

marco.m
  • 4,573
  • 2
  • 26
  • 41
  • Of course, changing file extensions, this is the first that comes to mind, however, it will lead to changes in the build system project (by the way, we don't use cmake; CMakeLists.txt generate himself CLion) and in order to try a new IDE would not like to change the project. I.e., my question was whether in the CLion fine-tune the type of the source file. Nevertheless, thank you. – Alexey Nikitin Mar 10 '15 at 13:54
  • 1
    @AlexeyNikitin: sure, I understand. What I wanted to suggest is too look at cmake generators, and then make the bold move to convert your build system to cmake :-) You will be happy, I assure you :-) – marco.m Mar 10 '15 at 14:45
1

I am using the Clangd autocompletion engine in CLion, and with that, the recursive set_source_files_properties CMake command (from Can CMake use g++ to compile C files?) did not work.

To fix it, I added file .clangd to the root of my project, containing

CompileFlags:                        # Tweak the parse settings
  Add: [-xc++, -std=c++20]           # treat all files as C++
  Compiler: clang++                  # Change argv[0] of compile flags to `clang++`

The config file is documented at https://clangd.llvm.org/config.

user7610
  • 25,267
  • 15
  • 124
  • 150
  • I had to revisit this and it seems that the clangd in Clion ignores that config file now? /me confused... – user7610 Apr 02 '23 at 19:16