Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to switch conan1.x to conan2 in gcc and cmake? #13625

Closed
freshduer opened this issue Apr 5, 2023 · 6 comments
Closed

[question] How to switch conan1.x to conan2 in gcc and cmake? #13625

freshduer opened this issue Apr 5, 2023 · 6 comments
Assignees

Comments

@freshduer
Copy link

freshduer commented Apr 5, 2023

so how to find my downloaded packages with conan_toolchain.cmake?

My workspace is Clion and gcc12.1 and cmake
- In conan 1.x
I compile my work in the way below:

  1. cd ./cmake-build-debug
  2. use "conan install .. --build=missing" to download the package i need
  3. use "conan build .. -c" to get a conanbuildinfo.cmake file,then i can use clion's cmake feature to configue
  4. use "conan build .." to compile my codes
    ok,i get the bin i need,and run in clion

but recently i find conan had updated into conan2, i always want to use new worktools,so i update into conan2
then i get some problems

- In conan 2

  1. cd ./cmake-build-debug——same
  2. use "conan install .. --build=missing" to download the package i need——same
  3. conan build .. -c——conanbuildinfo.cmake deprecated... so i can not use it with conan_basic_setup(TARGETS) in my cmakelists

so how to find my downloaded packages with conan_toolchain.cmake?

this is my conanfile.py:

from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps


class Conan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    requires = "abseil/20230125.2", "glog/0.6.0"
    build_requires = "gtest/1.8.1", "benchmark/1.6.1", "zstd/1.5.2", "boost/1.79.0", "concurrentqueue/1.0.3"
    # ,"folly/2020.08.10.00"
    generators = "CMakeToolchain"
    # Binary configuration
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": True, "fPIC": True}
    no_copy_source = True

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()
```

**this is my cmakelists:**
```
cmake_minimum_required(VERSION 3.22)
project(work_projects)

set(CMAKE_CXX_STANDARD 23)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)

conan_basic_setup(TARGETS)

enable_testing()
add_subdirectory(test_and_benchmark)

add_subdirectory(work_projects)
add_subdirectory(test)
```

### Have you read the CONTRIBUTING guide?

- [X] I've read the CONTRIBUTING guide
@memsharded memsharded self-assigned this Apr 5, 2023
@memsharded
Copy link
Member

Hi @freshduer

thanks for your question.

The new CMake integrations like CMakeDeps and CMakeToolchain are transparent: they work with a Conan agnostic CMakeLists.txt, using a standard find_package() and the -DCMAKE_TOOLCHAIN_FILE=..../conan_toolchain.cmake.

In your recipe you are missing:

  • Actual usage of CMakeDeps to generate the xxx-config.cmake files of dependencies
  • Remove the CMakeLists.txt include(conanbuildinfo.cmake) replace it with find_package() calls

Also:

  • It seems that your build_requires are not correct, they should be test_requires (they will be in the host context, not the build context).
  • It is recommended to use the cmake_layout(self) (inside a layout() method), to have the generated files not polluting your root folder

I'd recommend doing the tutorial in: https://docs.conan.io/2/tutorial/consuming_packages.html. Also the Conan 2.0 conan new cmake_lib -d name=pkg -d version=0.1 command will give you a full working example that could be a good starting point.

@freshduer
Copy link
Author

freshduer commented Apr 6, 2023

when i use CmakeDeps,i meet an error which i found this is a bug:

ValueError: Paths don't have the same drive During handling of the above exception, another exception occurred:
conans.errors.ConanException: error generating context for 'abseil/20230125.2': Paths don't have the same drive

ERROR: Error in generator 'CMakeDeps': error generating context for 'abseil/20230125.2': Paths don't have the same drive

and it will be fix in conan 2.0.4: #13607 #13606 (comment)
looking forward to 2.0.4

@memsharded
Copy link
Member

Thanks for the feedback @freshduer

That is the only missing piece, if you put your folders in the same drive to avoid that error, then things are working now with the new integrations? If that is the case, we might close this ticket as solved for 2.0.4

@freshduer
Copy link
Author

i want to wait for 2.0.4....i am not in a hurry to fix this problem

@freshduer
Copy link
Author

2.0.4 had fixed this problem, finally i can use conan again...

This is my reciepe

from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout


class Conan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": True, "fPIC": True}
    no_copy_source = True
    generators = "CMakeToolchain", "CMakeDeps"

    def requirements(self):
        self.requires("abseil/20230125.2")
        self.requires("glog/0.6.0")
        self.requires("zstd/1.5.2")
        self.requires("boost/1.79.0")
        self.requires("concurrentqueue/1.0.3")
        self.requires("gtest/1.8.1")
        self.requires("benchmark/1.6.1")
        # ,"folly/2020.08.10.00"

    def layout(self):
        if self.settings.build_type == "Debug":
            cmake_layout(self, build_folder="cmake-build-debug")
        else:
            cmake_layout(self, build_folder="cmake-build-release")

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

Cmakelist

cmake_minimum_required(VERSION 3.22)
project(work_projects)

set(CMAKE_CXX_STANDARD 23)

#conan_basic_setup(TARGETS)
find_package(absl REQUIRED)
find_package(Boost REQUIRED)
find_package(zstd REQUIRED)
find_package(glog REQUIRED)
find_package(gtest REQUIRED)
find_package(benchmark REQUIRED)

include_directories(${absl_INCLUDE_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${zstd_INCLUDE_DIRS})
include_directories(${glog_INCLUDE_DIRS})
include_directories(${gtest_INCLUDE_DIRS})
include_directories(${benchmark_INCLUDE_DIRS})

enable_testing()
add_subdirectory(test_and_benchmark)
add_subdirectory(test)
add_subdirectory(work_projects)

I can compile the binaries now.

If there are no problems, we can close the issue

@memsharded
Copy link
Member

Great, happy that it is working now.

Thanks for following up and reporting the final working recipe!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants