gdaramouskas
gdaramouskas

Reputation: 3747

Instruct gnu make to use only static libraries

Here is what I want to do:

There is a project written in c/c++ that I want to compile and deploy to an Android device.

I want every library that this project references to be statically linked to the whole compilation so that I can get a self contained binary (or a couple of them).

Question 1: After searching I found that there are more or less two types of libraries, dynamic and static. My question here is, do I have to provide the static libraries or is the gcc toolchain able to somehow compile them from the headers?

Question 2: When searching about static linking I only found examples of using flags only for a finite amount of libraries and for object files. I want a recursive function. That is "for every library reference within the project link the static version of it. If there is not such version, compile and link it (is this possible?)

Upvotes: 3

Views: 259

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

Libraries cannot be compiled from headers. You need the sources of all libraries you need. Usually, such sources come with their build scripts, and these scripts may allow choosing static or shared target. Unfortunately, in some cases only one target type is supported.

Typically, we build third party libraries separately, using their build scripts (some involve standalone toolchains, others may use CMake to configure, yet others - and their share increases as Android platform grows in popularity - provide Android.mk build scripts and are compiled with convenient ndk-build command.

At any rate, the app that uses these libraries must include explicit references to all these libraries, usually by adding include $(PREBUILT_STATIC_LIBRARY) to its Android.mk. But if you have many libraries in one directory, you can use gnu-make wildcards, e.g.

LOCAL_LDLIBS += -Ljni/libs $(patsubst jni/libs/lib%.a,-l%,$(wildcard jni/libs/lib*.a))

Upvotes: 1

Related Questions