60

since CLion has released a month ago there aren't many documents about it. So I'm confused about how to create a c project with CLion, when I want to create a new project I just asks the name of the project and creates a default main.cpp and CMakeLists.txt file which refers to main.cpp file. Well I can rename the file main.cpp to -> main.c and edit CMakeLists.txt manually but there are a few things in .txt file too, so I need some help over here.

Default CMakeLists.txt file;

cmake_minimum_required(VERSION 2.8.4)
project(example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(dbsg ${SOURCE_FILES})

Note: The problem might have an easier solution like create a C project instead of C++ project but I cannot see, so I have to let people who read this about the problem might have an easier solution then editing manually, thanks.

fx773d
  • 601
  • 1
  • 5
  • 3

2 Answers2

74

From the CMake file you provided, you can simply delete the CMAKE_CXX_FLAGS line, or perhaps replace it with a C one like this:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror")

The rest of it should be fine, apart from renaming main.cpp to main.c as you said.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    This method is working like a charm, thank you for giving the proper replacement. Here is the full CMakeLists.txt for amateurs like me. cmake_minimum_required(VERSION 2.8.4) project(example) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror") set(SOURCE_FILES main.c) add_executable(dbsg ${SOURCE_FILES}) – fx773d Oct 03 '14 at 10:59
  • 1
    @fx773d: You're welcome. You know, I messed up and left "CXX" where I meant to put "C" in there. You should change yours so you get compiler warnings turned on for C...it'll work without but this will help you catch bugs during the build. – John Zwinck Oct 03 '14 at 14:10
  • Thanks @JohnZwinck this has helped me as well. But may I ask how you came across to this solution? Does JetBrains have a documentation for this? If so, it's well hidden as I could not find it. Thanks! – Timber Oct 19 '14 at 01:53
  • 8
    @TimothyOnggowasito: I have never used JetBrains CLion, but I have used CMake, so I knew what needed to be done at a more fundamental level. Never believe that an IDE can completely abstract away your build system.... – John Zwinck Oct 19 '14 at 09:46
  • Thanks @John! I've been using CLion for my C++ apps, and eclipse for C. This is going to help a million times over! (Still new to CMake so I never would have figured this out). I wish I could give you more votes! – dhazelett Jan 16 '16 at 04:33
  • Are there any solution to create C projects by default? In 2016.2 I cant see any permanent solutions to create C projects. Editing CMakeList every time I create a new project is a bit time consuming :( Thanks – Armand Bozsik Sep 06 '16 at 10:12
7

Starting with version 2016.3.2 you can choose language (C or C++) and project type (Executable or Library) when creating a new project.

(Though this was in CodeBlocks for example for as long as I remember)) And I still never figured out whether I can create my own "project". Well IMHO CLion clearly sucks in this way if compared to CodeBlocks where I CAN WRITE MY SCRIPT FOR ALMOST EVERYTHING and customize the IDE this way((()

screenshot 1 screenshot 2
screenshot 3

https://blog.jetbrains.com/clion/2016/12/clion-2016-3-2-eap/#prj_templates

PS: a screenshot on how to leave a bugreport at https://youtrack.jetbrains.com/issues/CPP
create_issue button

Rules
  • 103
  • 1
  • 12
  • 1
    Do you have more info on this? I selected C Executable and it still creates a C++ software. Makes no sense – Maude Jul 09 '17 at 02:16
  • @Maude , How do you know it's C++, not C? I've updated my answer, see screenshots.. If what you say is really true than you may [leave a bug report](https://youtrack.jetbrains.com/issues/CPP) (also see screen in my answer on where to click) – Rules Jul 09 '17 at 14:22