gRPC giving linkage errors in c++ - c++11

I am writing a program using gRPC in the language c++. I have installed gRPC using the command brew install grpc on a MacOS I also have the most up to date version of protobuf. When I create a basic program using gRPC I compile them using the following commands:
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/test.proto
Which creates the test.pb.h and test.pb.cc files
Then the command protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/Cellar/grpc/1.45.2/bin/grpc_cpp_plugin test.proto which creates the test.pb.h and test.pb.cc files
I then run the command g++ -std=c++11 -stdlib=libc++ test.cpp test.grpc.pb.cc -L/usr/local/lib -lprotobuf to compile, where the main c++ file is test.cpp I then get some of the following linkage errors:
"grpc::ClientContext::ClientContext()", referenced from:
MathTestClient::sendRequest(int, int) in test-e2eddc.o
"grpc::ClientContext::~ClientContext()", referenced from:
MathTestClient::sendRequest(int, int) in test-e2eddc.o
"grpc::CreateChannel(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<grpc::ChannelCredentials> const&)", referenced from:
Run() in test-e2eddc.o
"grpc::InsecureChannelCredentials()", referenced from:
Run() in test-e2eddc.o
"mathtest::MathRequest::MathRequest(google::protobuf::Arena*, bool)", referenced from:
mathtest::MathRequest::MathRequest() in test-e2eddc.o
"mathtest::MathRequest::~MathRequest()", referenced from:
MathTestClient::sendRequest(int, int) in test-e2eddc.o
Which makes sense, judging based off the fact that in the file grcpp/impl/codgen/client_context.h the linkage errors would obviously be there because client context is defined as the following:
class ClientContext {
public:
ClientContext();
~ClientContext();
or with the CreateChannel function it is defined in create_channel.h file as
std::shared_ptr<Channel> CreateChannel(
const grpc::string& target,
const std::shared_ptr<ChannelCredentials>& creds);
In my main c++ file, I only use the following include from gRPC: #include <grpcpp/grpcpp.h>
Are there other include files from gRPC that I need to include which define these functions?

I ended up figuring out my problem if anyone else has installed grpc the same way using homebrew as I did. make sure that on top of the brew install grpc command is run that the commands brew install protobuf and brew install abseil
Next is to find out the paths of all of these using the commands brew info grpc, brew info protobuf and brew info abseil for myself personally the paths are:
/usr/local/Cellar/abseil/20211102.0
/usr/local/Cellar/grpc/1.45.2
/usr/local/Cellar/protobuf/3.19.4
Then the $PKG_CONFIG_PATH needs to be setup, if this does not already exist and there is an error if you run echo $PKG_CONFIG_PATH you can run brew install pkg-config the $PKG_CONFIG_PATH is defined by all the previous paths, with the added path of /lib/pkgconfig which then the $PKG_CONFIG_PATH can be defined as
export PKG_CONFIG_PATH=/usr/local/Cellar/grpc/1.45.2/lib/pkgconfig:/usr/local/Cellar/protobuf/3.19.4/lib/pkgconfig:/usr/local/Cellar/abseil/20211102.0/lib/pkgconfig
After that is all done, and you have the main c++ file and the proto file, mine were named test.cpp and test.proto respectively, the following commands can be run to make it compile correctly and be able to get and then run ./test
>>> protoc --cpp_out=. test.proto
>>> g++ -std=c++11 `pkg-config --cflags protobuf grpc` -c -o test.pb.o >>> test.pb.cc
>>> protoc --grpc_out=. --plugin=protoc-gen->>> grpc=/usr/local/Cellar/grpc/1.45.2/bin/grpc_cpp_plugin test.proto
>>> g++ -std=c++11 `pkg-config --cflags protobuf grpc` -c -o test.grpc.pb.o test.grpc.pb.cc
>>> g++ -std=c++11 `pkg-config --cflags protobuf grpc` -c -o test.o test.cpp
>>> g++ test.pb.o test.grpc.pb.o test.o -L/usr/local/lib `pkg-config --libs >>> protobuf grpc++` -lgrpc++_reflection -ldl -o test
>>> rm test.grpc.pb.cc test.pb.cc

Related

g++ error while compiling C++ application that used Mono API

I am compiling my C++ application and want to link it to mono. I am using OpenSusE. I have install mono-complete and glibc-devel-static packages. I am compiling as below -
$>g++ --static monoapp.cpp `pkg-config --cflags --libs mono` -I /usr/include/mono-2.0
/tmp/ccGfpBjX.o: In function `Launch(char const*, char const*, int, char const* const*, char const*)':
monoapp.cpp:(.text+0x23e): undefined reference to `mono_set_dirs'
monoapp.cpp:(.text+0x24f): undefined reference to `mono_jit_init_version'
monoapp.cpp:(.text+0x28b): undefined reference to `mono_domain_assembly_open'
monoapp.cpp:(.text+0x2c2): undefined reference to `mono_jit_exec'
collect2: error: ld returned 1 exit status
UPDATE 1 - I tried giving -lmono to linker but it complains about -lmono not found. Below is output of locate command.
$>locate libmono
/usr/lib64/libmono-2.0.a
/usr/lib64/libmono-2.0.so
/usr/lib64/libmono-2.0.so.1
/usr/lib64/libmono-2.0.so.1.0.0
Above output clearly shows that libmono is present.
UPDATE -2 Output of pkg-config is empty
$>pkg-config --cflags --libs mono
$>
UPDATE -3- I tried setting up PKG_CONFIG_PATH so that it can find mono.pc file but still it did not work
Please help.
Thanks,
Omky
I solved this by providing full path to static lib as -
g++ --static monoapp.cpp pkg-config --cflags --libs mono -I /usr/include/mono-2.0 -L /usr/lib64/ -lmono

Linking g++ OS X Mavericks

I'm trying to compile a test file which uses the Cmage library (http://cimg.sourceforge.net).
I already generated the .o file but apparently this file uses the X11 Library and I'm having some problems when linking.
If I do this:
g++ -o test CImg_demo.o
I get this:
Undefined symbols for architecture x86_64:
"_XAllocClassHint", referenced from:
cimg_library::CImgDisplay::_assign(unsigned int, unsigned int, char const*, unsigned int, bool, bool)in CImg_demo.o
"_XCheckMaskEvent", referenced from:
cimg_library::CImgDisplay::_events_thread(void*) in CImg_demo.o
etc, etc
If I do this:
g++ -o test CImg_demo.o -lX11
I get this:
ld: library not found for -lX11
collect2: ld returned 1 exit status
As my OS X is mavericks, X11 has been installed with XQuartz in /opt/X11/lib/
So I tried to add that directory where all the .dylib files are at the beginning of the line, like this:
g++ -L/opt/X11/lib/ -o test CImg_demo.o
but I get exactly the same references problem.
Does anyone know how to find out the directory that includes all the object files to correctly make the linking?
Thank you in advance.

How to run hello world FUSE program on Xcode

I would like to know the configurations to run this Hello World program on Xcode. I have installed MacOSXFUSE.
With the command
gcc -Wall hello.c `pkg-config osxfuse --cflags --libs` -o hello
and then with
./hello outputdrive -f -s
I am able to successfully mount the drive outputdrive.
I have environmental variable declare set as -x PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
MacOSXFUSE is installed in /usr/local/include/osxfuse/
More information:
/usr/local/lib/pkgconfig/osxfuse.pc
data in this file is
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: fuse
Description: OSXFUSE
Version: 2.7.3
Libs: -L${libdir} -losxfuse -pthread -liconv
Cflags: -I${includedir}/osxfuse/fuse -D_FILE_OFFSET_BITS=64 -D_DARWIN_USE_64_BIT_INODE
A little progress,
I did this on terminal,
pkg-config osxfuse --cflags --libs
Pasted the below result in "other c flags" in xocde
-D_FILE_OFFSET_BITS=64 -D_DARWIN_USE_64_BIT_INODE -I/usr/local/include/osxfuse/fuse -L/usr/local/lib -losxfuse -pthread -liconv
Now I get the below error,
Undefined symbols for architecture i386:
"_fuse_main_real", referenced from:
_main in main.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Please let me know if more details is required.
You need to put the /usr/local/lib in the "Library search paths" and the library "libosxfuze" to the "Linked Frameworks and Libraries".
does it compile from command line?
g++ hello.c -I/usr/local/include/osxfuse/fuse -D_FILE_OFFSET_BITS=64 -v -L/usr/local/lib -losxfuse

Mono mkbundle on OSX

I'm trying to create a standalone of my C# app using mono's mkbundle, I got Xcode installed and the Mono Developer Kit too (I'm sure it's MDK not the runtime). Yet I run mkbundle using
mkbundle test.exe
and I get these errors
Compiling:
as -o temp.o temp.s
cc -g -o a.out -Wall temp.c `pkg-config --cflags --libs mono-2` temp.o
sh: pkg-config: command not found
temp.c:1:10: fatal error: 'mono/metadata/mono-config.h' file not found
1 error generated.
[Fail]
What's happening?
pkg-config is stored at '/Library/Frameworks/Mono.framework/Commands'.
Solution (see here and here):
Prepend the "/Library/Frameworks/Mono.framework/Commands" folder to
your PATH variable:
export PATH=/Library/Frameworks/Mono.framework/Commands:$PATH
This is needed in addition to the architecture solution proposed by aiapatag and the objective-c runtime and CoreFoundation framework solution:
export AS="as -arch i386"
export CC="cc -arch i386 -framework CoreFoundation -lobjc -liconv"
It looks like the pkg-config tool is not found. Maybe it's not in the default paths.
Do you have a 'pkgconfig' directory somewhere? It should be a subdirectory of your Mono installation.
Try to see if you have a path looking like /Library/Frameworks/Mono.framework/Versions/XXXX/lib/pkgconfig
If yes, point the PKG_CONFIG_PATH environment variable to this path, you can specify it directly when running your mkbundle command (this is just an example):
$ PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/XXXX/lib/pkgconfig mkbundle ....

Libxml2: undefined reference to xmlTextReaderConstName

I have installed the latest libxml2-2.8.0, as usual: $ ./configure, $ make, $ make install.
The $ xml2-config --cflags --libs gives this output:
-I/usr/local/include/libxml2
-L/usr/local/lib -lxml2 -lm
But trying to compile any example...
$ gcc `xml2-config --cflags --libs` xmltest.c
The linker says:
/tmp/cc8ezrPl.o: In function `processNode':
xmltest.c:(.text+0x19): undefined reference to `xmlTextReaderConstName'
xmltest.c:(.text+0x38): undefined reference to `xmlTextReaderConstValue'
...etc.
Anything I've googled can be solved by xml2-config --cflags --libs flags, or upgrading to the latest version of libxml2, or something. Unfortunately, neither works for me.
What can be the steps to identify the problem?
Using Ubuntu 12.04 64-bit.
The libraries should be specified only after the source file so that the linker can resolve the undefined references in the source file. Try compiling the example with this
gcc -I/usr/local/include/libxml2 -L/usr/local/lib xmltest.c -lxml2 -lm

Resources