You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use an external C++ library linked to a Zephyr application as a static library. For a background - an external library is AgIsoStack
I have a problem with including it in the binary. Building goes well, but as soon as the application refers to any classes defined in the external library, the FW crashes.
To simplify the situation, I reproduced the issue on a smaller example.
Testing platform: NUCLEO-F446RE
Zephyr SDK version: 0.16.8
C and CXX compilers version: 12.2.0 both
Host: Ubuntu 22.04
Directory structure (In the real application, the external library is outside of app repo):
cmake_minimum_required(VERSION 3.10)
project(MathOperationsLibrary)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(SOURCES ${SRC_DIR}/MathOperations.cpp)
add_library(MathOperations STATIC ${SOURCES})
target_include_directories(MathOperations PUBLIC ${INCLUDE_DIR})
target_include_directories(MathOperations
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
MathOperations.hpp:
namespace MathLib {
class MathOperations {
public:
int add(int a, int b);
int subtract(int a, int b);
};
}
MathOperations.cpp:
#include "MathOperations.hpp"
namespace MathLib {
int MathOperations::add(int a, int b) {
return a + b;
}
int MathOperations::subtract(int a, int b) {
return a - b;
}
}
main.cpp:
#include <stdio.h>
#include <zephyr/kernel.h>
#include "MathOperations.hpp"
int main(void)
{
printf("APP STARTED\n");
auto operations = new MathLib::MathOperations();
int a = operations->add(1, 2);
printf("a = %d\n", a);
while (1) {
static int counter = 0;
printf("COUNTER: %d\n", counter++);
k_msleep(1000);
}
return 0;
}
Problem. The result of adding is not printed. Debugger stuck after the line operations->add(1, 2);.
In the real library case, I got BUS FAULT during object allocation:
I guess there is some linkage issue, which is not seen by linker. Looks like linker puts wrong addresses in the memory. Symbols defined in the library are present in target zephyr.map file. So, linker linked it, but looks like it links to the wrong addresses.
As a limitation. I can't get rid of the CMakeLists.txt defined in the library, I can modify my CMakeLists.txt in the app directory. Optionally, I can write some wrapper/adapter to the lib if needed.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to use an external C++ library linked to a Zephyr application as a static library. For a background - an external library is AgIsoStack
I have a problem with including it in the binary. Building goes well, but as soon as the application refers to any classes defined in the external library, the FW crashes.
To simplify the situation, I reproduced the issue on a smaller example.
Testing platform: NUCLEO-F446RE
Zephyr SDK version: 0.16.8
C and CXX compilers version: 12.2.0 both
Host: Ubuntu 22.04
Directory structure (In the real application, the external library is outside of app repo):
west.yml:
prj.conf:
CMakeLists.txt in app:
CMakeLists.txt in mathoperations:
MathOperations.hpp:
MathOperations.cpp:
main.cpp:
Problem. The result of adding is not printed. Debugger stuck after the line
operations->add(1, 2);
.In the real library case, I got BUS FAULT during object allocation:
I guess there is some linkage issue, which is not seen by linker. Looks like linker puts wrong addresses in the memory. Symbols defined in the library are present in target
zephyr.map
file. So, linker linked it, but looks like it links to the wrong addresses.As a limitation. I can't get rid of the
CMakeLists.txt
defined in the library, I can modify my CMakeLists.txt in the app directory. Optionally, I can write some wrapper/adapter to the lib if needed.Beta Was this translation helpful? Give feedback.
All reactions