Using CLion for C (in CSC173)

Using CLion for C (in CSC173)

George Ferguson

Last update: Summer 2021
(minor edits Summer 2023)

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
Image

CLion looks like a nice IDE from a company with a strong history (IntelliJ IDEA, for example).

It is available for macOS, Windows, and Linux. It is a commercial product, but there’s a 30-day free trial and special offers for students, educators, and others. It worked very well in my quick testing.

Compiler Options (Required for CSC173)

When you create a C project, CLion will create a file named “CMakeLists.txt” for you, as well as an initial source file (“main.c”).

Assuming that you selected the C99 standard when you created the project, CMakeLists.txt should contain the following line already:

set(CMAKE_C_STANDARD 99)

Add the following to CMakeLists.txt:

set(CMAKE_C_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Werror)
set(CMAKE_VERBOSE_MAKEFILE ON)

Setting CMAKE_C_STANDARD_REQUIRED to ON means CLion will tell you if your compiler can’t do that rather than fall back on something else. I noticed that this setting actually resulted in the -std=gnu99 option being used rather than c99. I can’t find a way to change that, but they’re pretty close anyway (Language Standards Supported by GCC).

Using add_compile_options adds the required compiler options for CSC173. You might consider also adding -Wextra and -pedantic. You might be able to slip -std=c99 in there but I didn’t check that it would appear in the right place.

Setting CMAKE_VERBOSE_MAKEFILE to ON means that CLion will show you the commands that it is using to build your project. This might help you if something doesn’t work, and anyway it’s a good way to learn.

Changing the Executable Program Name (optional)

The default CMakeLists.txt will use add_executable to build an executable with same name as the project from the one source file. Just change the first argument to change the name of the executable.

For example, to build an executable named hello instead of whatever your project is called:

add_executable(hello main.c)

BTW: You can rename the .c file using File > Rename. CLion will update your CMakeLists.txt to reflect the change.