Installing gtest with conan - googletest

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.)

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.

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);
}

Related

How do I tell meson setup about the location of a dependency?

I'm trying to build celluloid, which uses meson. I ran meson, but it failed to find an appropriate version of mpv:
Determining dependency 'mpv' with pkg-config executable '/usr/bin/pkg-config'
Called `/usr/bin/pkg-config --modversion mpv` -> 1
Found CMake: /usr/bin/cmake (3.13.4)
Determining dependency 'mpv' with CMake executable '/usr/bin/cmake'
Try CMake generator: auto
Called `/usr/bin/cmake --trace-expand -DNAME=mpv .` in /tmp/celluloid-0.20/build/meson-private/cmake_mpv -> 0
Dependency mpv found: NO (tried pkgconfig and cmake)
src/meson.build:125:0: ERROR: Dependency "mpv" not found, tried pkgconfig and cmake
so I downloaded and built the latest mpv release (0.33.0), built and installed it at /opt/mpv.
Now - how do I tell meson to take mpv from this new path?
Note: The relevant snippet of the meson files seems to be:
executable('celluloid', sources,
dependencies: [
libgtk,
libgio,
meson.get_compiler('c').find_library('m', required: false),
dependency('mpv', version: '>= 1.107'),
dependency('epoxy')
],
link_with: extra_libs,
include_directories: includes,
c_args: cflags,
install: true
)
You tell meson about your dependency by letting pkgconfig know about your dependency...
and that can be done by adding your dependency's path to the PKG_CONFIG_PATH environment variable; it is delimited by colons, just like PATH, e.g. /opt/foo:/opt/extra/baz.
Remember that you may also need to add associated paths to LD_LIBRARY_PATH after building and installing with a custom-built directory.

How to build and add a dependency library in CMake

For my project, I need to build and include another library, https://github.com/t-dillon/tdoku/, as my dependency.
Toku is also built using CMake, and there's a build script I can run by doing:
$ cd lib/toku
$ ./BUILD.sh
This puts the library file in ./lib/tdoku/build/libtdoku.a.
I'm trying to build the Toku library inside CMake, and use it as a dependency. How can I do this?
I tried using add_custom_target, add_custom_command, add_dependencies, add_subdirectory to no avail.
My current CMakeLists.txt looks like:
cmake_minimum_required(VERSION 3.17)
project(project_1_cpp)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3")
add_custom_target(toku)
add_custom_command(
TARGET toku
COMMAND ${CMAKE_SOURCE_DIR}/lib/tdoku/BUILD.sh
WORKING_DIR ${CMAKE_SOURCE_DIR}/lib/tdoku
)
file(GLOB project_1_src "*.cpp")
add_executable(project_1_cpp ${project_1_src})
add_dependencies(tdoku project_1_cpp)
target_link_libraries(project_1_cpp ${CMAKE_SOURCE_DIR}/lib/tdoku/build/libtdoku.a)
From some help of #Tsyvarev, I first changed my CMakeLists.txt to include:
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/lib/tdoku/build/libtdoku.a
COMMAND ${CMAKE_SOURCE_DIR}/lib/tdoku/BUILD.sh tdoku
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/lib/tdoku
)
add_custom_target(project DEPENDS ${CMAKE_SOURCE_DIR}/lib/tdoku/build/libtdoku.a)
The key is that WORKING_DIR does not work, and I need to instead have WORKING_DIRECTORY. I figured this part out by trying to modify the build.sh script, and having it print things to a file, so I can know if it's being run at all, and what's breaking inside the script. I realized that the script's working directory was wrong.
In fact, I believe I did all this before posting the question, but I got the following errors after, which lead me to think that the tdoku library wasn't compiling properly. It was, but it was linking incorrectly to my project:
/usr/bin/ld: /usr/bin/ld: DWARF error: could not find variable specification at offset 223f
/usr/bin/ld: DWARF error: could not find variable specification at offset 22ba
I googled "DWARF error: could not find variable specification", which led me to ld.exe: DWARF error: could not find variable specification at offset 101cee. Googling cmake flto led me to How do I enable link time optimization (LTO) with CMake?.
This finally allowed me to compile my project.
The entire list of changes include:
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/lib/tdoku/build/libtdoku.a
COMMAND ${CMAKE_SOURCE_DIR}/lib/tdoku/BUILD.sh tdoku
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/lib/tdoku
)
add_custom_target(project DEPENDS ${CMAKE_SOURCE_DIR}/lib/tdoku/build/libtdoku.a)

Include path error in conan cmake configuration

conanfile.txt //i am using local repo
[requires]
libxml2/2.9.0#conan/stable
[generators]
cmake
CMakeLists.txt
cmake_minimum_required(VERSION 3.4)
project(testlibxml)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(test_xml_lib test_xml.cpp)
test_xml.cpp
#include <libxml/xlink.h>
int main(){
return 0;
}
I expect includes to be resolved properly, but i am getting below error
error: libxml/xlink.h: No such file or directory.
It works if i add below line in CmakeLists.txt
include_directories(${CONAN_INCLUDE_DIRS}/libxml2)
I don't know where are you getting that version of the libxml2 package, because it is not in conan-center:
$ conan search libxml* -r=conan-center
Existing package recipes:
libxml2/2.9.3#bincrafters/stable
libxml2/2.9.8#bincrafters/stable
So what you are using might be an old, stale, or broken version from somewhere else. Changing your conanfile.txt:
[requires]
libxml2/2.9.8#bincrafters/stable
[generators]
cmake
And I have checked it works (at least in Windows)

Build MPICH with CMake

I need to force my Cmake to build and link my MPI code with MPICH. My MPICH is installed using the Ubuntu Package manager, in a standard location /usr/lib/mpich/. However, CMake still looks for the OpenMPI libraries, which I do not use. How can I instruct CMake to look for MPICH instead?
Below, you can see the output of some basic diagnostics:
$ whereis openmpi
openmpi:
$ whereis mpich
mpich: /usr/lib/mpich /usr/include/mpich
$ mpicc -v
mpicc for MPICH version 3.2
Below, I also provide the Cmake script and the errors I get from cmake and the mpirun.mpich. My Cmake is 3.5.1 and I run on Ubuntu Xenial 16.04.
cmake_minimum_required(VERSION 3.0)
message (STATUS "Adding mpiService")
find_package(MPI REQUIRED)
set(CMAKE_C_COMPILER mpicc)
set(CMAKE_CXX_COMPILER mpicxx)
set(MPI_GUESS_LIBRARY_NAME MPICH2)
message(STATUS ${MPI_INCLUDE_PATH})
message(STATUS ${MPI_C_LIBRARIES})
#add_definitions(-DOMPI_SKIP_MPICXX)
add_executable(mpiService main.cpp)
set(CMAKE_VERBOSE_MAKEFILE ON)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
target_link_libraries(
mpiService
PRIVATE
${MPI_C_LIBRARIES}
)
From the Cmake STATUS I get the following output:
/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/usr/lib/openmpi/include/openmpi/opal/mca/event/libevent2021/libevent/include/usr/lib/openmpi/include/usr/lib/openmpi/include/openmpi
/usr/lib/openmpi/lib/libmpi.so
And when I run the binary I get the following:
ubuntu#node1:~$ mpirun.mpich -np 2 --host node1,node2 mpiService
mpiService: error while loading shared libraries: libmpi.so.12: cannot open shared object file: No such file or directory
mpiService: error while loading shared libraries: libmpi.so.12: cannot open shared object file: No such file or directory
How can I instruct CMake to look for MPICH instead?
According to FindMPI documentation, you may set MPI_<lang>_COMPILER variable to the desired MPI compiler:
Set MPI_<lang>_COMPILER to the MPI wrapper (mpicc, etc.) of your
choice and reconfigure. FindMPI will attempt to determine all the
necessary variables using THAT compiler's compile and link flags.
set(MPI_CXX_COMPILER <path-to-mpich-compiler>)
find_package(MPI REQUIRED)
Alternatively, since CMake version 3.10, variable MPI_EXECUTABLE_SUFFIX can be set instead:
A suffix which is appended to all names that are being looked for. For instance you may set this to .mpich or .openmpi to prefer the one or the other on Debian and its derivatives.
set(MPI_EXECUTABLE_SUFFIX ".mpich")
find_package(MPI REQUIRED)
Herewith my current solution.
find_package(MPI REQUIRED)
# ----------------
# This is the only thing that made it work
# ----------------
set(MPI_C_LIBRARIES "/usr/lib/mpich/lib/libmpich.so")
set(MPI_INCLUDE_PATH "/usr/include/mpich")
# ----------------
add_executable(mpiService main.cpp)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
target_link_libraries(
mpiService
${MPI_C_LIBRARIES}
)
I personally do not like this solution, as I have to explicitly specify the path. Any other proposed solution was still building with OpenMPI. If I find a better alternative, I will re-post.

How can I run cmake from within cmake?

My project depends on mariadb-connector-c and I'm trying to automate the download, build and link process with cmake.
I currently download the project into a directory, I then try to execute generate ninja files and run them but I cannot run cmake at all:
execute_process(COMMAND "cmake -GNinja ." WORKING_DIRECTORY ${mariadb-connector-c_SOURCE_DIR})
I know this doesn't work because the next step, running ninja, fails:
execute_process(COMMAND "ninja" WORKING_DIRECTORY ${mariadb-connector-c_SOURCE_DIR})
cmake runs fine in CLI, I've tried using the full path to the cmake executable and replacing the dot with the variable with the full directory (which is also a valid variable, if you're wondering.)
How can I tell cmake to run cmake on that external project?
You can organize your project to a top-level CMakeLists.txt build your subprojects as ExternalProject.
This approach requires more work and maintenance of more CMake modules but it has its own benefits. I download Google Test as follows:
# Create download URL derived from version number.
set(GTEST_HOME https://github.com/google/googletest/archive)
set(GTEST_DOWNLOAD_URL ${GTEST_HOME}/release-${GTEST_VERSION}.tar.gz)
unset(GTEST_HOME)
# Download and build the Google Test library and add its properties to the third party arguments.
set(GTEST_ROOT ${THIRDPARTY_INSTALL_PATH}/gtest CACHE INTERNAL "")
ExternalProject_Add(gtest
URL ${GTEST_DOWNLOAD_URL}
CMAKE_ARGS -DBUILD_GTEST=ON -DBUILD_GMOCK=ON -DCMAKE_INSTALL_PREFIX=${GTEST_ROOT}
INSTALL_COMMAND make install
)
list(APPEND GLOBAL_THIRDPARTY_LIB_ARGS "-DGTEST_ROOT:PATH=${GTEST_ROOT}")
unset(GTEST_DOWNLOAD_URL)
unset(GTEST_ROOT)
The code abowe is inside my ExternalGoogleTest.cmake module which is included by CMakeLists.txt of third-party libraries:
set_directory_properties(PROPERTIES EP_BASE ${CMAKE_BINARY_DIR}/ThirdParty)
get_directory_property(THIRDPARTY_BASE_PATH EP_BASE)
set(THIRDPARTY_INSTALL_PATH ${THIRDPARTY_BASE_PATH}/Install)
set(GTEST_VERSION 1.8.0)
include(ExternalProject)
include(ExternalGoogleTest)
Your own project which depends on an external library will need a CMake module to build it as ExternalProject too. It can looks like:
ExternalProject_Add(my_project
DEPENDS gtest whatever
SOURCE_DIR ${CMAKE_SOURCE_DIR}/lib
CMAKE_ARGS
${GLOBAL_DEFAULT_ARGS}
${GLOBAL_THIRDPARTY_LIB_ARGS}
-DCMAKE_INSTALL_PREFIX=${DESIRED_INSTALL_PATH}/my_project
BUILD_COMMAND make
)
You can found more tips about this pattern here.