How to set up googletest wtih meson?
Asked Answered
F

2

10

I have a git repository with some test code in C++ and I want to use Googletest to write some tests. I used git submodule to get it as part of the above repository. I want to use meson as the build engine. So far, so good.

However, I cannot fathom how to get meson to build and link my tests with the googletest submodule… Should I use a wrap? An external dependency? what?

Note that meson supports dependencies on packaged versions of gtest/gmock but this is not what I want since the developers of gtest/gmock recommend against it. Plus, I want bleeding edge 'cause I am crazy⸮

Furthermore, I do not think ninja plays a role here but I mentioned that I use it just in case.

Firstborn answered 13/8, 2019 at 7:52 Comment(2)
Sounds about right, what is the linker error?Herndon
@FlorianZwoch Err … An error between keyboard and floor. I was using this clever hack which is not really needed.Firstborn
F
11

I tried using the wrap for gtest with

gtest_proj = subproject('gtest')
gtest_dep = gtest_proj.get_variable('gtest_dep')
gmock_dep = gtest_proj.get_variable('gmock_dep')

in meson.build. This builds a local copies of googletest which can then be used like so:

tests_src = [
  'tests/gtest-all.cpp',
  'tests/test_MyClass.cpp',
]  
e = executable(
  'gtest-all',
  tests_src,
  dependencies : [
    gtest_dep,
    gmock_dep],
  link_with : libshield,
)    
test('gtest tests', e)

Note that libshield is a shared library created from my (toy) code so I can link to it.

Firstborn answered 13/8, 2019 at 13:36 Comment(0)
V
12

If you would like to use a project that is not meson project you need to find that project in wrapDB:

meson wrap search gtest

If that command gives you name of the wrap then you need to install it to your project:

mkdir -p subprojects
meson wrap install gtest

Then you should reconfigure your project and meson will download that project for you:

meson --reconfigure path/to/build/dir

Additional information you can find in documentation of wrap tool.

--reconfigure - supported since 0.49.0

Vibratory answered 20/10, 2019 at 20:51 Comment(5)
meson gives me an error: unrecognized arguments: --reconfigure. It then does on to print a list of possible arguments. None of them appear to be close to reconfigureAppledorf
@Hemil, what is your version of meson?Vibratory
@Hemil, it supported since 0.49.0Vibratory
The one i got from synaptic was 0.45.1 @VibratoryAppledorf
Downloading the latest one from github :) I got it to work anyway. ThanksAppledorf
F
11

I tried using the wrap for gtest with

gtest_proj = subproject('gtest')
gtest_dep = gtest_proj.get_variable('gtest_dep')
gmock_dep = gtest_proj.get_variable('gmock_dep')

in meson.build. This builds a local copies of googletest which can then be used like so:

tests_src = [
  'tests/gtest-all.cpp',
  'tests/test_MyClass.cpp',
]  
e = executable(
  'gtest-all',
  tests_src,
  dependencies : [
    gtest_dep,
    gmock_dep],
  link_with : libshield,
)    
test('gtest tests', e)

Note that libshield is a shared library created from my (toy) code so I can link to it.

Firstborn answered 13/8, 2019 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.