Link an external static object archive (.a)

lotharyx
Posts: 3
Joined: Fri Dec 13, 2019 4:41 am

Link an external static object archive (.a)

Postby lotharyx » Fri Dec 13, 2019 4:53 am

Hi,

I hope this hasn't been answered before. I tried to search the forum but the page timed out before I got any results. Anyway...

I have a third-party library developed with autotools instead of CMake. I was able to successfully configure and build it with the xtensa-esp32 toolset that comes along with ESP-IDF. Now, for the life of me, I can't figure out how to get it linked in with the rest of my main IDF project. It seems like such a simple goal: an additional .a file included in the linker command. But I've tried everything I can think of and it always winds up in one of two places: Either CMake fails to finish generating the build scripts, or the link fails with undefined symbols. :( So let's say I have, in my "components" directory, a directory called "freetype" which contains all the relevant headers, and a static library file libfreetype.a. How do I get that fed into the linker under the ESP-IDF system?

If you're wondering why I'd even bother trying to link FreeType into my ESP32 project... My project includes a small graphical OLED display which communicates over SPI. At the hardware level, I've got that all working perfectly. But that display driver doesn't have a character ROM; it's purely graphical. I thought I might try using FreeType to handle rendering characters, and embed a .ttf font in the binary. Embedding stuff is easy (EMBED just works).

Anyway, thanks!

lotharyx
Posts: 3
Joined: Fri Dec 13, 2019 4:41 am

Re: Link an external static object archive (.a)

Postby lotharyx » Sat Dec 14, 2019 1:20 am

So, after banging my head against it for more hours today, I finally got it working. Here's the magic:

In components/freetype is libfreetype.a and a CMakeLists.txt which only contains this:

Code: Select all

idf_component_register()
(yep, no arguments)

In the component that depends on it, this is its CMakeLists.txt:

Code: Select all

idf_component_register(SRCS "resource.cpp" "font.cpp"
					   INCLUDE_DIRS "../freetype/include/"
					   INCLUDE_DIRS "../"
					   EMBED_FILES "myfont.ttf" 
					   )

add_library(freetype STATIC IMPORTED GLOBAL)
set_property(TARGET freetype PROPERTY IMPORTED_LOCATION "${COMPONENT_DIR}/../freetype/libfreetype.a")
target_link_libraries(${COMPONENT_TARGET} PUBLIC freetype)
So the magic is that after the "idf_component_register" call, you can put in commands that otherwise would cause "command is not scriptable" errors from CMake.

I hope this helps someone else!

User avatar
shabtronic
Posts: 49
Joined: Sun Nov 03, 2019 1:33 pm

Re: Link an external static object archive (.a)

Postby shabtronic » Sat Dec 14, 2019 3:03 am

Cool stuff dude - I had major problems linking in embedded binaries - so much so I gave up lol

Thanks for sharing the knowledge!

User avatar
dmarks_ls
Posts: 6
Joined: Fri Feb 10, 2023 4:56 pm

Re: Link an external static object archive (.a)

Postby dmarks_ls » Thu May 02, 2024 7:08 pm

FWIW, this is the correct answer:
https://esp32.com/viewtopic.php?t=16452

I just followed these instructions, and I was able to build a Hello World app while pulling a couple of functions from my application libraries. Here's the CMakeLists.txt for the component:

Code: Select all

# App libs include component w/ prebuilt source library.
# Set this to whatever components your library requires.
set (APPLIBS_REQUIRES freertos driver nvs_flash spi_flash esp_hw_support esp_wifi esp_adc)
idf_component_register(SRCS ""
                    INCLUDE_DIRS "include"
                    REQUIRES ${APPLIBS_REQUIRES})
add_prebuilt_library(applibs_source "lib/libapplibs_source.a"
                    REQUIRES ${APPLIBS_REQUIRES})
target_link_libraries(${COMPONENT_LIB} INTERFACE applibs_source)
So in my component directory, I have the above CMake file, a "lib" subdirectory containing my app libraries archive, and an "include" directory containing all the relevant include files for my library. Works great.

Who is online

Users browsing this forum: Google [Bot] and 76 guests