Skip to main content

Create package

This section introduces how to create your own Conan packages, explain conanfile.py recipes and the commands to build packages from sources in your computer.

Using the conan new command will create a “Hello World” C++ library example project for us:

$ mkdir hellopkg && cd hellopkg
$ conan new hello/0.1 --template=cmake_lib
File saved: conanfile.py
File saved: CMakeLists.txt
File saved: src/hello.cpp
File saved: src/hello.h
File saved: test_package/conanfile.py
File saved: test_package/CMakeLists.txt
File saved: test_package/src/example.cpp

The generated files are:

  • conanfile.py: On the root folder, there is a conanfile.py which is the main recipe file, responsible for defining how the package is built and consumed.
  • CMakeLists.txt: A simple generic CMakeLists.txt, with nothing specific about Conan in it.
  • src folder: the src folder that contains the simple C++ “hello” library.
  • (optional) test_package folder: contains an example application that will require and link with the created package. It is not mandatory, but it is useful to check that our package is correctly created.

conanfile.py

####### conanfile.py #######
from conans import ConanFile, CMake
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
license = "<Put the package license here>"
author = "<Put your name here> <And your email here>"
url = "<Package recipe repository url here, for issues about the package>"
description = "<Description of Hello here>"
topics = ("<Put some tag here>", "<here>", "<and here>")
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False]}
default_options = {"shared": False}
generators = "cmake"
exports_sources = "src/*"
def build(self):
cmake = CMake(self)
cmake.configure(source_folder="src")
cmake.build()
# Explicit way:
# self.run('cmake %s/hello %s'
# % (self.source_folder, cmake.command_line))
# self.run("cmake --build . %s" % cmake.build_config)
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.dll", dst="bin", keep_path=False)
self.copy("*.dylib*", dst="lib", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["hello"]

Also, by setting generators = “cmake”, Conan generates a conanbuildinfo.cmake file that can be included when we build the package with CMake

In this example, the hello world package does not have any required dependency for simplicity so there is not much meaning but If a package has any required dependency, we need to specify the build requirements in the conanfile.py file.