Installing gtest with conan

一世执手 提交于 2020-12-13 03:54:48

问题


I am about to change to conan, in the hope that is will simplify installing my package by my users. It was OK, until I started to add gtest to my package.

During install, I receive messages

gtest/1.8.1@bincrafters/stable: Package installed 
conanfile.txt imports(): Copied 4 '.a' files: libgmockd.a, libgtestd.a, libgmock_maind.a, libgtest_maind.a

However, during build I receive:

/usr/bin/ld: cannot find -lgmock_maind
/usr/bin/ld: cannot find -lgmockd
/usr/bin/ld: cannot find -lgtestd

My CMakeLists.txt file contains

target_link_libraries(
    ${PROJECT_NAME}_GTEST
    Modules
    ${CONAN_LIBS}
)

What is missing? Shall I provide some

link_directories(?)

argument?

(In the meantime, after some trials, I succeeded: Not only

 link_directories(${CONAN_LIB_DIRS_GTEST})

is needed, but also conan's .data must be cleared.)


回答1:


What generator are you using?

I have this in my conanfile.txt requires section

gtest/[~=1.8]@bincrafters/stable

This is what I have for generators in that section

cmake_find_package
cmake_paths

And in the CMakeLists.txt

include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
find_package(gtest REQUIRED)
add_dependencies(something gtest::gtest)
target_link_libraries(something gtest::gtest)

Note that FindGTest is a built in module, but Findgtest.cmake is a file generated by conan in the build directory.




回答2:


The Bincrafters package for gtest is marked as obsolete, you should use the one in the conan center.
For that, simply add the conan recipe to the conanfile.txt/py.
Let's say you use a plain conanfile.txt:

# conanfile.txt
[requires]
gtest/1.10.0
    
[generators]
cmake

Then you can run conan install

Then add the conan instructions to your project's CMakeLists.txt:

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})

You may print a message to see the included libraries:
message("-- Conan libs: ${CONAN_LIBS}").
This should include both gtest and gmock.

Finally, just include the header, and use the framework:

 #include "gtest/gtest.h"
    
 TEST(TestName, Foo)
 {
     EXPECT_TRUE(true);
 }


来源:https://stackoverflow.com/questions/57136560/installing-gtest-with-conan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!