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

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

Related

gRPC giving linkage errors in c++

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

How to link Libbluetooth for GCC?

I am trying to use hci.h, hci_lib.h etc... in my c files. The final command to link the object files into exec look like this:
gcc -g3 -o execfile ./dev1.o ./dev2.o ./main.o ./nets.o `pkg-config --cflags --libs glib-2.0 gio-unix-2.0`
However I am getting linking error like this:
undefined reference to `hci_open_dev'
undefined reference to `hci_le_set_scan_parameters'
undefined reference to `hci_le_set_scan_enable'
undefined reference to `hci_le_set_scan_enable'
undefined reference to `hci_close_dev'
I know that this is because I am not specifying -lbluetooth to gcc.
Is there a way to specify the libbluetooth-dev to pkg-config like I do for glib & gio-unix above?

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.

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

Linking the static version of a library instead of the dynamic one

I am trying to use libjpeg in my application. Making the project yields a libjpeg.a in .libs folder. What I would like to do is to use this file in the linkage stage. I have tried the following: I copied the libjpeg.a into the folder where my C code resides. Trying to link with
gcc libjpeg.a mycode.c -o executable_name
fails. If I do gcc -ljpeg mycode.c, the compilation is successful when I change my header to point to instead of "libjpeg.h", but this obviously links to the system-wide dynamic version of the library.
Trying to link with relative or absolute path also fails:
gcc ./libjpeg.a mycode.c -o executable_name
I have tried the static option as well:
gcc -static libjpeg.a mycode.c -o executable_name
The linker error is the following:
Linking...
gcc -std=c99 -Wall -Wextra -g -pedantic ./libjpeg.a ./libjpeg.a -lm obj/read_jpeg.o obj/utils.o -o test_jpeg
obj/read_jpeg.o: In function `read_JPEG_file':
/home/ustun/Downloads/jpeg_test/read_jpeg.c:37: undefined reference to `jpeg_std_error'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:45: undefined reference to `jpeg_CreateDecompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:46: undefined reference to `jpeg_stdio_src'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:47: undefined reference to `jpeg_read_header'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:48: undefined reference to `jpeg_start_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:62: undefined reference to `jpeg_read_scanlines'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:74: undefined reference to `jpeg_finish_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:75: undefined reference to `jpeg_destroy_decompress'
obj/read_jpeg.o: In function `read_JPEG_file_props':
/home/ustun/Downloads/jpeg_test/read_jpeg.c:93: undefined reference to `jpeg_std_error'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:100: undefined reference to `jpeg_CreateDecompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:101: undefined reference to `jpeg_stdio_src'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:102: undefined reference to `jpeg_read_header'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:103: undefined reference to `jpeg_start_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:113: undefined reference to `jpeg_read_scanlines'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:116: undefined reference to `jpeg_finish_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:117: undefined reference to `jpeg_destroy_decompress'
collect2: ld returned 1 exit status
make: *** [test_jpeg] Error 1
You can download a simple project with a Makefile here.
You'd have to give the full path to libjpeg.a, If you have libjpeg.a in a .libs folder relative to where you compile:
gcc mycode.c -o executable_name .libs/libjpeg.a
If your special libjpeg.a is elsewhere, give a full path to it.
If this fails, you have to tell us what happens. (the details are important, so please copy paste the exact errors and the exact command line that is run).
You need to use -static:
gcc -static -o exec_name mycode.c -ljpeg
No need to copy the archive (.a). You could have found out by reading man ld.
This may help you if you have the same problem as mine. In my system, I have
libjpeg.so.62 -> libjpeg.so.62.0.0
libjpeg.so.62.0.0
After I added symbolic link:
sudo ln -s libjpeg.so.62 libjpeg.so
My problem got solved.

Resources