0

I have an application that link against another library that uses GRPC. If I use make I can link it using pkg-config --libs protobuf grpc++ grpc and it works. But I am upgrading to cmake (in addition to vcpkg) and now when I try to link my app against that library. When I do that, I get myriad linking errors

In function `absl::lts_20220623::MutexLock::MutexLock(absl::lts_20220623::Mutex*)':
[build] (.text._ZN4absl12lts_202206239MutexLockC2EPNS0_5MutexE[_ZN4absl12lts_202206239MutexLockC5EPNS0_5MutexE]+0x26): undefined reference to `absl::lts_20220623::Mutex::Lock()'

I can make that go away by adding the flags to the target_link_libraries like so

target_include_directories(
....
/workspace/third_party/centos/grpc_1480/lib
....
)
target_link_libraries(kv-validation_lib PRIVATE fmt::fmt 
    libkv.a 
    libkvproto.a 
    yaml-cpp
    -lpthread 
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF}
    stdc++fs
    <pkg-config --libs protobuf grpc++ grpc GOES HERE AND FIXES IT>
)

But obviously, that's more like a hack. So, my question is how do I tell cmake to link against libraries that exists in a give location. Is there a FLAG that I can pass it to ${_ABSL}? Should I be using some cmake function to use the pkgconfig files? How?

If you can't tell, I a new to cmake. Thanks!

I tried using some flags like ${ABSL} but that didn't work. Looked at: How to link app against a library that depends on another library? What is the proper way to use `pkg-config` from `cmake`? Using a C++17 library against a C++11 application

Raaka
  • 304
  • 1
  • 3
  • 15
  • "Should I be using some cmake function to use the pkgconfig files? How?" - This is described in the link https://stackoverflow.com/questions/29191855/what-is-the-proper-way-to-use-pkg-config-from-cmake which you listed in your question. Have you tried solutions at that link? – Tsyvarev Jul 27 '23 at 17:33
  • Yes I did, I get cmake errors from that, specifically when I try to add `pkg_check_modules(gRPC REQUIRED IMPORTED_TARGET gRPC)` it says that it was unable to find the required package 'grpc'. I have `PKG_CONFIG_PATH` to point to the correct `.pc` file. Also, I'm not sure if case matters in there, is gRPC same as grpc? – Raaka Jul 27 '23 at 17:49
  • "I'm not sure if case matters in there, is gRPC same as grpc?" - When use `pkg-config` the case matters. Because `pkg_check_modules` is just a wrapper around that utility, the case for its last parameter matters too. – Tsyvarev Jul 27 '23 at 18:15
  • Ok, by changing the case, I cmake was able to find the package (but I think it's picking up GRPC installed by vcpkg, not the one installed here `/workspace/third_party/centos/grpc_1480/lib`). So, I have another question if you could answer, how do I find the name to use in cmake files? Should I be looking at .pc files? Should I have noticed that when I was installing the other library? Please know, posting on stackoverflow was really the last option I chose after having spent 2 days struggling with this. Your pointers have definitely helped me! – Raaka Jul 28 '23 at 12:20
  • Yes, name of the package to search via pkg-config should correspond to the name of `.pc` file. – Tsyvarev Jul 28 '23 at 12:32
  • I got this to work, from what I've learned, in cmake the package name to use have to match (case sensitive) with the .pc file that you can use `CMAKE_PREFIX_PATH` for cmake to look for. I kind of thought that cmake "knows" about some major packages and thus they have to match with what cmake "knows", but obviously that's not true. I have found the combination of cmake and vcpkg to take away most painful parts of working with C++! Thanks @Tsyvarev for helping me with this. – Raaka Aug 02 '23 at 16:05

0 Answers0