I have been trying to integrate ArduinoCore-avr in a little project for a few days, but I don't seem to be able to make it work.
Here you will find where I am at, and a link to the repo. The error I get makes me think the issue is related to avr-libc.
I understand that avr does not ship with a C++ standard library (my cMakeLists.txt is using c++_shared, which is the recommended one for external libraries in NDK), so I will have to rely on C++ core language features.
Now, I reckon Arduino.h depends on avr. I was wondering how I can fix this. Is there a cpp source avr I can replace avr with?
I am an Arduino and C++ "tourist", as I am a high level software developer, so feel free to give any kind of advise and suggestions.
This is my cMakeLists.txt
cmake_minimum_required(VERSION 4.1.1)
project(native-lib CXX)
find_package(openssl REQUIRED CONFIG)
find_package(curl REQUIRED CONFIG)
include(FetchContent)
FetchContent_Declare(
cpp-subprocess
GIT_REPOSITORY https://github.com/arun11299/cpp-subprocess.git
GIT_TAG v2.5
)
FetchContent_Declare(
arduino-core
GIT_REPOSITORY https://github.com/arduino/ArduinoCore-avr.git
)
FetchContent_Declare(
avr-libc
GIT_REPOSITORY https://github.com/avrdudes/avr-libc.git
GIT_TAG avr-libc-2_2_0-release
)
FetchContent_MakeAvailable(cpp-subprocess avr-libc arduino-core)
add_library(avr-libc INTERFACE)
target_include_directories(avr-libc
SYSTEM INTERFACE
${avr-libc_SOURCE_DIR}/include
${avr-libc_SOURCE_DIR}/include/avr
)
add_library(arduino-core INTERFACE)
target_include_directories(arduino-core
SYSTEM INTERFACE
${arduino-core_SOURCE_DIR}/cores/arduino
)
file(REMOVE ${arduino-core_SOURCE_DIR}/cores/arduino/new)
add_library(native-lib SHARED src/main/cpp/native-lib.cpp)
set_target_properties(native-lib PROPERTIES CXX_STANDARD 17)
target_link_libraries(native-lib
PRIVATE
avr-libc
arduino-core
cpp-subprocess::subprocess
-llog
curl::curl
)
and this is my .cpp entry point
#include <jni.h>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <memory>
#include <sstream>
#include <iostream>
#include <string>
#include <array>
#include <format>
#include <cstdio>
#include <stdexcept>
#include <android/log.h>
#include "curl/curl.h"
#include "java_interop.h"
#include "cpp-subprocess/subprocess.hpp"
#include "Arduino.h"
using namespace subprocess;
namespace arduinoide {
... etc etc
When I try to build via NDK, I get the following error:
In file included from /repos/android_arduino_ide/app/src/main/cpp/native-lib.cpp:6:
/home/feelhippo/Android/Sdk/ndk/28.2.13676358/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/cstdlib:89:5: error: <cstdlib> tried including <stdlib.h> but didn't find libc++'s <stdlib.h> header. This usually means that your header search paths are not configured properly. The header search paths should contain the C++ Standard Library headers before any C Standard Library, and you are probably using compiler flags that make that not be the case.
89 | # error <cstdlib> tried including <stdlib.h> but didn't find libc++'s <stdlib.h> header. \
| ^
-
2the question should stand on its own ... please add the code and error message to your post ... write text, not picturesjsotola– jsotola2025年09月25日 15:46:15 +00:00Commented Sep 25 at 15:46
-
"Is there a cpp source avr I can replace avr with?", there is no need for cpp version of avr-libc, as all the avr-libs source codes are marked as C so that the gcc know how to compile and link those codes into ultimate target code. Whatever you are trying to do with C++_shared has nothing related to avr-libc.hcheung– hcheung2025年09月26日 06:25:39 +00:00Commented 2 days ago
-
@jsotola I have updated the original question.Fi Li Ppo– Fi Li Ppo2025年09月26日 07:27:03 +00:00Commented 2 days ago
-
@hcheung thank you for your help. I reckon the error I get (see updated question) is caused by NDK searching for a cpp source. This is where my limited knowledge of this kind of programming ends, and any help will be much appreciated :)Fi Li Ppo– Fi Li Ppo2025年09月26日 07:28:37 +00:00Commented 2 days ago
-
1High level, what are you trying to do? Trying to compile the AVR Arduino core for an Android device natively? Huh?Maximilian Gerhardt– Maximilian Gerhardt2025年09月26日 15:57:32 +00:00Commented 2 days ago