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

6
  • "Should I be using some cmake function to use the pkgconfig files? How?" - This is described in the link stackoverflow.com/questions/29191855/… which you listed in your question. Have you tried solutions at that link?
    – Tsyvarev
    Commented Jul 27, 2023 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
    Commented Jul 27, 2023 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
    Commented Jul 27, 2023 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
    Commented Jul 28, 2023 at 12:20
  • Yes, name of the package to search via pkg-config should correspond to the name of .pc file.
    – Tsyvarev
    Commented Jul 28, 2023 at 12:32

0

Browse other questions tagged or ask your own question.