12/1/2021

Clion New Project

Clion
  1. Clion New Cmake Project From Sources
  2. Clion New Project
  3. Clion New Project Template

There is an official Jetbrains plugin Conanplugin for CLion.

Create a new project If no project is currently opened in CLion, click New Project on the Welcome screen. Otherwise, select File New Project on the main menu. In the New Project dialog that opens, select the target type of your project (executable or library), and the language to be used (pure C or C). Read on in our blog about Makefile project support in CLion. Known issues which cause most of the failures: CPP-19305 – distcc is not yet supported. CPP-18723 – Makefile projects which use BSD make syntax are not supported. Here is the list of Makefiles projects we've tested on. These 10 CLion tips can take you to a new level of productivity, by letting you focus on the important and complicated tasks while the IDE handles routine jobs for you. Before we start, let me introduce you to CLion, a new cross-platform IDE for developing in C and C, created by JetBrains. At the startup dialog, select Import Project and select your chromium directory; this should be the parent directory to src. Selecting src instead would result in some CLion IDE files appearing in your repository. (Optional) Modify the CMakeLists.txt file Open the CMakeLists.txt file.

You can read how to use it in the following blog post

General Integration¶

CLion uses CMake as the build system of projects, so you can use the CMake generator to manage your requirements in your CLion project.

Just include the conanbuildinfo.cmake this way:

If the conanbuildinfo.cmake file is not found, it will print a warning message in the Messages console of your CLion IDE.

Using packages in a CLion project¶

Let see an example of how to consume Conan packages in a CLion project. We are going to require and usethe zlib conan package.

  1. Create a new CLion project
  1. Edit the CMakeLists.txt file and add the following lines:
Clion

3. CLion will reload your CMake project and you will be able to see a Warning in the console, because theconanbuildinfo.cmake file still doesn’t exist:

4. Create a conanfile.txt with all your requirements and use the cmake generator. In this case we onlyrequire the zlib library from a Conan package:

5. Now you can run conan install for debug in the cmake-build-debug folder to install your requirements andgenerate the conanbuildinfo.cmake file there:

6. Repeat the last step if you have the release build types configured in your CLion IDE, but change the build_typesetting accordingly:

  1. Now reconfigure your CLion project. The Warning message is not shown anymore:

8. Open the library.cpp file and include zlib.h. If you follow the link, you can see that CLion automaticallydetects the zlib.h header file from the local Conan cache.

  1. Build your project normally using your CLion IDE:

You can check a complete example of a CLion project reusing conan packages in this github repository: lasote/clion-conan-consumer.

Creating Conan packages in a CLion project¶

Now we are going to see how to create a Conan package from the previous library.

  1. Create a new CLion project
  1. Edit the CMakeLists.txt file and add the following lines:
  1. Create a conanfile.py file. It’s recommended to use the conan new command.

Edit the conanfile.py:

  • We are removing the source method because we have the sources in the same project; so we can use theexports_sources.
  • In the package_info method, adjust the library name. In this case our CMakeLists.txt creates a target library calledmylibrary.
  • Adjust the CMake helper in the build() method. The cmake.configure() doesn’t need to specify the source_folder, becausewe have the library.* files in the root directory.
  • Adjust the copy function calls in the package method to ensure that all your headers and libraries are copied to the Conan package.
  1. To build your library with CLion, follow the guide of Using packages from step 5.

5. To package your library, use the conan export-pkg command passing the used build-folder. Itwill call your package() method to extract the artifacts and push the Conan package to the localcache:

  1. Now you can upload it to a Conan server if needed:

8. If you would like to see how the package looks like before exporting it to the local cache (conan export-pkg)you can use the conan package command to create the package in a local directory:

If we list the mypackage folder we can see:

  • A lib folder containing our library
  • A include folder containing our header files
  • A conaninfo.txt and conanmanifest.txt conan files, always present in all packages.

You can check a full example of a CLion project for creating a Conan package in this github repository: lasote/clion-conan-package.

Before starting with CLion, check this Quick Start Guide.

Configure toolchains

Configure and use different toolchains for different projects in CLion, or even inside one project for different configurations.

You can change CMake executable, Make, C and C++ compilers and debugger. Note that you can use either bundled or custom version of CMake and debugger executable.

On Linux and macOS you can use GCC or Clang as a compiler, on Windows – you can select between MinGW-w64, MinGW, Cygwin, Clang-cl and MSVC (see below). You can also use the IAR compiler on Linux and Windows.

Use Remote toolchains for building, running and debugging on a remote host.

Windows Subsystem for Linux

On Windows use WSL/WSL2 toolchains. Compile, run, and debug Linux binaries while working on Windows. Test targets under Valgrind Memcheck to detect possible errors and leaks.

Microsoft Visual C++ toolchain

CLion supports and auto-detects Microsoft Visual C++ toolchain from Visual Studio 2013, 2015, 2017, and 2019.

To use Clang-cl instead of MSVC, select the Visual Studio toolchain in CLion and point to clang-cl.exe in the toolchain settings (versions 8.0 and later are supported).

The LLDB-based debugger for the Visual Studio C++ toolchain developed by JetBrains is a default debugger for this toolchain. To enable bundled support for native visualizers, use Settings Build, Execution, Deployment Debugger Data Views Enable NatVis renderers for LLDB.

Project models

All work in CLion is done within the context of a project, which serve as the basis for coding assistance, bulk refactoring, coding style consistency, and other smart features.

CLion supports:

  • CMake,
  • Makefile,
  • new Gradle C++ plugins,
  • compilation database projects.

For all four CLion provides full code insight.

For CMake you can enable various CMake generators (Ninja, Xcode, Visual Studio, etc.).

Start a new project

To start a completely new project, use File New Project... to generate the sample project. For C and C++ CLion will create a CMake project and pre-fill root CMakeLists.txt with the necessary information.

Open an existing project

To start with an existing project in CLion, select one of the following options:

  • For CMake project: locate the root CMakeLists.txt file, CMakeCache.txt file or CMake generation directory (the last two options work only for Makefiles generator).
  • For Gradle C++: open the build.gradle file from the project sources.
  • For compilation database: use the compile_commands.json file (see this list of ways to get a compilation database for your project).
  • For other project models: open a single file or a folder with C/C++ sources in CLion, mind the code insight will be limited in this case.
  • Alternatively, import a C/C++ project from an existing source, and CLion will create CMakeLists.txt for you.

Clion New Cmake Project From Sources

Clion

Clion New Project

Add new files

New files can be added to the project by using the New menu. In the Project tree view, press Alt+Insert or select New in the context menu.

Clion New Project Template

  • Click C++ Class to generate a .cpp file source file with a header and a class stub.
  • Click C/C++ Source File to generate a simple source file, optionally with an associated header.
  • Click C/C++ Header File to generate a simple header file.