Create a STATIC library with another STATIC library whose contents are on iOS using CMake

I have a collection of libfooi.a; libfoo1.a, libfoo2.a, libfoo3.a ..., which use factories (with static code), have a common interface for creating C ++ objects.

With CMake, I select one of them and create libfooWrapper.a that bind it and add all the content. Using CMake, this CMakeLists.txt works in Android:

PROJECT(fooWrapper) INCLUDE_DIRECTORIES(___) ADD_LIBRARY(fooWrapper SHARED ${SRC} ${HEADERS} ) # Must be STATIC in iOS IF(selected1) TARGET_LINK_LIBRARIES(fooWrapper -Wl,--whole-archive foo1 -Wl,--no-whole-archive) ELSEIF(...) TARGET_LINK_LIBRARIES(fooWrapper -Wl,--whole-archive foo2 -Wl,--no-whole-archive) 

A manually-executed executable application project, only linked fooWrapper generated and working.

But on iOS using Clang, I changed ADD_LIBRARY to STATIC and try using -Wl, the whole archive, but it doesn't work. I checked the documentation that using -Obj -Wl, -force_load should work. I also tried using the -Obj -Wl, -all_load flag.

By analyzing the libfooWrapper.a library with otool, it seems that all the content from libfooi.a has not been added to libfooWrapper.a, but I need to put it inside to avoid changing the flags manually in the executable application project.

What is wrong with the link?

+6
source share
1 answer

For iOS, use libtool to create one static library from several static libraries:

 add_library(fooWrapper STATIC ${SRC} ${HEADERS} ) add_custom_command(TARGET fooWrapper POST_BUILD COMMAND /usr/bin/libtool -static -o $<TARGET_FILE:fooWrapper> $<TARGET_FILE:fooWrapper> $<TARGET_FILE:foo1> $<TARGET_FILE:foo2> $<TARGET_FILE:foo3> ) 

The post build action combines the goals of the CMake static library foo1, foo2, and foo3 into fooWrapper. In addition, you can also use full library paths instead of the generator expressions $<TARGET_FILE:...> .

+7
source

Source: https://habr.com/ru/post/976334/


All Articles