1

Based on tensorflow lite minimal example, I build a small application and want to share it as lib.

My CmakeList looks like that:

cmake_minimum_required(VERSION 3.16)
project(MY_LIB C CXX)

set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
  "Directory that contains the TensorFlow project"
)
if(NOT TENSORFLOW_SOURCE_DIR)
  get_filename_component(TENSORFLOW_SOURCE_DIR
    "~/tensorflow"
    # "${CMAKE_CURRENT_LIST_DIR}/../../../../"
    ABSOLUTE
  )
endif()

add_subdirectory(
  "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
  "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
  EXCLUDE_FROM_ALL
)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../results)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../results)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_library(MY_LIB SHARED
 src/my_code.cpp
)


target_link_libraries(MY_LIB
  tensorflow-lite
  ${CMAKE_DL_LIBS}
)

target_include_directories(MY_LIB PUBLIC
  inc
)


When running build, I get an .so file with ~ 5 MB, which works well even on machines without tensorflow (lite).

Then, I've replaced add_library(MY_LIB SHAREDby add_library(MY_LIB STATIC and get the expected .a file. Surprisingly, this file has only 500 kB and lacks of symbols, when running it.

I assume that tensorflow content is part of shared lib, but referenced somehow externally in the static lib. What do I need to configure to get also the full code, without any external dependencies in the static lib?

I've read here that both kinds of lib contains the whole code.

Thanks.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
amw
  • 452
  • 1
  • 5
  • 14
  • Try `set_property(TARGET MY_LIB PROPERTY POSITION_INDEPENDENT_CODE ON)` – m88 Jul 27 '21 at 15:12
  • I've already set thia globally `set(CMAKE_POSITION_INDEPENDENT_CODE ON)` – amw Jul 27 '21 at 15:41
  • "I've already set thi[s] globally"... **no, you haven't.** You set that variable _after_ including TFLite's sources via `add_subdirectory`. – Alex Reinking Jul 27 '21 at 16:22
  • 1
    "What do I need to configure to get also the full code, without any external dependencies in the static lib?" -- Does this answer your question? [Combining several static libraries into one using CMake](https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake) – Alex Reinking Jul 27 '21 at 16:24
  • Even when setting the property (at the end of `CMakeLists.txt` nothing changed. – amw Jul 27 '21 at 17:58

1 Answers1

1

I assume that tensorflow content is part of shared lib, but referenced somehow externally in the static lib. What do I need to configure to get also the full code, without any external dependencies in the static lib?

This is really quite hard to do in CMake.1

If you're using CMake to manage your libraries, however, it isn't an issue since CMake will handle placing all the required static libraries in the correct order on the link line.

1. (Note: none of the linked answers, including mine, are correct in every possible scenario).

I've read here that both kinds of lib contains the whole code.

That answer is a horrible oversimplification of what a static library is. A static library may have dependencies on other static libraries. CMake makes no attempt to merge static libraries together into one final artifact. On the other hand, linking a static library into a shared library will merge the two together.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • Thanks a lot. As a beginner I'll need much more time to read the linked questions and even more to understand the answers. Didn't expect that this is such a tough topic. – amw Jul 27 '21 at 17:59