Linking protobuf library with code (Google protocol buffers) - linker

I am getting linking error when I try to compile a test code.
I'm using cygwin on windows 7.
Initial steps like ./configure, make, make test & make install went fine
I'm also able to generate .pb.cc and .pb.h with protoc command.
But when I try to compile my test code, it gets many linking errors. I'm sure those errors are because it is unable to link to library.
Cygwin has protobuf static library and linking library in /usr/local/lib
. include files are present in /usr/local/include
I tried with -lprotobuf, but it returns error saying -lprotobuf not found

It hard to say what the problem is since you don't include neither the makefile nor the errors, but my guess is that the path /usr/local/lib is not included in the search path when looking for libraries.
Try adding -L/usr/local/lib before -lprotobuf.

Related

Eliminate dependency on MinGW-specific DLLs when compiling dynamic library

I am using msys2 to compile a library that uses autotools as the build system. The end result is a DLL. This DLL ends up referring to the following other DLLs that come with msys2:
libgcc_s_seh-1.dll
libstdc++-6.dll
libwinpthread-1.dll
How can I link these statically and eliminate these dependencies?
There are other questions dealing with this (example) and the solutions suggest using the options -static-libgcc -static-libstdc++. These work when linking an .exe, but they do not seem to work when linking a .dll.
I set the following variables before running ./configure (and checked the output to verify that these compiler options are really being used), but Dependency Walker still shows a dependency on libstdc++-6.dll, just as before.
export CFLAGS="-static-libgcc -static-libstdc++" CXXFLAGS="-static-libgcc -static-libstdc++" LDFLAGS="-static-libgcc -static-libstdc++"
(I assume these must only go in LDFLAGS, but since I don't have a full understanding, I also added them in CFLAGS and CXXFLAGS.)
Is there a way to get rid of these dependencies when linking a DLL, not an EXE?
The library is written in a mix of C and C++ and has a C API.
Try just using the -static option in LDFLAGs. I tested it just now in MSYS2 and it worked for me.

Multible definitions including open source library (with seemingly correct linker settings?)

Im working on a program for my studies, that uses an open source library. It is meant to run on Raspberry Pi (Raspbian Kernel). Because of my intention to be also able to load it on a PLC i used mostly pure C. The Library itself comes with suitable header and .c files.
When i use the pre installed GCC compiler on the Raspberry my program compiles without any errors and works fine. Now here comes my problem:
I tried to get this projekt to work on windows using code::blocks IDE with MinGW installed. I revisited the library and downloaded the zip for windows (apperently same header and .c file, but also .lib and .dll included).
I set the search directories and linker settings within the project and included the header as usual with #include "header.h" for the relative path. It doesn't compile and gives alot multible definition and first defined here which usually indicate wrong linking and inclusion.
As i tried to identify some of this definitions i noticed that the functions which cause errors are defined one time in the library.c file. At the beginning of this .c file it also includes the header one time.
Short summary:
This works with raspbian GCC:
$ gcc -sdt=c99 main.c library.c -o executable
but gives errors with Windows IDE + MinGW
Am i missing something serious? The dynamic link lib should only be used by the executable afterwards. I thought maybe the libraby.c gets replaced by the library.lib but if i remove one of them the project doesn't know the functions. I also searched for wrong inclusions. I'm really at the end of my knowledge here, and also searched for posts that would help me, but those were mostly "where is the linker path" or "inclusion of .c files". It seems so an simple problem which i overlooked.
Any help Would be appriciated. I will supply more details if needed.
Thanks!
Edit (2):
obj\Debug\open62541.o:open62541.c:(.text+0x3152a): undefined reference to `__imp_shutdown'
obj\Debug\open62541.o:open62541.c:(.text+0x3153f): undefined reference to `__imp_closesocket'
obj\Debug\open62541.o:open62541.c:(.text+0x315a7): undefined reference to `__imp_send'
obj\Debug\open62541.o:open62541.c:(.text+0x315b9): undefined reference to `__imp_WSAGetLastError'
.....
Edit (3)
Answer 1!
Compiled good now, thanks everyone.
It looks like your library is open62541.
There are two ways to include the library in your source:
Build a shared/static lib and link it to your code
Enable Amalgamation which generates a single .c and .h file which you can directly compile with your code
You are combining both methods on mingw which adds the whole library two times.
Probably you only want to link the .c file without the .lib, thus your compile command should look something like this:
gcc -sdt=c99 main.c open62541.c -o test
Additionally, since open62541 needs the ws2_32 library on windows, the compiler should be called with:
gcc -std=c99 main.c open62541.c -o test -lws2_32

using i686-w64-mingw32-g++ for static libraries

I have a JNI project, which I have to make work on Windows (I am working on Linux). This project actually depends on third-party library file which is static (archived i.e .a files). I am trying to create a JNI shared library file using i686-w64-mingw32-g++ and including -static followed by static third-party library name. Following is the command I am using
i686-w64-mingw32-g++ -v -L./ -L/home/user/jre1.8.0_40/lib/amd64/ -I/user/all/apps/Linux2/x86_64/gcc/4.8.2/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2/include -shared -o test.dll test.cpp -lstdc++ -static -thirdparty
In-spite of placing the third party library in the current working directory, I keep getting error
/user/all/apps/Linux2/src/mxe/2013_12_03/usr/bin/../lib/gcc/i686-w64-mingw32/4.8.1/../../../../i686-w64-mingw32/bin/ld: cannot find -thirdparty
Please note : I included -I/user/all/apps/Linux2/x86_64/gcc/4.8.2/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2/include to avoid the error cannot find jni.h which I hit before including the path.
I also tried to compile using gcc, in place of g++.
Do I need to create .dll of this third-party library(currently it is archived .a containing .obj files)?
Being a newbie in cross compilers, I might be doing something wrong. Please correct me and any suggestions with this will be very helpful. Thank you.
-Wl,--export-all-symbols -Wl,--add-stdcall-alias -v adding this solved my problem

Statically Linking glib

I'm trying to statically link glib into my C program. I'm not sure what's the best way to do this. I downloaded the code and put it in a subdirectory called glib-2.36.4. I added "-Iglib-2.36.4" when using gcc. The glib.h is in the glib-2.36.4/glib directory and in that file there are references to other header files under the glib directory (such as #include ).
I'm not sure why that is since both glib.h and these other header files are at the same level (in glib subdirectory). I got a compile error due to galloca.h not being found (even though it's there). So I copied glib.h up one level and those errors went away. I then got an error about a missing glibconfig.h. I copied that from my usr directory and that error went away. I compiled my project and now I'm getting an error about undefined reference to g_ptr_array_new. I guess this must be because I haven't actually compiled glib. I had tried to build glib, but when I typed "./configure", but I got this message:
checking if arpa/nameser_compat.h is needed... configure: error: could not compile test program either way
I did install glib using yum, but I really want this code to run even if glib is not installed on a machine.
You need to install both glib and glib-dev via yum, compile using ./configure, (take a look in the ./configure script to see if there are any flags you need to supply or defines you need to produce the static build), without moving any files about, and then you need to compile your code using -i path/to/glib/includes and link with -L path/to/built/static/library

Can't Link Psapi.h in Open Source Project Using MinGW

I am trying to modify the source code to an open source application on windows that uses mingw.
I am having a problem linking the psapi library.
psapi.h and libpsapi.a are in the mingw directory and I have tested it using the standard
gcc -o program program.c -lpsapi
method, and it works.
However, when I try to compile the program using the
./configure
make
method, it doesn't work I have tried,
./configure LDFLAGS=-lpsapi
make
and that doesn't work
and I tried going into the makefile.am and putting -lpsapi in AM_LDFLAGS but that doesn't work
The error it gives is just a standard "undefined reference to [function]", implying that the library with the functions is not linked
I have even tried putting psapi.h in the source directory and including it as #include "psapi.h" and that STILL didn't work.
LDFLAGS is the wrong one.
LIBS=-lpsapi should do the trick.
The order is important for the linker. The libraries (LIBS) have to come after the objects, LDFLAGS can be before.

Resources