1

I am trying to build a simple test project using gTest as a dependency. My conanfile.txt looks like:

[requires]
gtest/1.13.0

[generators]
CMakeToolchain
CMakeDeps

[layout]
cmake_layout

Installing the gTest dependency works fine with:

conan install . mytests/1.0.0@

The folder with the conanfile.txt also contains a simple CMakeLists.txt:

project(my_test)

find_package(GTest REQUIRED)

add_executable(my_test main.cpp)

target_link_libraries(my_test GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(my_test)

At the build step it fails with:

$ conan build .
ERROR: Conanfile not found at /home/user/myTest/conanfile.py

Is it really necessary to create a full blown conanfile.py or am I missing something?


Edit: The goal would be to just build and run the project. No need to package my_test in the end as a conan package.

When I try to use cmake to build after installing the dependencies, cmake has issues finding the conan generated GTestConfig.cmake files. Are there any suggestions how to let CMake know where to find the cmake config files?

  1. Extending the CMAKE_PREFIX_PATH? I don't really want to make my CMakeLists.txt file dependent on locations to my local conan cache
  2. Any conan virtualrunenv generator
  3. Some other way?

I would be interested to know which way you suggest to build after running conan install.

cmake --build --preset release seems to work pretty well without further modifications.

evolved
  • 1,850
  • 19
  • 40
  • `conan build` calls the `build()` method in a `conanfile.py`. You use that when you want to build a conan package. What do you want to do? Build your project `my_test` or should `my_test` itself end up as a conan package? – MSpiller Apr 20 '23 at 14:53
  • The conanfile.txt is only supported to solve dependencies for simple cases, where you want to install dependencies and build your project by your build tool (cmake) commands. What are you doing is following the development flow, by using conan build command, but it's used to build packages and require `build()` method configured. So you have 2 options: Convert your conanfile.txt to conanfile.py and convert your project to a package. Or, you simply run cmake . && cmake --build . – uilianries Apr 20 '23 at 16:05
  • Thank you both for your answers. I made an edit to my question. It seems cmake presets do the job for me. However I would be interested to know about other ways for building with cmake. @uilianries running `cmake . && cmake --build .` doesn't work for me because CMake doesn't know about the build/Release/generators/GTestConfig.cmake script. What is your suggested way to let CMake know about the find package scripts? – evolved Apr 20 '23 at 22:20
  • 1
    Sorry, my approach is not complete, what you need to do: Remove layout entry from your conanfile.txt first, because you are not building a package, and will only mess your folder when building with cmake. Second, you need to execute the follow commands in order, from your project folder: `conan install . --output-folder=build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake && cmake --build .`. These commands will build your project. – uilianries Apr 21 '23 at 07:16
  • 1
    Also, please Please, read https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html. The cmake tutorial is exactly same as your scenario, so all steps are detailed there, so you can learn more about Conan too. Regards. – uilianries Apr 21 '23 at 07:17
  • Thanks for the helpful and detailed answer! – evolved Apr 21 '23 at 11:55

0 Answers0