2
votes

What is difference between:

  1. Loading an Shared library using dlopen() available in dlfcn.h
  2. Including the shared library in Android.mk file while building the executable.

Code example: Android.mk file:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=     \
    test.c

LOCAL_SHARED_LIBRARIES := \ libtest

LOCAL_MODULE:= test
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)

include $(BUILD_EXECUTABLE)

While executing the above 2 methods, which is the better/more efficient way of implementing?

2

2 Answers

2
votes

With dlopen(), the linker never works for you. You must manually look up each function, before calling it using dlsym(). This makes each call-site of a function from a shared object more complex than just doing a plain call and letting the linker sort it out.

2
votes

With method 2 (linker), the whole program doesn't start, if the library is missing. With method 1 (dlopen) you can gracefully handle the error, leaving the program running. dlopen is used for some special cases, like loading plug-ins, or starting optional features.