Hey,
I’m trying to build a template file for building all my c++ projects easily, and I want to also include tests via gtest. Currently, I have it easily compiling my cpp code, but for zig run test I can’t figure out how I would link what is needed. Typically the flags -lgtest -ltest_main -pthread but I can’t figure out how to add the -pthread to the script. So far, the test part of my script is as following:
FYI we have packaged googletest so you could turn that system dependency into a normal Zig dependency that the package manager can fulfill directly :^)
That said, going back to your project, if you also link to libc (linkLibC()) (and remove the manual -pthread flag), what errors do you get from the build?
These errors are about gtest, so that’s what you need to fix next. Maybe the version installed in your system is mismatched with what you expect in your project? That might explain the first error, not sure about the second.
Do you have a way of knowing precisely which version of gtest you expect in your codebase and if your system is providing it directly (or a compatible version at least)?
Maybe the suggestion to use the one packaged in allyourcodebase isn’t that bad afterall :^)
I’ll have to admit I’m a bit ignorant to gtest and about Zig, generally. I’m trying to do a basic ASSERT_EQ as seem above, and I can confirm it does with with g++. Is there a way I can use the allyourcodebase one, to see if that works? I certain would like to use Zig as a package manager if I am able to, ideally to help with building on Windows as well since I’m not a fan of manually linking libraries.
Unfortunately troubleshooting C/C++ build pipelines tends to require you to get familiar with the process. In this case, if another system compiler builds everything correctly, it’s probably a missing flag, but exactly which and where does require knowing how the build works.
I can give you some instructions to use Zig to build googletest but let me warn you right away that whatever issue is causing your build to fail might not go away simply by sourcing the dependency through Zig.
Keeping that in mind, in allyourcodebase there’s another repo that depends on googletest, you can copy the relevant lines from there:
First add googletest as a dependency in your build.zig.zon:
Then, in your build.zig, obtain a reference to the dependency:
Finally, link the library:
Note that in your case you also seem to want gtest_main, so make sure to link that too.
This worked perfectly! Thank you so much for your help!
As a followup, do you know of any resources you can link me about the Zig build system and Zon package manager? The more I know about what the build system can do, the better, and libraries will be much better to work with if I can package them simply with Zon, such as the classic SDL2 library (Which I have figured out how to build with addLibraryPath() and installBinFile)