简体   繁体   中英

my first conan package not getting found by CMAKE?

I have seemingly successfully built my first local conan package and uploaded it to my local artifactory.. I can also add it by full name in the dependencies of other projects like so..

[requires]
thrift/0.13.0
mypkg/0.0.1@user/channel

[generators]
cmake

I can link it without error likeso in my OtherProject CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(OtherProject)
add_compile_options(-std=c++11)

# Using the "cmake" generator
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_executable(OtherProject src/Main.cpp)
target_link_libraries(OtherProject CONAN_PKG::my-pkg)

but when I attempt to reference this in my consuming project's C++ it fails to find the mypkg.a file or any headers I expect to be available to it? Did I build my recipe incorrectly? Or am I just needing to change how I reference my new conan package in my code ?

OtherProject/consumingHeader.H

#pragma once
#include "MyPkgsHeader.h"

Error on build

> cmake --build .
Scanning dependencies of target OtherProject
[ 50%] Building CXX object CMakeFiles/OtherProject.dir/src/Main.cpp.o
In file included from /OtherProject/src/Main.cpp:12:
/OtherProject/src/TestCppClient.h:8:10: fatal error: 'MyPkgsHeader.h' file not found
#include "MyPkgsHeader.h"
         ^~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/OtherProject.dir/src/Main.cpp.o] Error 1
make[1]: *** [CMakeFiles/OtherProject.dir/all] Error 2
make: *** [all] Error 2

MyPkg Recipe conanfile.py

from conans import ConanFile, CMake, tools


class MyPkgConan(ConanFile):
    name = "mypkg"
    version = "0.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 mypkg 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"

    def source(self):
        self.run("git clone --depth 1 --branch 0.0.1 git@github.com:PROJECT/my-pkg.git")

        tools.replace_in_file("my-pkg/CMakeLists.txt", "         LANGUAGES CXX )",
                              '''         LANGUAGES CXX )
add_compile_options(-std=c++11)''')

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="my-pkg")
        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="my-pkg")
        self.copy("*hello.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)
        self.copy("*.so", dst="lib", keep_path=False)
        self.copy("*.dylib", dst="lib", keep_path=False)
        self.copy("*.a", dst="lib", keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["mypkg"]

#EDIT: Seeing a problem with includes folder

As instructed I took a look at my conanbuildinfo.cmake and specifically looked for the path under CONAN_INCLUDE_DIRS_MY-PKG

the source files are in a folder structure I didn't expect

/../<conanhash>/include/source/cppclient/client/MyPkgsHeader.h

when I was expected perhaps

/../<conanhash>/client/MyPkgsHeader.h

or

/../<conanhash>/MyPkgsHeader.h

Sounds like I need to change my recipe a bit...

The right process to debug this kind of problems is the following:

  • Check the generated conanbuildinfo.cmake file, it will contain variables like CONAN_INCLUDE_DIRS pointing to folders in the Conan cache, that should contain the expected headers.
  • If you cannot identify the variables in the conanbuildinfo.cmake that belongs to your missing dependency, it might be that you are missing a requires = "pkg/version..." in your recipe, or to execute conan install again to fetch that dependency and generate a new conanbuildinfo.cmake
  • Navigate to those folders, check if the expected headers are there. If they are not, it seems that the package was incorrectly created. You need to go back to the recipe that created that package, fix it and execute conan create again
  • Most common problems while packaging the final artifacts is some wrong pattern or path in the self.copy() calls inside the package() method . For headers the call would be something like self.copy("*.h", dst="include", src="<path/to/headers>") , where the src argument could be wrong.
  • It is possible to go to the Conan cache folder and navigate inside after a conan create and check manually if the expected files are there.

In order to avoid problems of incorrectly created packages, it is very recommended to use the test_package functionality. In short it is a "consumer" project, together with the package recipe that will be automatically fired when conan create , and it will install+build+execute whatever app you tell it, to make some basic checks about the package and fail as early as possible if not correct. Check the "test_package" docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM