-1

I compiled OpenCV 3.x with the Static Libraries options (-DBUILD_SHARED_LIBS=OFF).

No I have a project built with few functions which uses OpenCV.
I want to build my project as a portable static library.
By portable I mean I can share it with others to use my functions in their code without the need to install OpenCV.

How can I do that on Windows, macOS and Linux?
I would like the process to be done using a Compiler in order to extract only the needed objects (Functions) from all OpenCV libraries.

How is it different from other questions on the subject?
The difference in my question versus the generic answer of archiving multiple static libraries into one is the selection of which functions to include.

Let's say we have 2 static libraries - lib001.lib and lib002.lib.
Where lib001.lib has the functions fun001(), fun002() and fun003() and lib002.lib has fun004(), fun005() and fun006().

Let's assume the functions has dependency which I'm not aware of (I didn't write the libraries). So in my project I use fun001() and fun005() and let's say fun001() depends on fun004() and fun005() depends on fun002().

What I'd like is the linker to understand this and build me a custom static library which includes my functions and fun001(), fun002() fun003() and fun004() without me telling the dependency.

Remark
Above I assume each object in the static library is a function. One could replace the use of functions by objects to be more accurate.

Royi
  • 4,640
  • 6
  • 46
  • 64
  • If you don't want to ship OpenCV with your library, then you need to combine OpenCV library and your one into the single library. See [that question](https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake) about the ways for doing that. – Tsyvarev Sep 14 '19 at 21:33
  • 1
    Possible duplicate of [Combining several static libraries into one using CMake](https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake) – S.S. Anne Sep 14 '19 at 21:37
  • What about doing it at Windows? Also, I’d like it to be done by a Compiler so only relevant functions are extracted and copied into a new library. – Royi Sep 14 '19 at 21:52
  • For Windows see [that question](https://stackoverflow.com/questions/13492365/how-to-merge-two-windows-vc-static-library-into-one). You actually ask **several questions** in one (how to do something on Windows, on macOS, on Linux) and expect that single, **universal answer** covers all these questions. Unfortunately, this is not a case: all we have is *separate* approaches on different platforms. So your question is just "too broad" – Tsyvarev Sep 15 '19 at 07:35
  • @Tsyvarev, I'm not asking about the archiving process of 2 static libraries into single library. I want the compiler to the work and select the relevant objects from each static library. What I use is a sub set and I want the compiler to decide on this sub set according to the functions used in my project. – Royi Sep 15 '19 at 14:52
  • @JL2210, See my updates to the question. I tried explaining why my question is different. – Royi Sep 15 '19 at 15:00
  • "What I'd like is the linker ..." - The linker isn't involved when making **static** libraries. A static library is just an archive of object files. – Tsyvarev Sep 15 '19 at 15:22
  • I'm sorry if I'm not accurate. Which tool can archive into single library only the functions / objects needed in my project and not the whole set of static libraries? – Royi Sep 15 '19 at 15:28
  • 1
    What you're trying to do is impossible. Just use shared libraries if you want something remotely like this (or make OpenCV a dependency and use some sort of pkgconfig-like system). – S.S. Anne Sep 15 '19 at 15:52
  • Why is that? Let's say you have an ensemble of object files, Linker knows which one are relevant and assemble them. So in the compilation process there is a stage the linker does something to understand what's needed and what's not. – Royi Sep 15 '19 at 16:24
  • "Which tool can archive into single library only the functions / objects needed in my project and not the whole set of static libraries?" - Assuming you ask about creation of **static** library I know no such tool. It could be that such tool exists, but it is not one known-for-everyone. – Tsyvarev Sep 15 '19 at 17:02
  • OK, Could we be creative, create EXE which can act as a static library or something like that? – Royi Sep 15 '19 at 17:08
  • @Tsyvarev, What about the trick in https://stackoverflow.com/questions/10129986? – Royi Sep 16 '19 at 17:32
  • @JL2210, For Windows, what about the option [`/WHOLEARCHIVE`](https://learn.microsoft.com/en-us/cpp/build/reference/wholearchive-include-all-library-object-files?view=vs-2019)? – Royi Sep 16 '19 at 18:19
  • @Royi I don't know. I never use Windows. – S.S. Anne Sep 16 '19 at 18:49
  • @Royi: Using the "trick" you will get resulted static library containing **all objects** from the original libraries. Note, only the **linker** can filter object files and drop unneeded ones. But creating static libraries (on Linux) doesn't involves the linker call. I am not an expect in compilation process on Windows, but `/WHOLEARCHIVE` smells like usual creating **shared** library, that is the linker is called and it filters the objects. And the description says that this option is for creating an executable... – Tsyvarev Sep 16 '19 at 22:07

1 Answers1

0

If you wish to compile OpenCV on a static manner on all the 3 platforms you mentioned, I would suggest to use conan.io package manager (here is the conan-opencv package), by issuing the command:

conan install opencv/4.1.1@conan/stable -o shared=False --build=missing

And from there, using conan in your project and make a final static executable.

Here is the official documentation of Conan, complete with examples.

In your case, your conanfile.txt will have the follow content:

[requires]
opencv/4.1.1@conan/stable

[generators]
cmake

[options]
opencv:shared=False
madduci
  • 2,635
  • 1
  • 32
  • 51
  • The issue I'm not after executable. I'm after building a custom static library which is a sub set of OpenCV (Or any other given libraries for that matter) with my own additional functions. – Royi Sep 16 '19 at 19:02
  • ok, then you need to create a conan package (check the documentation), so you can use the recipe on all the platforms. Remember that you still need to build a big static library at the end of the process, with the suggestions already present in comments – madduci Sep 16 '19 at 19:04
  • What I'm after is a way to have selective selection of the objects needed from OpenCV. Not to just package all OpenCV into single static library but have some linker to do dependency analysis and infer what's needed and repack only that. – Royi Sep 16 '19 at 19:08
  • @Royi unfortunately a cross-platform way to do what you want to achieve isn't possible. What you can do is to create a wrapper around OpenCV with all you need and build a Dynamic Library (.dll, .so, .dylib) out of it and link statically OpenCV to it. You'll have only one library (1 per Platform, of course) to import in your target project. Still, I heavily suggest to use something like conan to achieve the dependency management – madduci Sep 17 '19 at 05:53
  • I'm neither looking for cross platform compilation nor package management. I'm looking for a way, let's say on macOS, to create a single file static library from few functions of mine which depends on a different static library (In this case OpenCV). I want this file to contain all my functions and all functions / objects needed form OpenCV without those not needed to run my functions. Namely create a single static library which contains my functions and a sub set of the functions of OpenCV. – Royi Sep 17 '19 at 07:45
  • as said, you can't. Build a wrapper as dynamic library and link OpenCV statically to it. The linker (maybe with /LTO) will erase the unneeded functions for you. – madduci Sep 17 '19 at 10:04
  • So it is possible only when creating Shared Library and not with Static Library? – Royi Sep 17 '19 at 10:05