Add package and static library issue

Hello every one! I would like to add an external package. Before trying to build the package’s sources into RIOT (I struggle on this I am new to makefiles), I provide the pre-built static library ns_pkg_lib.a on one side, and all the header of the package on another side.

I rely on the package cryptoauthlib, which uses a static library. Let me do the comparison :

Makefile.cryptoauthlib and Makefile.ns_pkg_lib are the same (the name of the module changes of course).

in pkg/cryptoauthlib/Makefile.include :



DIRS += $(RIOTPKG)/cryptoauthlib/contrib

ARCHIVES += $(BINDIR)/cryptoauthlib.a

in pkg/ns_pkg_lib/Makefile.include :

NS_SOURCE_DIR ?= $(RIOTBASE)/build/pkg/ns_pkg_lib

-I$(NS_SOURCE_DIR)/lib \ #this is where the .a is found -I$(NS_SOURCE_DIR)/ns_pkg_lib/include/ns_pkg_lib-interface

DIRS += $(RIOTPKG)/ns_pkg_lib/contrib

ARCHIVES += $(BINDIR)/ns_pkg_lib.a

in pkg/cryptoauthlib/Makefile : fetches the repo, make the lib and do cp $(PKG_BUILD_DIR)/libcryptoauth.a $(BINDIR)/$(PKG_NAME).a

in pkg/ns_pkg_lib/Makefile: We fetch the repo but we do not make the package as the .a is already provided, then cp $(PKG_SOURCE_DIR)/lib/ns_pkg_lib.a $(BINDIR)/ns_pkg_lib.a

I can tell this line works because I have ns_pkg_lib.a file in tests/pkg_ns_pkg_lib/bin/board_used/

How I test it: In my package, in a .c file I call a function supposed to call RIOT spi functions. In the .h of the package, the proto of the function is declared, but I have no implementation of this function in my package. I did the implementation in pkg/contrib/spi_platform.c, and I include the header of the package. I can tell it finds the header because it doesn’t tell it can’t find it, and when I comment it, I have implicit declaration error. In my main.c, I include the header, and I call the function. It compiles but I have Undefined reference to function, so it doesn’t work.

could not having implementation in the package be an issue? How can I know if it cannot link the library or I did a coding mistake? Am I missing something? Thank you for reading this long post and thank you very much for your help. Do not hesistate to ask for further detail.


The archive is not linked to the resulting firmware that’s why you get the build failure. If you add

ARCHIVES += $(BINDIR)/ns_pkg_lib.a

in pkg/ns_pkg_lib/Makefile.include, it should link correctly.

Hello, thank you for your response! This line is already there, but a carriage return was missing so it was confusing, my bad, the post is edited. Cheers

Can you post the link error raised ? Do you have a Makefile.dep containing the dependencies of your package (with USEMODULE += <modules> and/or FEATURES_REQUIRED += <features>) ? Since the driver require SPI, maybe there’s a missing FEATURES_REQUIRED += periph_spi in the package Makefile.dep ?

The “error” : bin/ld : riot/tests/pkg_ns_pkg_lib/main.c:91 : undefined reference to « spi_exchange »

Yes on my Makefile.dep I have only one line : USEMODULE += periph_spi because this is the feature I want to test for now.


For periph_spi you should use FEATURES_REQUIRED += periph_spi instead of USEMODULE += periph_spi. And you should also include your contrib module with USEMODULE += ns_pkg_lib_contrib, assuming there’s Makefile.base file in $(RIOTPKG)/ns_pkg_lib/contrib that contains:

MODULE = ns_pkg_lib_contrib
include $(RIOTBASE)/Makefile.base

I just built it, and it worked! No more Undefined reference :). Thank you very much for your help!