1

I am trying to write some code in C++ in QtCreator to connect to a grpc server when I get this compiler error:

/some/path/serverproxy.cpp:40: error: undefined reference to `grpc::InsecureChannelCredentials()'

Here's my grpc includes at the top of serverproxy.cpp

#include <grpc++/channel.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>

and the error comes from this line:

somenamespace::NewStub(
        grpc::CreateChannel("someaddress",
                            grpc::InsecureChannelCredentials()))};

I tried adding existing files and adding these headers in QtCreator (although it would be very strange to have to do that manually for the headers to all external libraries), but it didn't work. They are located at /usr/local/include/grpc++/**/*.h. Also tried adding INCLUDEPATH += /usr/local/include to my .pro file.

I also tried cleaning the project, running qmake and building again.

What do I have to do to be able to use these grpc++ functions?

Matthias
  • 3,160
  • 2
  • 24
  • 38
  • 3
    I think this question closed incorrectly. In this specific case, you can add output of the `pkg-config --libs grpc protobuf` and `pkg-config --cflags grpc protobuf` into compiler options. Also in my case another library is required the `grpc++_unsecure` perhaps because I used insecure channel. I suggest you to use `CMakeList.txt` and `common.cmake` of the official examples for simplicity. Please note that you need to set `PKG_CONFIG_PATH` pointing to `package` folder of the installed grpc in order to work earlier commands. – S.M.Mousavi Oct 14 '21 at 20:01

1 Answers1

3

I looked at so many undefined reference in qtcreator threads without noticing this, which I finally saw here which is what got rid of the undefined reference errors:

You are facing a link error. Just add in your .pro the path to your lib and its name with :

LIBS += -L<path_to_the_lib> -l<libName>

specifically

LIBS += -L/usr/local/include/grpc++ -lgrpc++
Matthias
  • 3,160
  • 2
  • 24
  • 38
  • I know this is pretty old, but do you happen to remember if you added anything else to the .pro file? Simply adding the line above does not fix it for me completely. – fmcgarry Mar 02 '20 at 18:21
  • sadly I do not, but I remember there was something really bad about this solution of mine, a misunderstanding of how these things work. There was some much cleaner way to do this – Matthias Mar 03 '20 at 14:42
  • Thanks for responding. I was able to figure out why adding this line of code didn't work correctly on my system. I was compiling grpc++ as a static library instead of a shared library. I ran cmake with ```-DBUILD_SHARED_LIBS=ON``` to generate the shared library files I needed. – fmcgarry Mar 03 '20 at 18:44