This post explains how to setup a development environment for C and C++ projectsusing Clang as compiler, CMake as build system, CLion as IDE and Conanas package manager. The name 4 Cs is cool, but not my idea, it has beencoined by well known C++ blogger Arne Metz, authorof the great Simplify C++ blog
The JetBrains (formerly IntelliJ) from Czech Republic has offered many excellent IDEs for various website and application developers. For example, the IntelliJ IDEA has been recognized as one of the best Java developing tools in the industry. Here, of course, what AppNee concerns about is all the approaches that can effectively activate JetBrains/IntelliJ all products. For more information on adding a license to a repository, see 'Adding a license to a repository.' The goal of GitHub's open source licensing efforts is to provide a starting point to help you make an informed choice. GitHub displays license information to help users get information about open source licenses and the projects that.
Though Clang has some support in Windows, its usage there is still low compared with MSVC,and Apple-Clang is the default on OSX, so it has not special difficulties. This postwill use Ubuntu 14.04. As example, we will develop a simple application using thewell known POCO and Boost libraries.
Setting up Clang C/C++ compiler
The default compiler in Ubuntu is GNU/gcc, so we have to install first Clang:
Installed this way, clang will not add itself to the path. It is true thatsetting environment variables CC and CXX is enough for most use cases. Butfor others, as building boost, it is much simpler if clang is in the path,otherwise editing .bjam files might be necessary. Currently, the conan package for boost,does not do that, so it requires clang to be in the path. Lets add a couple of links(most likely we can also use other ways as update-alternatives):
Setting up latest CMake (meta) build system
The default CMake version bundled with Ubuntu 14 is still 2.8.12, which is really old.CMake current version is 3.5 at the time of this writing, so lets install it. Go toCMake downloads and get it. In this case, the tar.gzwas retrieved and installed, prepending the bin
subfolder to the path, so it getshigher priority. I did this way because I have to keep CMake 2.8.12 as default, butit is easy to add it permanently to the path.
We will be using CMake 'Unix Makefiles' generators. Though it is possible to passthe compilers to CMake in the command line, I find it error prone, so I usuallyprefer to set the CC and CXX environment variables:
This is also convenient for later CLion usage with this compiler.
Setting up Conan C/C++ package manager
To install conan in your computer, the easiest way is with Python package:
You might need to use sudo if you are installing it globally, not necessaryif using a virtualenv.
Set up the project
Inside the folder there is a simple conanfile.txt
defining our dependencies:
Let's install the dependencies. We will use a temporary folder called .conan
(you can use any name), as the CLion editor uses an external build folder. Usinga known and close folder for conan temporary files allows easy management of dependencies:
Note the final --build=missing
. Most packages in conan provide binaries for the mainstream compilers,MSVC in Win, GNU/gcc in Linux and Apple-Clang in OSX. So, it is expected that pre-compiled binariesfor CLang in Linux will not be available, so we have to tell conan to build them from sources.Go and grab a coffee, it will build Poco, Boost and some other libs!
This install step will generate 2 files: conanbuildinfo.cmake
with some CMake variablesdefined inside, as include paths and library names and conaninfo.txt
which stores your currentconfiguration. You can use in parallel many configurations if you want, just place each one in aseparate folder.
You can check the installed dependencies with:
Developing with CLion
CLion is a cross-platform C/C++ IDE from JetBrains.Let's build our application with CLion. As we already have defined CC and CXX environment variables, launching CLion inour terminal will automatically use CLang as default compiler.
If you go to File->Settings, you could see the compiler is Clang:
It is possible too to use the CMake variables -D CMAKE_C_COMPILER=clang-3.6 -D CMAKE_CXX_COMPILER=clang++-3.6
defined in the CLion IDE, to define the compiler. Note that typically a CMake cache clean&restart might be necessary.Now, it is possible to do such clean&restart without restarting the IDE in Menu->Tools->CMake->Reset Cache and Reload Project(the other option is Menu->File->InvalidateCache&Restart, but as it restarts the IDE, it is slower). The clang compiler will be used in your builds, but it will not show as default in the Settings dialog.
From CLion CMakeLists.txt project file, we can load the generated conanbuildinfo.cmake
.
Clion License Server 2020
CLion will be able to autocomplete from both Poco and Boost headers:
That is all! You can now select in CLion the 'timer' target, build it, and run it!
Conclusions
Setting up a development environment for C++ with these 4 tools is not complicated.Basically make sure that CLang is in the path, and that CMake is using it (throughthe environment variables CC and CXX) instead of the sytem GNU/gcc compiler.
Also I found more intuitive to launch CLion with those variable defined, so it detectsthe CLang compiler and shows it in the Settings dialog. It is possible to use cmake command lineparameters -DCMAKE_C_COMPILER=clang-3.6 -DCMAKE_CXX_COMPILER=clang++-3.6
in the same CLionSettings dialog, but a CMake cache restart is typically necessary, as CMake cache onlystores the compiler the first invocation (which is automatically done by CLion, before able to setthe command line parameters).
Clion License Server Github Ubuntu
Conan packages (Poco, Boost and their dependencies: Zlib, electric-fence…) have built finewith Clang, but it is also possible that some other conan packages have not been thoroughlytested yet with CLang, and they might eventually fail to build. Please contribute in thatcase, submitting an issue to the package repository.
The default CMake version bundled with Ubuntu 14 is still 2.8.12, which is really old.CMake current version is 3.5 at the time of this writing, so lets install it. Go toCMake downloads and get it. In this case, the tar.gzwas retrieved and installed, prepending the bin
subfolder to the path, so it getshigher priority. I did this way because I have to keep CMake 2.8.12 as default, butit is easy to add it permanently to the path.
We will be using CMake 'Unix Makefiles' generators. Though it is possible to passthe compilers to CMake in the command line, I find it error prone, so I usuallyprefer to set the CC and CXX environment variables:
This is also convenient for later CLion usage with this compiler.
Setting up Conan C/C++ package manager
To install conan in your computer, the easiest way is with Python package:
You might need to use sudo if you are installing it globally, not necessaryif using a virtualenv.
Set up the project
Inside the folder there is a simple conanfile.txt
defining our dependencies:
Let's install the dependencies. We will use a temporary folder called .conan
(you can use any name), as the CLion editor uses an external build folder. Usinga known and close folder for conan temporary files allows easy management of dependencies:
Note the final --build=missing
. Most packages in conan provide binaries for the mainstream compilers,MSVC in Win, GNU/gcc in Linux and Apple-Clang in OSX. So, it is expected that pre-compiled binariesfor CLang in Linux will not be available, so we have to tell conan to build them from sources.Go and grab a coffee, it will build Poco, Boost and some other libs!
This install step will generate 2 files: conanbuildinfo.cmake
with some CMake variablesdefined inside, as include paths and library names and conaninfo.txt
which stores your currentconfiguration. You can use in parallel many configurations if you want, just place each one in aseparate folder.
You can check the installed dependencies with:
Developing with CLion
CLion is a cross-platform C/C++ IDE from JetBrains.Let's build our application with CLion. As we already have defined CC and CXX environment variables, launching CLion inour terminal will automatically use CLang as default compiler.
If you go to File->Settings, you could see the compiler is Clang:
It is possible too to use the CMake variables -D CMAKE_C_COMPILER=clang-3.6 -D CMAKE_CXX_COMPILER=clang++-3.6
defined in the CLion IDE, to define the compiler. Note that typically a CMake cache clean&restart might be necessary.Now, it is possible to do such clean&restart without restarting the IDE in Menu->Tools->CMake->Reset Cache and Reload Project(the other option is Menu->File->InvalidateCache&Restart, but as it restarts the IDE, it is slower). The clang compiler will be used in your builds, but it will not show as default in the Settings dialog.
From CLion CMakeLists.txt project file, we can load the generated conanbuildinfo.cmake
.
Clion License Server 2020
CLion will be able to autocomplete from both Poco and Boost headers:
That is all! You can now select in CLion the 'timer' target, build it, and run it!
Conclusions
Setting up a development environment for C++ with these 4 tools is not complicated.Basically make sure that CLang is in the path, and that CMake is using it (throughthe environment variables CC and CXX) instead of the sytem GNU/gcc compiler.
Also I found more intuitive to launch CLion with those variable defined, so it detectsthe CLang compiler and shows it in the Settings dialog. It is possible to use cmake command lineparameters -DCMAKE_C_COMPILER=clang-3.6 -DCMAKE_CXX_COMPILER=clang++-3.6
in the same CLionSettings dialog, but a CMake cache restart is typically necessary, as CMake cache onlystores the compiler the first invocation (which is automatically done by CLion, before able to setthe command line parameters).
Clion License Server Github Ubuntu
Conan packages (Poco, Boost and their dependencies: Zlib, electric-fence…) have built finewith Clang, but it is also possible that some other conan packages have not been thoroughlytested yet with CLang, and they might eventually fail to build. Please contribute in thatcase, submitting an issue to the package repository.
Use this page to specify the version control settings that will be applied to the directories of your project that are under Git control.
Item | Description |
---|---|
Path to Git executable | In this field, specify the path to the Git executable file. Type the path or click Browse and specify the path in the dialog that opens. CLion supports Git from the Windows Subsystem for Linux 2 (WSL2), which is available in Windows 10 version 2004. If Git is not installed on Windows, CLion searches for Git in WSL and uses it from there. Also, CLion automatically switches to Git from WSL for projects that are opened when you use the wsl$ path. |
Test | Click this button to verify the path to the Git executable file. |
Set this path only for current project | Select this option if you want to use different paths for different projects, and don't want this setting to be applied globally. |
Execute branch operations on all roots | This option only becomes available if you have a multirooted project, that is there are several Git repositories within a single project. Select this option if you want branch operations (such as |
Commit automatically on cherry-pick | When you cherry pick a specific commit, the Commit Changes dialog is displayed. If the Commit automatically on cherry-pick option is selected, the selected commit is submitted silently on clicking the cherry-pick button , without displaying the Commit Changes dialog. |
Add the 'cherry-picked from ' suffix when picking commits pushed to protected branches | Select this option if you want to keep a reference to the original commit when cherry-picking a commit from a protected branch. By default, no suffix is added when cherry-picking a change. |
Warn if CRLF line separators are about to be committed | Select this option to enable smart handling of This setting is not applied to files where you have set any related Git attributes. In this case, CLion assumes that you clearly understand what you are doing and excludes such files from analysis. If this option is deselected, you will have to fix issues with line endings manually using the Difference Viewer dialog. |
Warn when committing in detached HEAD or during rebase | Select this option if you want CLion to display a warning when a commit is performed from a detached head or on rebase, as this may cause issues and code loss. |
Explicitly check for incoming commits on remotes | If this option is enabled, CLion will check if there are pending incoming commits that have not been fetched to your local repository, and will mark such branches in the Branches popup. Select how you want to CLion to query the remote to check for incoming commits:
|
Update method | Use this list to choose the strategy to synchronize your local repository with the remote storage. The selected method will be used when the push operation is rejected (if the Auto-updated if push of the current branch was rejected option is enabled), or when you invoke the Update Project operation. The following options are available:
|
Clean working tree using | Select how you want uncommitted changes to be treated when you perform a project update:
|
Auto-update if push of the current branch was rejected | Select this checkbox if you want the current branch to be updated automatically if the If this option is deselected, CLion will display the Push Rejected dialog when pushing a branch is rejected because your local repository and the remote storage are not synchronized. Note the following:
|
Show Push dialog for Commit and Push | Select this option if you want the Push dialog to be displayed after you've clicked Commit and Push in the Commit Changes dialog. Otherwise, your changes will be pushed automatically to the affected repository. |
Show Push dialog only when committing to protected branches | Select this option if you only want to show the Push Changes dialog if you are pushing to a protected branch when you've clicked Commit and Push in the Commit Changes dialog. Otherwise, your changes will be pushed automatically to the affected repository. |
Protected branches | If you want to disable the ability to force push changes for certain branches, list them here (this is a team-shared parameter that is stored in .idea/vcs.xml). You can list several branches separated by a semicolon, or supply branch patterns as the input is treated as a list of regular expressions. |
Use credential helper | Select this option if you don't want to override credential helpers>credential helpers, which is the default behavior. You will be able to authenticate using a credential helper in the Git login dialog. |
Filter Update Project information by paths | If you don't want to get information on all changes to a project in the Update Info tab when you perform an update, you can filter the list by specific paths. |