Installing Protobuf Development Libraries in MinGW - c++

I installed Protobuf in MinGW from the sources on github.
When I try to compile my C++ program I get errors:
CMakeFiles/nxcore_interface.dir/main.cpp.o:main.cpp:(.rdata$.refptr._ZN6google8protobuf8internal13empty_string_E[.refptr._ZN6google8protobuf8internal13empty_string_E]+0x0): undefined reference to `google::protobuf::internal::empty_string_'
collect2: error: ld returned 1 exit status
I found something that indicates the development libs are not present:
Program with protocol-buffers don't compile with MinGW-w64: "undefined reference to google::protobuf:: ..."
I have included the -lprotobuf compiler flag.
After some searching I determined I need to use libprotobuf-dev but I am having trouble locating it.
Does anyone know where to get it, or is something else wrong?

You should compile your application with pkg-config.
g++ my_program.cpp `pkg-config --cflags --libs protobuf`
If you don't have pkg-config you should locate libraries path and set them with -L option. Eg:
g++ my_program.cpp -L/usr/local/protobuf/lib -lprotobuf

Related

Can't link GLFW library with it's header file on Ubuntu 18.04

I've installed the libglfw3-dev:amd64 package on Ubuntu using the standard sudo apt get etc. My following compiling line is:
g++ -o output -IL/usr/lib/x86_64-linux-gnu -lglfw driver.o
My current c++ file is:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
if (!glfwInit())
return -1;
}
I've tried using local libraries of glfw and setting the -I and -L locations but nothing has seemed to work. I've made sure the .so and .h files are in their respective locations but I always get this error while running make:
g++ -o output -I/usr/include/GLFW -L/usr/lib/x86_64-linux-gnu -lglfw
driver.o
driver.o: In function `main':
driver.cpp:(.text+0x5): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'output' failed
make: *** [output] Error 1
I've tried looking at all the other SO posts and they recommend compiling with tons of extra flags, but the only thing I've been able to draw from them is that something is wrong with my library since VScode detects the .h files. How can I compile this without any errors?
Have you tried swapping the linker arguments around? That is, compile with
g++ -o output driver.o -lglfw
The linker goes through the files from left to right, and it has to know which symbols from libraries you need, before the libraries are processed.
All is perfectly explained in the manual https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig
The key problem is in your -I/usr/include/GLFW and #include <GLFW/glfw3.h> that gives in sum the path /usr/include/GLFW/GLFW/glfw3.h. I suppose this is a wrong path to glfw3.h. compilation was successful because of the system default include path -I/usr/include.
Do not tune compiler flags manually, let pkg-config do
it for you.
A typical compile and link command-line when using the static version of the GLFW library may look like this:
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --static --libs glfw3`
If you are using the shared version of the GLFW library, simply omit the --static flag.
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --libs glfw3`

How to link libgomp statically when linking other libraries dynamically?

I am trying to build an image processing program written in C++ that depends on the following libraries using MinGW + MSYS (with GCC4.8.1) that I downloaded from www.mingw.org/ on a Windows 8 64bit computer
LibJPEG
BLAS and LAPACK
Armadillo
OpenMP
I have successfully compiled all the source code files (with -fopenmp flag of course), then I linked with the following statement:
g++ -o ./build/rspfitter {a list of .o files}
-L{paths to libraries} -ljpeg -lopenblas -lgomp -lpthread
The executable was correctly produced. However, it asks for the following dlls:
libgomp-1.dll
libpthread-2.dll
pthreadGC2.dll
I think it might a better idea to link libgomp and libpthread statically, so that I can minimize the number of dlls I need to deploy my program with (the above three dlls are not the only ones the program depends on). So I tried to link only libgomp and libpthread statically with the following command:
g++ -o ./build/rspfitter {a list of .o files}
-L{paths to libraries} -ljpeg -lopenblas -Wl,-static -lgomp -lpthread
But this time it fails with the following error message:
d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o):(.text.startup+0xbfe):
undefined reference to `_imp__pthread_attr_init'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o):(.text.startup+0xc13):
undefined reference to `_imp__pthread_attr_setdetachstate'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o):(.text.startup+0x3c):
undefined reference to `_imp__pthread_attr_setstacksize'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o): bad reloc
address 0x0 in section `.ctors'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
final link failed: Invalid operation
Then I tried to execute the exact same compiling and linking commands using the MinGW + GCC 4.8.1 environment that was installed together with CodeLite. It fails again with different error messages:
./tmp/hshfitcmdline.o:hshfitcmdline.cpp:(.text.unlikely+0x105):
undefined reference to `_Unwind_Resume'
./tmp/hshfitcmdline.o:hshfitcmdline.cpp:(.text$_ZN9NormalMapD1Ev[__ZN9NormalMapD1Ev]+0xb4):
undefined reference to `_Unwind_Resume'
d:/mingw-4.8.1/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
./tmp/hshfitcmdline.o: bad reloc address 0xb4 in section
`.text$_ZN9NormalMapD1Ev[__ZN9NormalMapD1Ev]'
d:/mingw-4.8.1/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
final link failed: Invalid operation collect2.exe: error: ld returned
1 exit status make: *** [build/rspfitter] Error 1
I confirmed that the file "libgomp.a"/"libgomp.dll.a" was present on the [MinGW dir]/lib/gcc/mingw32/4.8.1/ for both installations of MinGW. However, they are of different sizes! In installation downloaded from MinGW.org, 'libgomp.a' is of 86kb, and "libgomp.dll.a" is of 87kb; In the CodeLite installation, however, the sizes are 74kB and 148Kb respectively.
Now I am wondering:
What could be the cause of the error messages given by the two MinGW systems? Could it be that the statically libraries that I downloaded from MinGW are corrputed? But dynamically linking was perfectly fine on both systems.
How on earth can I correctly link libgomp statically?
Thanks
To link libgomp statically you can do
ln -s `g++ --print-file-name=libgomp.a` && \
g++ foo.o -static-libgcc -static-libstdc++ -L. -o foo -fopenmp -ljpeg -lopenblas
However your executable will still depend on a pthread dll. The reason you are getting the error is that libc is still linking dynamically. To fix this you have to link libc statically as well
ln -s `g++ --print-file-name=libpthread.a` && \
ln -s `g++ --print-file-name=libc.a` && \
g++ foo.o -static-libgcc -static-libstdc++ -L. -o foo -fopenmp -ljpeg -lopenblas
However, if openblas or jpeg libraries depend on libc then there will likely still be undefined references.

C++: linker cannot find -lcrypto, but the library is in the path

I am compiling a C++ application using GNU g++. The project takes advantage of OpenSSL libraries.
Background
On my machine (a 64 bit CentOS quad core) I compile and link my files.
g++ -g -c -L/usr/local/lib/ -L/usr/lib64/
-I/usr/local/include/ -I/usr/local/ssl/include/
-lcrypto mysrc1.cpp mysrc2.cpp mysrc3.cpp
g++ -L/usr/local/lib/ -L/usr/lib64/ -lcrypto
*.o -o ./myapp.out
My application uses function MD5 which is contained in libcrypto.so. As you can see I specify to g++ the dirs where to search using the -L, -I options and which libraries to look for with the -l<lib-name> option. There are some trivial paths like /usr/local/lib which can be omitted of course, but I specified them because the makefile is parametric.
The problem
My problem is that I can successfully compile my stuff (first command), but linking fails (second command):
/usr/bin/ld: cannot find -lcrypto
collect2: ld returned 1 exit status
make: * [cppsims_par] Error 1
But I did check folders and everything... libcrypto.so is inside /usr/lib64/. What is going on?
It may help if you try strace to find why it failed the file lookup
strace -f -e trace=file g++ -L/usr/local/lib/ -L/usr/lib64/ -lcrypto
*.o -o ./myapp.out
I did find the problem and it is related to this question: ld cannot find an existing library
Actually I had no symlink libcrypto.so and the compiler was not able to find the library...
I had related issue, and resolved it after inspecting the trace.
I had
-L<my/path/to/lib> -llib_some_library
when it should have been
-L<my/path/to/lib> -lsome_library

How to tell ld where to find libc

So I created a static library with some basic assistance functions in C++ today. I built it with Clang 3.2 (SVN snapshot). However when I try to run a test program that links to it (prog.cpp), I get the following error:
~/Projects/CPP/AssisterLib> g++ prog.cpp -o program -static -L. -lassister
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -lm
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
I get the same error with G++ and Clang++. Apparently it can't find libc.a and libm.a, which are both in /usr/lib64 (provided by glibc-devel in OpenSUSE). Adding -L/usr/lib64 does nothing for the error.
Why can't ld find those libraries? Is there a flag that I'm missing?
The problem is likely the use of -static. I would conclude you do not have static version of the libm and libc installed. You can try removing -static to confirm.
The -static flag signals to the compiler that you want your executable to be entirely statically linked, and so it fails if not all the libraries have static versions available.

OpenCV on ubuntu 11.10

I've just updated my system from ubuntu 11.04 to 11.10 and now I can't compile anymore any C program that contain references to OpenCV libraries
I've already tried to reinstall OpenCV (I use the 2.1 version) but I'm stuck with this error:
/tmp/ccArHTZL.o: In function `main':
z.c:(.text+0x59): undefined reference to `cvLoadImage'
z.c:(.text+0xa0): undefined reference to `cvNamedWindow'
z.c:(.text+0xb1): undefined reference to `cvShowImage'
z.c:(.text+0xbb): undefined reference to `cvWaitKey'
z.c:(.text+0xc5): undefined reference to `cvDestroyWindow'
z.c:(.text+0xd1): undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
In order to install OpenCV I've always followed this procedure:
$ sudo apt-get install libcv2.1 libcv-dev libcvaux2.1 libcvaux-dev libhighgui2.1
libhighgui-dev opencv-doc python-opencv
$ export LD_LIBRARY_PATH=/home/opencv/lib
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig
$ pkg-config --cflags opencv
-I/usr/include/opencv
$ pkg-config --libs opencv
-lcxcore -lcv -lhighgui -lcvaux -lml
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.c
Anyone can help me?
Why don't you use pkg-config to your favor?
g++ hello.c -o hello `pkg-config --cflags --libs opencv`
I think it is because of some changes from gcc 4.5 to gcc 4.6
Try this command instead (i.e., move the libraries to the end, instead of at the beginning of your command line) -- it works for me:
g++ -I/usr/include/opencv hello.c -lcxcore -lhighgui -lm
I'm still on kubuntu 10.10 so I'm not really familiar how does 11.10 work, but the most common answer to problems with not finding libraries is to use ldconfig with sudo. It'll refresh libraries database. If that doesn't help, look into /usr/lib, /usr/lib64 and /usr/lib32, because its the default place where apt-get throws libraries in. When you find the libraries, change the LD_LIBRARY_PATH so it contains the directory. I don't think that /home/opencv/lib is where they are, but i don't know Your environment
I just upgraded to 11.04 on my laptop and having similar issues. I would try building the latest version of OpenCV (2.3.1) and see if this fixes anything, this seemed to fix quite a few issues for me.
Use the following command, it worked for me:
gcc pkg-config --cflags opencv opencv.c -o open_cv pkg-config --libs opencv