How to use Conan Package Manager for C++ on CLion - Mac

已完成

In this tutorial, we will be using the C++20 standard and the boost library. Feel free to use whichever C++ standard you want.

General Setup:

This should only be done once.

 

Step 1: Install Conan

This can be done easily using pip. Open your terminal and run:

sudo pip install conan

This will install Conan globally on your machine.

 

Step 2: Install Conan plugin in CLion

Open CLion and click on the CLion icon in the top-left corner and go to preferences > plugins. Search for Conan and install it. Restart CLion to save the changes.

 

Step 3: Create aliases for installing Conan packages to avoid typing long phrases.

Go back to your terminal and add the following to your shell profile:

alias conan-i20='conan install . -s build_type=Debug -s compiler.libcxx=libc++ -s cppstd=20 --install-folder=cmake-build-debug --build missing'

alias conan-i17='conan install . -s build_type=Debug -s compiler.libcxx=libc++ -s cppstd=17 --install-folder=cmake-build-debug --build missing'

alias conan-i14='conan install . -s build_type=Debug -s compiler.libcxx=libc++ -s cppstd=14 --install-folder=cmake-build-debug --build missing'

alias conan-i11='conan install . -s build_type=Debug -s compiler.libcxx=libc++ -s cppstd=11 --install-folder=cmake-build-debug --build missing'

alias conan-ir20='conan install . -s build_type=Release -s compiler.libcxx=libc++ -s cppstd=20 --install-folder=cmake-build-release --build missing'

alias conan-ir17='conan install . -s build_type=Release -s compiler.libcxx=libc++ -s cppstd=17 --install-folder=cmake-build-release --build missing'

alias conan-ir14='conan install . -s build_type=Release -s compiler.libcxx=libc++ -s cppstd=14 --install-folder=cmake-build-release --build missing'

alias conan-ir11='conan install . -s build_type=Release -s compiler.libcxx=libc++ -s cppstd=11 --install-folder=cmake-build-release --build missing'

This will add aliases for installing Conan packages in debug and release modes for C++11, 14, 17, and 20. From Xcode/clang 10, apple requires that to use “libc++” as the CPP library flag. For older versions, use  “compiler.libcxx=libstdc++”.

Your shell profile may be under the name of “.zprofile” if using zsh or “.bash_profile” if using bash. This file is hidden in your home directory. Run “ls -al” to view all files including hidden ones.

Save the changes and exit. Restart your terminal and run “where conan-i20” to verify that the aliases were added. This should output:

conan-i20: aliased to conan install . -s build_type=Debug -s compiler.libcxx=libc++ -s cppstd=20 --install-folder=cmake-build-debug --build missing

 

Project Setup

This should be done for every CLion project.

Create a CLion project and set up Conan by doing the following (we'll assume the name of the project is "conan_test"):

 

Step 1: Link Conan executable:

Click on the CLion icon and go to preferences > Build, Execution, and Deployment > Conan. Add the path to the Conan executable and click OK. If you don’t know this, go to your terminal and run “where conan” to view the path.

 

Step 2: Update the CMakeLists.txt file.

Update the cmakelists.txt to look like below:

cmake_minimum_required(VERSION 3.16)
project(conan_test)

set(CMAKE_CXX_STANDARD 20)

set(CONAN_CMAKE_CXX_STANDARD 20)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(conan_test main.cpp)
target_link_libraries(conan_test ${CONAN_LIBS})

This would give an error because "conanbuildinfo.cmake" doesn’t exist yet.

 

Step 3: Add a conanfile.txt

To fix the error generated above, create a new file called “conanfile.txt” and add the following to it.

[requires]
boost/1.73.0

[generators]
cmake

Your dependencies (the packages you want to use) should go under the “requires” section. To search for packages, run (you can use either CLion’s terminal or your mac’s terminal):

conan search <package-name> --remote=conan-center

This will search for the package on your local computer and if it doesn’t find it, it will look for it in a remote location, in this case, conan-center. You can also change the remote location if you want. Note, this may take a few seconds. All available boost packages will be listed. Choose the one you want and add it to the "requires" section of the conanfile.txt.

 

Step 3: Install the packages

Go to the terminal window on CLion and run “conan-i20” (or “conan-ir20”, if you want to build the package in release mode).

Note: Every time you add a new dependency, you have to run the above command again. The already installed packages won’t be installed again.

A possible output is shown below. In the second screenshot, you’ll see that a “conanbuildinfo.cmake” was created. The error in your CMakeLists.txt should be gone now and you should be ready to use Boost now. Paste the following code in your main.cpp and run it. You shouldn’t get any errors.

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <iterator>
#include <iostream>

int main(){
using namespace boost;
using namespace boost::adaptors;

copy(irange(1, 10) | filtered([](int x){ return x % 2 == 0;} ),
std::ostream_iterator<int>(std::cout, ","));
return 0;
}

 

After running the program, your output should look like:

请先登录再写评论。