Tips & Tricks

How To Address 7 Major C++ Pain Points with CLion

Read this post in other languages:

In the 2023 Annual C++ Developer Survey conducted by the C++ Foundation, the community identified a number of major pain points when working with C++ (see page 11 of the survey summary).

Pain points

As we’ll discuss in this article, CLion can help C++ developers with most of these in various ways:

DOWNLOAD CLION

Managing third-party libraries

The biggest challenge when working with C++ is managing third-party libraries that a C++ application depends on: 47% of users identified this as a major pain point. Unlike many other programming languages, C++ does not have a standard package manager, and various solutions are used. CLion comes with built-in support for vcpkg, one of the most popular C++ package managers. You can also use the other popular C++ package manager, Conan, with CLion. The Conan plugin for CLion is currently not compatible with the latest version of CLion. Work is under way to fix this, but in the meantime, you can use Conan with CLion anyway since Conan outputs CLion-compatible CMake files.

For third-party packages known to CMake, CLion now offers code completion for find_package.

Boost completion in CMake

Improving build times

The second-biggest challenge according to users is build times, which 43% of respondents identified as a major pain point. While CLion does not include a compiler but simply invokes one configured in the toolchain (like GCC or Clang, for example) and therefore cannot directly influence build times, you can optimize the CMake execution step with CLion’s CMake profiling ability. With CMake profiling, you can identify the most time-consuming operations during CMake configuration, and optimize your CMake scripts if necessary, which helps with reducing the overall build time of your project.

CMake Profiling

Setting up a CI-pipeline

The third-biggest major pain point according to the survey is setting up a continuous integration pipeline from scratch (automated builds, tests, etc). CLion offers a lot of tools to help with this. It has built-in support for all major C++ testing frameworks (Google Test, Boost.Test, Catch2, Doctest, CTest). This includes dedicated run and debug configurations for tests, gutter icons to run or debug tests/suites/fixtures and check their status, a specialized test runner, and code generation for tests and fixture classes.

Further, there is a CLion plugin for TeamCity, JetBrains’ own automated build management and CI tool, as well as plugins for various other continuous integration solutions.

Unit testing

Managing a CMake project

31% of users reported that a major pain point for them is managing CMake projects. CMake is the most popular cross-platform build system for C and C++. CLion comes bundled with CMake and has extensive CMake integration that makes managing CMake projects a smooth experience. CLion has a powerful editor with full syntax highlighting, autocompletion, and quick documentation for CMake scripts; a built-in CMake debugger; built-in GUI support for adding configurations, build types, creating CMake targets, adding or removing files to/from these targets, and reloading the CMake project; support for CMake presets and CMake profiles; and much more.
CMake Debugger

But CMake is not the only project model that CLion supports. Managing Makefiles is also commonly reported by users as a pain point. The good news is that CLion also supports Makefile projects and the Makefile language, which greatly simplifies working with Makefiles.

Checking code for thread, memory and type safety issues on-the-fly

Several other major pain points reported by users relate to safety. This includes thread safety (races, deadlocks, etc.), memory safety (bounds safety, use after free, memory leaks, etc.) and type safety. To identify and fix these common issues, CLion offers various possibilities for static and dynamic code analysis to the developer. For static analysis, CLion offers many useful code inspections, integration with Clang-Tidy, and built-in data flow analysis.

CLion’s data flow analysis (DFA) can statically identify many sources of undefined behavior and potential vulnerabilities, such as endless loops, infinite recursion, missing return statements, dangling pointers, potentially-invalidated iterators, and out-of-bounds memory accesses. DFA can work locally, within a single function, as well as globally, on a whole translation unit. Here is an example where local DFA identifies an iterator that might be invalidated by a call to vector::push_back:

Dangling iterator

Here is another example where global DFA warns about a write-after-free through a pointer that has been deleted in another function:

Dangling pointer

For dynamic analysis, CLion fully integrates with Valgrind as well as with numerous sanitizers, such as AdressSanitizer, LeakSanitizer, ThreadSanitizer, UndefinedBehaviourSanitizer, and MemorySanitizer. We also recently added better support for multithreaded debugging to track down concurrency issues, such as our parallel stacks view and the ability to freeze and unfreeze individual threads during debugging.

Development environment

Another pain point reported by users is the difficulty of setting up a development environment from scratch (compiler, build system, etc). CLion can help with this, too: We already mentioned that CLion provides a bundled CMake, but it also ships with a bundled debugger; bundled MinGW on Windows; Docker support via a dedicated Docker plugin and Docker toolchain; and more. For remote development, CLion supports JetBrains Gateway, which lets you run CLion on a remote host and connect to it via a thin client, as well as various other ways to set up a remote development environment.

Gateway

Modernize your code

Finally, moving existing code to the latest language standard is a major pain point for 8% of users and a minor pain point for 27% of users. To easily move existing code to a more modern C++ standard, CLion supports Clang-Tidy modernize checks directly in the IDE.

Modernize C++ code

DOWNLOAD CLION

image description