Conan: Creating a simple header only package -- "header.hpp: No such file or directory"

Created on 29 Dec 2018  路  3Comments  路  Source: conan-io/conan

I'm trying to package up the PCG-CPP library to conan, however I am having major problems. Here are the steps to reproduce:

conanfile.py:

from conans import ConanFile


class PcgcppConan(ConanFile):
    name = "pcg-cpp"
    version = "1.0.0"
    build_policy = 'missing'
    no_copy_source = True

    def source(self):
        self.run("git clone https://github.com/imneme/pcg-cpp")

    def package(self):
        self.copy("*.hpp", dst='.')

    def package_id(self):
        self.info.header_only()

Run:

conan create . dodiknikola/pcg-cpp
conan export . pcg-cpp/1.0.0@dodiknikola/

In a different project's conanfile.txt, include pcg-cpp/1.0.0@dodiknikola/stable, and run conan install .., cmake ...

In one of the project files have #include <pcg_random.hpp>, and get the following error from GCC when trying to compile:

fatal error: pcg_random.hpp: No such file or directory
#include <pcg_random.hpp>

The documentation wasn't able to explain to me how to fix this problem, and I would be grateful if somebody could clear this up for me?

question

Most helpful comment

Hi @dodiknikola !

You need to change your package method:

    def package(self):
        self.copy("*.hpp", dst="include", keep_path=False)

When conan downloads pcg-cpp and copies it to build folder, it searches for all *.hpp files and preserve their paths, so your package folder looks like:

${HOME}/.conan/data/pcg-cpp/1.0.0/dodiknikola/stable/package//pcg-cpp/include/

However, Conan uses package_folder/include as include directory by default. Thus, you need to declare keep_path=False to remove all directories from path and set dst="include" to indicate the headers place.

Also, you could change the includedir path:

    def package_info(self):
        self.cpp_info.includedirs = ["pcg-cpp/include"]

But it sounds tricky, so I do recommend the first option.

If you read Conan documentation about header-only, it shows self.copy("*.h"), but all files must be in include dir and your current path must be build_folder/project_name.

Regards!

All 3 comments

Hi @dodiknikola !

You need to change your package method:

    def package(self):
        self.copy("*.hpp", dst="include", keep_path=False)

When conan downloads pcg-cpp and copies it to build folder, it searches for all *.hpp files and preserve their paths, so your package folder looks like:

${HOME}/.conan/data/pcg-cpp/1.0.0/dodiknikola/stable/package//pcg-cpp/include/

However, Conan uses package_folder/include as include directory by default. Thus, you need to declare keep_path=False to remove all directories from path and set dst="include" to indicate the headers place.

Also, you could change the includedir path:

    def package_info(self):
        self.cpp_info.includedirs = ["pcg-cpp/include"]

But it sounds tricky, so I do recommend the first option.

If you read Conan documentation about header-only, it shows self.copy("*.h"), but all files must be in include dir and your current path must be build_folder/project_name.

Regards!

Yay it worked, thank you!

Hi @dodiknikola

A couple of notes and hints:

  • As you defined name and version, not necessary to define them again in create and export, just specify user and channel:
conan create . dodiknikola/testing
conan export . dodiknikola/testing
  • No need to do the export. The export is already done in the create as the first step, actually.
  • I would avoid the build_policy = 'missing', it is usually better to actually create the package.
  • You can skip no_copy_source, unless the package is very big and performance of the copy is an issue, no need to use it, it is only a small optimization.
  • Instead of using another project to check if the package was created ok, I would strongly recommend to use a test_package (as the one generated with conan new -t), check the docs. Such test_package is exactly a consumer project, but automated.

As this question seems to be solver, I am closing it now, but do not hesitate to ask for further help if needed! :)

Was this page helpful?
0 / 5 - 0 ratings