Building the protobuf library as a static library - build

Is there a elegant way to build the protobuf library as a static library with bazel?
If not then not is there a way to build in bazel and get the absolute path of the so for linking?

You can build a library as static using the linkstatic option. See: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.linkstatic
As for linking, you should let Bazel generate the linker flags for you by adding the Protobuf library to the deps list. See: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.deps

Related

How to build static libraries and linkthem to a c++ project in linux platform?

I am working on a text-classification project, which is big and doesn't use bazel as its build tool. I want to integrate tensorflow into my project, but I find it is hard to change my build tool to bazel. So I wish to build static libraries on tensorflow and link them into my project.
Does anyone know how to build standalone static libraries on tensorflow source and link them in the existing c++ project? Thanks a lot.
The TensorFlow repository has some Makefiles you can use to build a static library (see tensorflow/contrib/Makefile).
Alternatively, you could use bazel to build the TensorFlow C++ shared library and then load and use the shared library in your application (bazel build -c opt //tensorflow:libtensorflow_cc.so). Unfortunately, bazel can't yet produce a static library (#1920).
Hope that helps.

Linker is looking for the static libraries that was linked to my own static library

I have a static library that links to a statically built Boost library.
However, when my executable is linking to my own static library, it will fail with a message saying that it cannot find the Boost library.
I thought when my static library is statically linked to the Boost library, any further upstream dependency of the Boost library should be gone. Isn't this correct?
MyLib.lib -> linked to static Boost library
MyApp.exe -> links to MyLib.lib statically, Failed!, cannot find Boost library
Any help is appreciated.
Static libraries are really nothing more than a collection of object files. If you want to use a static library that depends on other libraries, you need to link withe other libraries as well.

Link poco static library to target in cmake

I read a lot of post but I don't yet well understood how to link my target to a static version of a library.
My project depends on poco libraries and, while in linux they are stored in /usr/local/lib (both the static and shared versions) in my windows machine are in d:\libs\poco\lib and d:\libs\poco\bin (where I have an enviroment variable called POCO_DIR = D:\libs\poco)
So, how can I have to write the find_library() directive in cmake file?
Thanks in advance.
You never link with a Poco DLL on windows, not even when you use shared Poco libraries. Linking is always with entries in the %POCO_BASE%/lib. For shared builds, .lib is just a stub ("import library") that takes care of loading the DLL at runtime. See Linking Implicitly for details on how this works.
Poco static libraries can be distinguished from the import libraries for DLLs by the file name - static libs have "mt" ("mtd" for debug binaries, "md" and "mdd" when runtime library DLLs are used) appended to the name. So the import library for PocoFoundation.dll will be named PocoFoundation.lib, while the static library using static runtimes is named PocoFoundationmt.lib. Static library using dynamic runtimes is PocoFoundationmd.lib. See Use Run-Time Library for details.
As for CMake, I am not an expert, but for e.g. static Foundation and Net libraries should be something like this:
FIND_LIBRARY(Poco_LIBRARIES NAMES PocoFoundationmt PocoNetmt PATH_SUFFIXES ${POCO_DIR}/lib)
EDIT: If you define POCO_STATIC in your project, static linking will be automatic through library headers, see e.g. Foundation.h.

Why doesn't Libtool want to link with a static library?

I want to build a shared library that uses ZipArchive using GNU Autotools but I'm having this problem:
Warning: linker path does not have real file for library -lziparch.
I have the capability to make that library automatically link in when
you link to this library. But I can only do this if you have a
shared version of the library, which you do not appear to have
because I did check the linker path looking for a file starting
with libziparch and none of the candidates passed a file format test
using a file magic. Last file checked: /usr/local/ZipArchive/ZipArchive/libziparch.a
The inter-library dependencies that have been dropped here will be
automatically added whenever a program is linked with this library
or is declared to -dlopen it.
If I build a static library or if I use a shared library of ZipArchive it works but the problem is that the makefile that comes with ZipArchive source code only builds a static library.
How can I force Libtool to link with a static library?
Generally, static archives are created with non-pic object files and they cannot be put into shared libraries.
What this message is telling you though, is that when a program links to YOUR library using Libtool, that -lziparch will get added to the link. So you don't need to change anything unless you're building a module for an interpreted language. In that case, you will have to build ZipArchive as a shared library. In addition, this wouldn't work on a platform like MS Windows where shared libraries (DLLs) have to resolve all their symbols at link time.
All that said, if your ziparch static lib is PIC code, you can use the -whole-archive flag when linking it to your library. This would be the least portable solution though.

Linking a dynamic library to a static library that links to other static libraries

In my C++ application I have a static library (libCOMMON.a) that links to boost libraries: system, filsystem, timer and chrono.
I am using CMake and here is how I create and link libCOMMON.a:
ADD_LIBRARY(COMMON ${COMMON_SRCS})
target_link_libraries(COMMON
${BOOST_LIB_DIR}/libboost_filesystem.a
${BOOST_LIB_DIR}/libboost_system.a
${BOOST_LIB_DIR}/libboost_timer.a
${BOOST_LIB_DIR}/libboost_chrono.a
)
I also have plugins for this application that links to libCOMMON.a. The plugins are built as dynamic libraries. Everything compiles ok (using gcc) but when I start the application, the plugins can't be loaded because some symbols in the dynamic libraries related to boost cannot be resolved.
The solution was to link each of the plugins to boost. Is there a better way ?
I thought that if boost libraries are linked statically into libCOMMON.a, it would be enough to link the plugins to libCOMMON.a.
Can someone explain what's happening ?
Thanks
I think the problem is that boost libraries are built as dynamic libraries by default. Even if the ".a" suggests that they are built as static libraries, the lib folder of boost contains a ".so" library with each ".a". Which means that libCOMMON.a is linked dynamically to boost libraries. For this reason, the plugins that statically links to libCOMMON.a has also to dynamically link to boost libraries. A better solution would be to build boost libraries as static libraries.