[C++] Building And Publishing Conan Packages
In this article I'll create a simple library and publish it on Artifactory. If you haven't heared about Conan yet, you can read my first two articles about Conan, a C++ Package Manager:
Getting Started With Conan
Protobuf: Project Setup With Conan and CMake
A Simple Library
In this example I'll only create a simple library with an add(..)
function like this:
// add.hpp
int add (int a, int b );
// add.cpp
int add(int a, int b) { return a+b; }
And this is compiled into a static library (libadd.a
), which means for clients, they need the header and the library.
Artifactory
The steps on the Artifactory are fairly easy. We just create a conan repository:
Add This Repository To Conan
Now we need to add this remote to our conan remotes. Therefore Artifactory already provides the commands we need. Select the button "Set Me Up" on the upper right corner, select our conan-cwt repository and we get the commands. (Depending on the Artifactory version the Artifactory API key is printed there).
conan remote add <REMOTE> https://cwt.jfrog.io/artifactory/api/conan/cwt-conan
conan user -p <PASSWORD> -r <REMOTE> <USERNAME>
where <REMOTE>
is a name of your choice, I'll use cwt
, <PASSWORD>
the API key from artifactory, which is found under your profile and <USERNAME>
your user name (most likely your email address). Now we have added this remote to conan:
$ conan remote list
conancenter: https://center.conan.io [Verify SSL:True]
cwt: https://cwt.jfrog.io/artifactory/api/conan/cwt-conan [Verify SSL:True]
Creating A Conan Package
First of all let's build our add library:
$ cmake -S ./src -B ./build
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /d/git/conan_playground/build
$ cmake --build ./build/
Scanning dependencies of target add
[ 50%] Building CXX object CMakeFiles/add.dir/add.cpp.o
[100%] Linking CXX static library libadd.a
[100%] Built target add
Now we need to implement a conanfile.py
to create a conan package. Our project structure looks like this:
add-lib-directory
│
│ conanfile.py
│
├───build
│ └───out
│ add.hpp
│ libadd.a
│
└───src
add.cpp
add.hpp
CMakeLists.txt
We need to implement a conanfile.py
to create a conan package. Since we have already built our library, we have the following conanfile:
# conanfile.py
from conans import ConanFile, CMake, tools
class addConan(ConanFile):
# these properties we provide for our library
name = "add"
version = "1.0"
generators = "cmake"
exports_sources = "build/out/add.hpp","build/out/libadd.a"
# settings are passed later with the terminal command
settings = "os", "compiler"
# we publish the static library and the header file
def package(self):
self.copy("*.hpp", dst="include", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
# we name this library just "add"
def package_info(self):
self.cpp_info.libs = ["add"]
And now we can create the conan package and publish it to our remote repository. On the export-pkg command we specify all the settings we added in the conanfile.py
and also the name of our library. here we define it as: add/1.0@cwt/stable
(the general syntax here is pkg/version@user/channel
).
$ conan export-pkg . add/1.0@cwt/stable -s os=Linux -s compiler=gcc -s compiler.version=9.3
$ conan upload add/1.0@cwt/stable -r cwt --all
And thats it, we have published our pre-build library to the artifactory:
How To Use This Library
Just like any other library which we access from bincrafters or conancenter. As long you have your remote available, just use a conanfile.txt like this:
[requires]
add/1.0@cwt/stable
[generators]
cmake
Note: In this case you need to make sure, that the pre-build library has the same settings (compiler version, operating system, etc.) as we defined during publishing. You can check your conan profile with conan profile show default
.
Conclusion
There are more options to publish dependencies with conan. This is a short introduction to publish pre-build dependencies to Artifactory. If you have open source projects, you can also publish the entire project, add some build rules and the build is done on clients side. Your conanfile.py
needs to implement this but this is not scope of this article.
That's it for now.
Best Thomas.