Common problems when compiling a C/C++ source file for Android and how to fix them

Last Update: 11.10.2026/bs



Contents


  1. Common problems when compiling a C/C++ source file for Android and how to fix them
    1. Contents
    2. Introduction
    3. History
    4. General information
      1. Toolchains to compile C/C++ sourcecode for Android
      2. Patches for C/C++ programs or libraries
      3. Compiler parameter
      4. Missing definitions and functions
      5. Missing libraries
      6. Unnecessary libraries
      7. Dynamically linked binaries versus statically linked binaries
      8. How to force the addition of a static library to an executable file
      9. Runtime libraries for C++ programs compiled with clang
      10. Runtime libraries for C++ programs compiled with gcc
      11. How to force clang to list the used objects and libraries to link a binary
      12. How to change the libraries required by a binary
      13. Testing the compiled binary
      14. Sample configure wrapper
      15. General trouble shooting hints
      16. How to print the default settings for the compiler
      17. How to list the compiler builtin macros
      18. How to print the search path for include files
      19. How to print the output files the C compiler would create
      20. How to print verbose make messages to STDOUT
      21. How to print verbose cmake messages to STDOUT
      22. How to print messages to STDOUT while compiling
      23. How to print messages to the Android log
      24. Useful parameter for CMAKE
      25. cmake Logfiles
      26. How to debug a binary in Android
      27. How to debug a binary from Android with gdb
      28. Functions and symbols defined in the standard Android OS libraries
      29. Examples for compiling C/C++ programs for Android
      30. Documentation for the compiler and tools
      31. General hints
        1. Memory usage
    5. Specific errors
      1. ERROR: "cannot find crtbegin_dynamic.o: No such file or directory"
      2. ERROR: fatal error:sys/fcntl.h:No such file or directory
      3. ERROR: undefined reference to `getsubopt'
      4. ERROR: error: undefined reference to 'stderr'
      5. ERROR: faddrs.h: No such file or directory
      6. ERROR: ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
      7. ERROR: error: "./nmap": executable's TLS segment is underaligned: alignment is 8, needs to be at least 64 for ARM64 Bionic
      8. ERROR: ld: error: undefined symbol: index
      9. ERROR: ld: error: undefined symbol: rindex
      10. ERROR: ld: error: undefined symbol: strverscmp
      11. ERROR: use of undeclared identifier 'ushort'; did you mean 'short'?
      12. ERROR: use of undeclared identifier 'versionsort'
      13. ERROR: libutil.so missing
      14. ERROR: ld: error: undefined symbol: getdtablesize
      15. ERROR: the static libraries for building a static binary are missing
      16. ERROR: src/ls.c:2276:6: error: function-like macro '__GNUC_PREREQ' is not defined
      17. ERROR: unused DT entry: DT_RPATH (type 0xf arg 0xf7a)
      18. ERROR: cannot find entry symbol _start
      19. ERROR: non-root user can not create hard links
      20. ERROR: ld: error: unable to find library -lrt
      21. ERROR: ld: error: undefined reference due to --no-allow-shlib-undefined: wmempcpy
      22. ERROR: use of undeclared identifier 'nl_langinfo'
      23. ERROR: Pointer tag for 0xdbcd000000060106 was truncated
      24. ERROR: ld.lld: error: undefined symbol: __assert_fail
      25. ERROR: ld.lld: error: undefined reference: frexp
      26. How to define the value used by the marco  __BIONIC_AVAILABILITY_GUARD
      27. ERROR: openat-die.c:37:3: error: use of undeclared identifier 'error'
      28. ERROR: error: expected ')' in the statement __attribute__((__sentinel__(1)));
      29. ERROR: libintl.h not found
      30. ERROR: ld.lld: error: undefined symbol: bzero
      31. ERROR: ./include/fileutils.h:90:19: error: static declaration of 'close_range' follows non-static declaration
      32. ERROR: ld.lld: error: duplicate symbol: memcmp
      33. ERROR: ld.lld: error: undefined symbol: tzfree
      34. ERROR: ld.lld: error: undefined symbol: backtrace
      35. ERROR: error: use of undeclared identifier 'pthread_cond_clockwait'
      36. ERROR: crtbegin.c:(.text+0x20): undefined reference to `main'
      37. ERROR: /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdlib.h:128:1: error: unknown type name 'uint32_t'
      38. ERROR: rm: /data/local/tmp/develop/ksh/arch/android.arm64-64/lib/package/gen/make.out.fifo: Permission denied
      39. ERROR: ld.lld: error: undefined symbol: bcmp
      40. ERROR: incomplete definition of type 'struct udphdr'
      41. ERROR: config.guess does not know Android
      42. ERROR: undefined reference to rpl_malloc / rpl_realloc / rpl_free
      43. ERROR: ./asm/types.h not found
      44. ERROR: ld.lld: error: undefined symbol: dlopen
      45. ERROR: checking build system type... build-aux/config.guess: unable to guess system type
      46. ERROR: ./time.h:1057:19: error: conflicting types for 'tzalloc'
      47. ERROR:  error: use of undeclared identifier 'CPU_SETSIZE'
      48. ERROR : call to undeclared function 'pipe2'
      49. ERROR: The linker does not find an existing library

 

Introduction


This page contains general information about compiling C/C++ programs for Android with clang or gcc. It also provides some tips for troubleshooting common problems when compiling a binary for Android.

Please note that all binaries or Magisk Modules mentioned in this documentation are for Android on arm64 CPUs.


General instructions for setting up the various tool chains for compiling C/C++ programs for Android, as well as instructions for compiling various Unix programs using different tool chains, can be found here:

Compiling_programs_for_Android

 

History


ChangeLog


11.10.2026

added the section How to print the output files the C compiler would create  
updated the section Runtime libraries for C programs compiled with gcc       


09.10.2026

added the section Runtime libraries for C programs compiled with gcc       


03.10.2026

added a hint for using gdb   


01.10.2026

added the section Memory usage       


29.09.2026

added the section ERROR: error: use of undeclared identifier 'CPU_SETSIZE'         
added the section ERROR : call to undeclared function 'pipe2'   
added the section ERROR: The linker does not find an existing library      


07.08.2026

added the section cmake Logfiles  


04.08.2026

added the section Testing the compiled binary    


30.07.2026

updated the section How to change the libraries required by a binary            


29.07.2026

added the section ERROR: ./time.h:1057:19: error: conflicting types for 'tzalloc'   
added the section How to change the libraries required by a binary            
added the section How to force clang to list the used objects and libraries to link a binary  


12.07.2026

added the section ERROR: checking build system type..._build-auxconfig.guess: unable to guess system_type   


14.06.2026

updated the section How to print the search path for include files        


07.06.2026

added the infos about fixing the error ERROR: ld.lld: error: undefined symbol: dlopen           
added the infos about the gcc/g++ toolchain for Android  


02.06.2026

added the URL of the Termux User repository to the section with infos about patches for C/C++ programs and libraries  
added the infos about fixing the error ERROR: ./asm/types.h not found  


30.05.2026

added infos about fixing the error ERROR: undefined reference to rpl_malloc / rpl_realloc / rpl_free  
added the section How to print the list the search path for include files        
added the section How to print the default settings for the compiler        


19.04.2026

updated the links to the new page Compiling_programs_for_Android


08.03.22026

add infos about how to fix config.guess errors  


24.02.2026

added the section Useful parameter for CMAKE       


04.02.2026

add the infos about fixing the ERROR: incomplete definition of type 'struct udphdr'  


03.02.2026

added the infos for fixing the error ERROR: ld.lld: error: undefined symbol: bcmp      
added the infos about the script create_dummy_library.sh . 
updated the infos about the definitions in the file add_missing_definitions.h     


09.11.2025

updated the infos about the pointer tag errors 


28.10.2025

added the infos for fixing the error ERROR: crtbegin.c:(.text+0x20): undefined reference to `main'          
added the infos for fixing the error ERROR: /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdlib.h:128:1: error: unknown type name 'uint32_t'         
added the infos for fixing the error ERROR: rm: /data/local/tmp/develop/ksh/arch/android.arm64-64/lib/package/gen/make.out.fifo: Permission denied       


27.10.2025

added the section How to force the addition of a static library to an executable file           


16.10.2025

added the infos for fixing the error ERROR: ld.lld: error: undefined symbol: backtrace           
added the infos for fixing the error ERROR: error: use of undeclared identifier 'pthread_cond_clockwait'         


14.10.2025

added the section How to debug a binary from Android with gdb     
added the section Runtime libraries for C++ programs compiled with clang   


03.10.2025

added the infos for fixing the error ERROR: ld.lld: error: undefined symbol: tzfree  
updated the list of definitions in the file add_missing_definitions.h  


21.09.2025

inital release



----


Old changelog

27.11.2024

initial release

22.01.2025

added the infos for fixing the error use of undeclared identifier 'nl_langinfo'  

23.01.2025

added the infos about the error ERROR: Pointer tag for 0xdbcd000000060106 was truncated     

27.01.2025

added the infos about the error ERROR: ld.lld: error: undefined symbol: __assert_fail  

13.04.2025

added the infos about the error ERROR: ld.lld: error: undefined reference: frexp   

15.04.2025

added the infos about How to define the value used by the marco  __BIONIC_AVAILABILITY_GUARD  

19.04.2025

added the infos about the error ERROR: openat-die.c:37:3: error: use of undeclared identifier 'error'      

07.06.2025

added the infos about the error ERROR: error: expected ')' in the statement __attribute__((__sentinel__(1)));    
added the infos about the error ERROR: libintl.h not found   

15.06.2025

added the infos about the error ERROR: ld.lld: error: undefined symbol: bzero      

07.07.2025

updated the infos about the alignment error : The additional flags must be added to the LDFLAGS and NOT to the CFLAGS

24.08.2025

added the infos about the error duplicate definition of the function close_range  

19.02.2025

added the infos about the error ld.lld: error: duplicate symbol:       
updated the infos about the alignment error : The clang from the Android NDK must be version 29 or newer



 

General information




Toolchains to compile C/C++ sourcecode for Android




Update 02.06.2026

See also the page Hints and Examples for compiling Unix programs for Android for instructions to install and use the various tool chains for compiling C/C++ binaries or libraries for Android.


 

There are various ways to compile C/C++ programs for Android.


The "official" way is to use one of the Android NDKs from Google on a PC running Linux, Windows, or MacOS.

The Android NDKs are available here for download:

https://developer.android.com/ndk/downloads#stable-downloads

The Android NDKs contain everything needed to compile C or C++ programs for the Android OS.


Please note that there is only a clang compiler in the current version of the Android NDKs.

The last Android NDK with the gcc is the Android NDK r14b - that NDK version is still available for download on this page:

https://github.com/android/ndk/wiki/Unsupported-Downloads#r14b


Another method for compiling C/C++ programs is to use a compiler on your phone.


This can be done in Termux, for example. Termux is an almost complete Linux environment for Android that contains all the necessary tools and libraries for creating C/C++ programs on Android.
The disadvantage here, however, is that programs created under Termux use Termux's own configuration files and libraries and therefore only run to a limited extent on phones without Termux.


To get around this limitation, I created a Magisk module that contains the clang compiler (clang version 19) and the files from the Android NDK. The binaries created with the compiler from this Magisk module should run on any phone.

The documentation for the Magisk Module with clang19 is here:

Documentation for the Magisk Module with clang19 and the NDK r27b


The Magisk Module is available here: 

http://bnsmb.de/files/public/Android/clang19_19.0.0git-v1.2.2.zip

The files from this Magisk Module are also available in a GitHub repository:

https://github.com/bnsmb/Magisk-Module-with-clang19

Note that I have also created several other Magisk modules (with cmake, gdb, git, etc) that may be useful for compiling C/C++ programs on phones. See this page for a list of Magisk Modules that I have created.


There is also a Magisk Module with the gcc :

The gcc in this GCC toolchain is version gcc 10.2 and the libraries in the GCC toolchain come from the Android API version 21, which are both quite old.

This Magisk Module is available here:

https://github.com/Googlers-Repo/gcc/




Update 07.06.2026

There is now also a GCC tool chain with a recent gcc and g++ compiler for Android that can be used without root access available in this GitHub repository:

https://github.com/AmanoTeam/android-gcc-cross

See this guide for instructions to use that GCC tool chain without using Termux.



Update 14.06.2026

Since version 2.0.0 the clang19 toolchain also contains the gcc and g++ compiler from

https://github.com/AmanoTeam/android-gcc-cross





For those who do not use Magisk on their phone or do not have root privileges, I have also created a tar archive with a clang19 toolchain that installs in /data/local/tmp on the phone and can be used by the user shell (the default user for adb sessions).

This clang19 toolchain contains the clang compiler, the necessary files from the Android NDK, and other tools needed to compile C/C++ programs (including the debugger gdb).
Since version 2.0.0 of the clang19 toolchain the compiler gcc and g++ are also included.


As of 14.06.2026, the tar file contains these program versions:

grep "^#" README | cut -c2- -d " "

ASUS_I006D:/data/local/tmp/sysroot $ date
Sun Jun 14 16:23:02 CEST 2026
ASUS_I006D:/data/local/tmp/sysroot $

ASUS_I006D:/data/local/tmp/sysroot $  grep "^#" README | cut -f2- -d " "
The tools included in the tar file with the clang19 toolchain are:

clang 19.0.0
gcc 16.1.1
cmake 3.30.4, 4.0.1, and 4.3.0
ninja 1.13.1
make 4.4.1
meson 1.9.1
binutils 2.45 (addr2line  ar  as  c++filt  elfedit  gprof  ld  ld.bfd  nm  objcopy  objdump  ranlib  readelf  size  strings  strip)

autoconf 2.72
automake 1.17
m4 1.4.21
libtool 2.5.3
patch 2.8 (gnupatch)
patchelf 0.18
GNU help2man 1.49.3
pkg-config 0.29
pkgconf 2.30
autopoint 0.23
texinfo 7.2

bash 5.2.37
mksh @(#)MIRBSD KSH R59 2025/06/02

bison 3.8
flex 2.6.4

gdb 17.2 (dynamically and statically linked binaries)

rsync 3.4.1

wget 2.2.0
curl 8.19.0

perl 5.40
python 3.14
pip 25.0.1
tcl 9.0
lua 5.4.8
sqlite3 3.51.1

sshd/ssh 10.0p2
OpenSSL 3.5.6 and 4.0.0

git 2.49
git-lfs 3.7.1

gpg 1.4.3

dig 9.11.37
mtr 0.96
ngrep 1.48.3

nano 9.0, 8.7.1, and nano 6.4
vim 9.2
ed 1.21
bvi /bmore 1.5.0 (a hex editor)
hexcurse 1.60.0 (a hex editor)

man 2.13

tmux 3.5a

tar 1.35
dd 9.5.58

jq 1.7.1

gawk 5.3.2
grep 3.12
gsed 4.9
diff 3.11
find 4.9
xtargs 4.9
bc 1.08.2
less 704
htop 3.4.1
btop 1.4.4 (btop needs root access)
strace 6.18

Android SDK build tools 35.0.2 (aidl zipalign aapt aapt2 dexdump split-select e2fsdroid sqlite3 fastboot sload_f2fs hprof-conv etc1tool mke2fs make_f2fs adb make_f2fs_casefold veridex)
  in the directories /data/local/tmp//usr/androidsdk35_0_2/*-tools
  (the binaries are from this ZIP file: https://github.com/lzhiyong/android-sdk-tools/releases/download/35.0.2/android-sdk-tools-static-aarch64.zip)

OpenVPN 2.7.4 (openvpn requires root access)
stunnel 5.77  (Stunnel is a proxy designed to add TLS encryption functionality to existing clients and servers without any changes in the programs code, https://www.stunnel.org/)

unfsd 0.11.0 (a userland NFS daemon usable by non-root user; the default exports file for this binary is /data/local/tmp/sysroot/etc/exports)
darkhttpd 1.17 (a simple webserver; see https://github.com/emikulic/darkhttpd)

protoc v32.1.0 (currently the binary only)

various compression tools:

7zz 26.00
bzip2 1.0.8
zip 3.0
unzip 5.52
lz4 v1.10.0
lzop 1.04
compress 5.0
uncompress 5.0
zstd 1.5.6
pigz 2.8
xz 5.8.1
gunzip 1.13
gzip 1.13
unrar 7.20 beta 2
lzip 1.25

ASUS_I006D:/data/local/tmp/sysroot $




see How to install a Toolchain for clang on phones without root access for the documentation for the clang19 toolchain.


The clang19 toolchain is available here for download:

clang19_toolchain*tar.gz


The files from the clang19 toolchain are also available in a repository on GitHub:

https://github.com/bnsmb/clang19_toolchain_for_android




Notes

I recommend using the clang19 toolchain for compiling C/C++ programs on phones.


The URLs in this page are current as of 03.02.2026.

The URLs in this page for the Magisk Modules and the tar file change with new versions.
If a link no longer works, please check the list of my Magisk Modules or the file list on the web server for a newer version:




Patches for C/C++ programs or libraries


The file system structure in Android differs from the standard file system structure in Linux. Therefore, the source code for most programs that use configuration files in a location common in Linux must be adapted for Android.

To avoid reinventing the wheel, it is advisable to check whether there are already patches in Termux for the program to be compiled. The patches from Termux can usually be applied without further changes after correcting the directory name in the patches.

Since the patches in Termux also contain other changes that are necessary for Android, it is definitely advisable to take a look at the patches before compiling.

The patches from Termux for Unix programs can be found here:

https://github.com/termux/termux-packages/tree/master/packages/


Update 02.06.2026

Another repository with patches for Unix packages is the Termux User Repository:

https://github.com/termux-user-repository/tur/tree/master/tur



Compiler parameter


When compiling C/C++ programs for Android these compiler and linker flags are mandatory for dynamically linked binaries:

CFLAGS="${CFLAGS} -fPIE -fPIC"

LDFLAGS="${LDFLAGS} -pie"


Some configure scripts only use the CFLAGS variable for the compiler options to compile and build a test program, causing the configure script to fail because the linker option -pie is missing. To work around this, add the parameter -pie to CFLAGS before running the configure script.

Note that option -pie is not allowed for creating libraries.


Many source codes for Unix programs use code that is no longer permitted in clang by default. However, this code is still okay and working

To ensure that clang ignores most of these errors in the source code and still compiles the program, I use these compiler flags

CFLAGS="${CFLAGS} -Wno-implicit-function-declaration -Wno-int-conversion"


To create a binary with all info necessary for using a debugger like gdb, use these compiler flags:

DEBUG_CFLAGS="-g -O0"

CFLAGS="${CFLAGS} ${DEBUG_FLAGS}"



Missing definitions and functions


There are a lot of definitions used in the source code of existing Unix programs that do not exist in Android. For many of them, workarounds for compiling under Android can be created relatively easily.

I have created an include file with the missing definitions that I have found so far when compiling C programs in Android. The include file can be found here: add_missing_definitions.h



As of 14.06.2026 there are these definitions in the file:

//
// include file with standard definitions missing in the libc from Android
//
// Please note that you must remove the definitions for functions that already exist in the source code from this include file.
// If not, error messages like this are displayed when compiling your source code:
//
//    command.c:(.text+0x348): multiple definition of `catclose'
//    array.o:array.c:(.text+0x348): first defined here
//
// Therefore, either edit this file before using it or copy only the necessary parts to your source files
//
//
// This file defines
//   quad_t
//   u_quad_t
//   short
//   PTR
//
// This file defines the functions
//   strverscmp
//   versionsort
//   getdtablesize
//   index
//   rindex
//   catopen
//   catgets
//   catclose
//   nl_langinfo
//   getpwent
//   endpwent
//   CloseSocket
//   mktime_z
//   bzero
//   tzalloc
//   tzfree
//   localtime_rz
//   mktime_z
//   android_getpass
//   bcmp
//   ldexpl
//   frexpl
//
// This file defines the macros
//
//   __GNUC_PREREQ
//   isblank
//   min
//   max
//


In most cases, you cannot include this file without editing it - see the comment in the header of the include file:

// Please note that you must remove the definitions for functions that already exist in the source code from this include file.
// If not, error messages like this are displayed when compiling your source code:
//
//    command.c:(.text+0x348): multiple definition of `catclose'
//    array.o:array.c:(.text+0x348): first defined here
//
// Therefore, either edit this file before using it or copy only the necessary parts to your source files
//


Notes

The include file is also available in my GitHub repository with include files and libraries for Android:

https://raw.githubusercontent.com/bnsmb/libraries-and-include-files-for-Android-on-arm64-CPUs/refs/heads/main/sysroot/usr/include/add_missing_definitions.h




Missing libraries


The NDK for Android only contains some of the standard include files and libraries for Linux. If one of the standard include files is missing, check the sysroot from another NDK version to see if it contains the missing file.


I've created a GitHub repository with some of the standard non-OS libraries required by Unix programs:

https://github.com/bnsmb/libraries-and-include-files-for-Android-on-arm64-CPUs

This repository contains include files, and static and dynamic libraries for Android running on arm64 CPUs.

Please note that I have not checked whether patches are required for Android for most of the libraries in this repository.

The purpose of these libraries is to enable you to compile a binary without errors. Before using the libraries for productive binary files, you should therefore compile your own versions of the neccessary libraries for your binary after applying the necessary patches.



Unnecessary libraries


The configuration scripts and makefiles for some Unix programs require libraries that are not needed in the Android environment and are not available there.

This applies, for example, to the library libpthread.so: The functions from this library are included in the standard OS libraries in Android.

To avoid having to manually adjust the configure scripts and makefiles, it makes sense to create an empty dummy library with the name libpthread.so.

This can be done as follows (after initializing the clang19 toolchain with "source bin/init_clang19_env" )

cat<<EOT >foo.c
// foo.c  - make sure to use a unique name for this dummy function!
int __bs_function_42() {
    return 42;
}

EOT

$CC -fPIE -fPIC -c foo.c

$AR rcs libpthread.a foo.o

$CC $LDFLAGS -shared -o libpthread.so foo.o


The commands create a library for static builds (libpthread.a) and a library for dynamic builds (libpthread.so); $CC and $AR must contain the name of the executables that are used to create binaries for Android, e.g:

[aarch64-linux-android] [ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/source/dummyLib/test ] $ echo $CC
/data/develop/android/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang
[aarch64-linux-android] [ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/source/dummyLib/test ] $

[aarch64-linux-android] [ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/source/dummyLib/test ] $ echo $AR
/data/develop/android/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar
[aarch64-linux-android] [ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/source/dummyLib/test ] $


The environment variable $LDFLAGS in this example must contain the necessary linker parameter, example:

[clang19 toolchain] ASUS_I006D:/data/local/tmp $ echo $LDFLAGS
-L/data/local/tmp/sysroot/usr/lib -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31 -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android -L/data/local/tmp/develop/sysroot/usr/lib -B/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31/ --sysroot=/data/local/tmp/sysroot/usr/ndk/r27d/sysroot -lc
[clang19 toolchain] ASUS_I006D:/data/local/tmp $


Please note that the dummy library is then also required to execute the binary file. To avoid this, delete the linker parameter for the library, -lpthread in this example, in the Makefile instead of using a dummy library before creating the binary file. Or create a statically linked binary using the static library libpthread.a.


There is now a simple script called create_dummy_lib.sh to create a dummy library with a unique dummy function to satisfy the configure / make files from various Unix tools:

https://github.com/bnsmb/clang19_toolchain_for_android/raw/refs/heads/main/sysroot/bin/create_dummy_lib.sh

example usage:

[clang19 toolchain] 127|ASUS_I006D:/data/local/tmp/develop $ create_dummy_lib.sh util
Creating the library "libutil" ....
-rw-rw-rw- 1 shell shell 1218 2026-02-03 20:37 libutil.a
-rwxrwxrwx 1 shell shell 5272 2026-02-03 20:37 libutil.so
[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop $




Dynamically linked binaries versus statically linked binaries


Unix programs can be either dynamically or statically linked. Most configure scripts for Unix programs create makefiles to build dynamically linked binaries by default, but also support the building of statically linked binaries.

Both methods have advantages and disadvantages (for more information, see Building Dynamically Linked Binaries for Android Using C or C++)


Since statically linked binary files cannot use hostname resolution in Android, I recommend creating dynamically linked binary files, but only for the standard Android OS libraries, e.g.:
 
[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot/usr/bin $ file $PWD/wget2
/data/local/tmp/sysroot/usr/bin/wget2: ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64), for Android 28, built by NDK r25 (8775105), stripped
[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot/usr/bin $

[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot/usr/bin $ ldd $PWD/wget2

    linux-vdso.so.1 => [vdso] (0x7d96df3000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x7d915cd000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x7d91602000)
[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot/usr/bin $


Binaries compiled this way should run on any Android OS.



How to force the addition of a static library to an executable file


Sometimes it makes sense to add all the functions of a static library to an executable file, even if they are not required for linking the executable file (e.g., because the functions from the library are only used when the program is executed).

To force the linker from clang to do that these parameter can be used:

-Wl,--whole-archive <archive_name> -Wl,--no-whole-archive

example:

# add all functions from the library libcrypt.a:
#
-Wl,--whole-archive  /data/local/tmp/develop/sysroot/usr/lib/libcrypt.a -Wl,--no-whole-archive



Runtime libraries for C++ programs compiled with clang


When compiling a C++ program with the clang19 toolchain, clang++ adds the library  libc++_shared.so as dependency for a compiled C++ program.

Example:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/protobuf-32.1/build $ myldd protoc

# Executing ldd "/data/local/tmp/develop/protobuf-32.1/build/protoc"
    linux-vdso.so.1 => [vdso] (0x7b7b1ae000)
    liblog.so => /system/lib64/liblog.so (0x7b709d5000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x7b74090000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x7b74c44000)
    libc++_shared.so => /data/local/tmp/sysroot/usr/lib/libc++_shared.so (0x7b70830000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x7b74044000)
    libc++.so => /system/lib64/libc++.so (0x7b74cb0000)
[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/protobuf-32.1/build $


The library
libc++_shared.so is not part of the standard Android OS. Therefore, binaries using that library will not work in Standard Android.

To compile a C++ binary that does not require this library, add these parameter at the end of the clang++ command to link the binary:

 
-nostdlib++ \
/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/libc++_static.a \
/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/libc++abi.a

and remove the parameter -lc++shared from the parameter for clang++.

Note: use the correct path for the static libraries for your environment


The dependencies of the binary then look like this:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/protobuf-32.1/build $ myldd protoc

# Executing ldd "/data/local/tmp/develop/protobuf-32.1/build/protoc"
    linux-vdso.so.1 => [vdso] (0x7682ea0000)
    liblog.so => /system/lib64/liblog.so (0x76809d1000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x767eee1000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x7680a00000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x76809ae000)
    libc++.so => /system/lib64/libc++.so (0x768085e000)
[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/protobuf-32.1/build $

All libraries required by this binary are part of the Standard Android OS.


Notes

According to ChatGPT these compiler flags may also work :

CXXFLAGS += -static-libc++
LDFLAGS  += -static-libc++

see also Runtime libraries for C++ programs compiled with gcc



Runtime libraries for C++ programs compiled with gcc


URL: https://xdaforums.com/t/guide-runtime-libraries-for-c-programs-compiled-with-gcc.4804175/

With the default settings the g++ compiler for Android from the GCC Toolchain for Android creates binaries that need the C++ libraries from the g++ toolchain.

Example:

ASUS_I006D:/system/usr/share/gcc $  g++ -o /data/local/tmp/helloworld_in_c++  helloworld_in_c++.cpp 
ASUS_I006D:/system/usr/share/gcc $

ASUS_I006D:/system/usr/share/gcc $ LD_LIBRARY_PATH=/system/lib64:/system/lib/android-gcc-cross/aarch64-unknown-linux-android/lib ldd /data/local/tmp/helloworld_in_c++

    linux-vdso.so.1 => [vdso] (0x75bde45000)
    libestdc++.so => /system/lib/android-gcc-cross/aarch64-unknown-linux-android/lib/libestdc++.so (0x75b6282000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x75bca83000)
    libegcc.so => /system/lib/android-gcc-cross/aarch64-unknown-linux-android/lib/libegcc.so (0x75b9442000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x75b94a9000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x75bca43000)
ASUS_I006D:/system/usr/share/gcc $

The libraries libestdc++.so and libegcc.so are not part of the standard Android OS. Therefore, these binaries will not work on plain Android.


To compile a C++ binary with g++ that does not require these libraries, add the parameter -static-libstdc++ to the g++ command:

Example:

ASUS_I006D:/system/usr/share/gcc $ g++ -static-libstdc++ -o /data/local/tmp/helloworld_in_c++  helloworld_in_c++.cpp
ASUS_I006D:/system/usr/share/gcc $

ASUS_I006D:/system/usr/share/gcc $ ldd /data/local/tmp/helloworld_in_c++
    linux-vdso.so.1 => [vdso] (0x7b5cfa3000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x7b59705000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x7b568c5000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x7b59743000)
ASUS_I006D:/system/usr/share/gcc $ 


Update 10.10.2026

Another way to force the g++ compiler to use the static runtime library is to set the PINO_STATIC_RUNTIME environment variable to “true”.

Example:

ASUS_I006D:/data/local/tmp/develop/7z2604 $ PINO_STATIC_RUNTIME=true  g++ -o helloworld_in_c++ /system/usr/share/gcc/helloworld_in_c++.cpp
ASUS_I006D:/data/local/tmp/develop/7z2604 $

ASUS_I006D:/data/local/tmp/develop/7z2604 $ myldd ./helloworld_in_c++ 

# Executing ldd "/data/local/tmp/develop/7z2604/./helloworld_in_c++"
    linux-vdso.so.1 => [vdso] (0x76cd065000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x76cbc01000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x76cba8e000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x76cbbc7000)
ASUS_I006D:/data/local/tmp/develop/7z2604 $


Update 11.10.2026

When cross-compiling a program written in C++ for Android in Linux, the environment variable PINO_STATIC_RUNTIME may be ignored by the g++ cross compiler. In this case, use the compiler parameter

-static-libstdc++  -static-libgcc

to force the g++ to use the static libraries for the gcc runtime (libegcc) and the C++ runtime(libestdc++).



see also Runtime libraries for C++ programs compiled with clang




How to force clang to list the used objects and libraries to link a binary


Use the parameter -Wl,-t or -Wl,--trace to print the object files and libraries used when linking a binary

Example:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ clang -Wl,--trace \
>   -I/system/usr/clang19/include \
>   -I/system/usr/ndk/r27b/include  \
>   -I/system/usr/ndk/r27b/sysroot/usr/include \
a>   -I/data/local/tmp/develop/sysroot/usr/include \
>    --sysroot=/system/usr/ndk/r27b/sysroot \
>   -L/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31 \
>   -L/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android \
 >   -L/data/local/tmp/develop/sysroot/usr/lib  \
>   -B/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/ \
>   -lc \
>   -o /data/local/tmp/helloworld_in_c \
>   /system/usr/share/clang_examples/helloworld_in_c.c
/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/crtbegin_dynamic.o
/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/libc.so
/data/local/tmp/helloworld_in_c-f13afc.o
/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/libdl.so
/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/libc.so
/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/libdl.so
/system/usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/31/crtend_android.o
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


Add the parameter -v to also print the search list for the libraries




How to change the libraries required by a binary



To list the libraries required by a binary use either ldd:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ LD_LIBRARY_PATH=/data/local/tmp/develop/selinux/lib ldd $PWD/find
    linux-vdso.so.1 => [vdso] (0x7a63b60000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x7a61442000)
    libselinux.so.1 => /data/local/tmp/develop/selinux/lib/libselinux.so.1 (0x7a6148a000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x7a61587000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x7a5ee48000)
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


or, better, readelf:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -d $PWD/find | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libm.so]
 0x0000000000000001 (NEEDED)             Shared library: [libselinux.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


or use patchelf:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ patchelf --print-needed $PWD/find 
libm.so
libselinux.so
libdl.so
libc.so
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


To replace the name of the required libraries, e.g. replace "selinux.so.1" with "selinux.so", the tool patchelf can be used.

The syntax to replace the name of a library in the binary file is:

patchelf --replace-needed <old_library> <new_library> <binary>


Example:

ASUS_I006D:/data/local/tmp $ ldd $PWD/find

    linux-vdso.so.1 => [vdso] (0x7093097000)
CANNOT LINK EXECUTABLE "linker64": library "libselinux.so.1" not found: needed by main executable
1|ASUS_I006D:/data/local/tmp $


[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -d $PWD/find | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libm.so]
 0x0000000000000001 (NEEDED)             Shared library: [libselinux.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ patchelf --replace-needed libselinux.so.1 libselinux.so ./find
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -d $PWD/find | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libm.so]
 0x0000000000000001 (NEEDED)             Shared library: [libselinux.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $

ASUS_I006D:/data/local/tmp $ ldd $PWD/find
    linux-vdso.so.1 => [vdso] (0x7743362000)
    libm.so => /apex/com.android.runtime/lib64/bionic/libm.so (0x773cc04000)
    libselinux.so => /system/lib64/libselinux.so (0x773cdde000)
    libdl.so => /apex/com.android.runtime/lib64/bionic/libdl.so (0x773e6f6000)
    libc.so => /apex/com.android.runtime/lib64/bionic/libc.so (0x773cc58000)
    libpcre2.so => /system/lib64/libpcre2.so (0x773e661000)
    liblog.so => /system/lib64/liblog.so (0x773e75c000)
    libpackagelistparser.so => /system/lib64/libpackagelistparser.so (0x773e704000)
    libc++.so => /system/lib64/libc++.so (0x773cac6000)
ASUS_I006D:/data/local/tmp $



patchelf can also be used to remove a library from the list of libraries required for a binary. The syntax for this function is (e.g. to remove the threads library, libpthread.so, which is not used in Android):

patchelf --remove-needed <library_name> binary


Use the tool nm to check, if the library is really not required for the binary

Example:

[clang19 vdisk env] 1|ASUS_I006D:/data/local/tmp $ nm -D find | grep pthread
                 U pthread_mutex_destroy@LIBC
                 U pthread_mutex_init@LIBC
                 U pthread_mutex_lock@LIBC
                 U pthread_mutex_unlock@LIBC
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $

-> all pthread symbols used by this binary are defined in the library libc (@LIBC) and the library libpthread.so can be removed from the list of required libraries.

You can also use readelf to print the list of symbols in the binary file

Example:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -Ws find | grep pthread     
   123: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND pthread_mutex_init@LIBC (2)
   124: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND pthread_mutex_destroy@LIBC (2)
   126: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND pthread_mutex_lock@LIBC (2)
   127: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND pthread_mutex_unlock@LIBC (2)
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


The library name added to a binary by the linker is the SONAME defined in the library. The SONAME can be different to the filename.

Example:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -d /data/local/tmp/develop/selinux/lib/libselinux.so | grep SONAME
 0x000000000000000e (SONAME)             Library soname: [libselinux.so.1]
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


patchelf can also be used to change the SONAME in a library, the syntax for this modification is:

patchelf --set-soname <new_soname> <library_name>


Example:

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -d /data/local/tmp/develop/selinux/lib/libselinux.so | grep SONAME
 0x000000000000000e (SONAME)             Library soname: [libselinux.so.1]
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $

[clang19 vdisk env] 1|ASUS_I006D:/data/local/tmp $ patchelf --set-soname libselinux.so  /data/local/tmp/develop/selinux/lib/libselinux.so
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $

[clang19 vdisk env] ASUS_I006D:/data/local/tmp $ readelf -d /data/local/tmp/develop/selinux/lib/libselinux.so | grep SONAME
 0x000000000000000e (SONAME)             Library soname: [libselinux.so]
[clang19 vdisk env] ASUS_I006D:/data/local/tmp $


To change the SONAME while building a library, use the linker parameter

-Wl,-soname,<so_name_for_the_library>


Notes

A readelf binary and a patchelf binary compiled for Android on arm64 CPUs is available here:

http://bnsmb.de/files/public/Android/binaries_for_arm64/readelf

http://bnsmb.de/files/public/Android/binaries_for_arm64/patchelf


Both binaries are also available in the GitHub repository with binaries for Android on arm64 CPUs:

https://github.com/bnsmb/binaries-for-Android/


The source code for patchelf is in this repository:

https://github.com/nixos/patchelf



Testing the compiled binary


The Makefiles for many tools contain code for testing the compiled binaries. In most cases, the command to run the test suite is:

make check

or

make test

It is strongly recommended that you use this feature to test the binaries.



Sample configure wrapper


I use this template for a wrapper script for configure to create makefiles for compiling Unix binaries:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot $ cat myconfigure
#
# template file with ./configure command for using with the clang19 tool chain
#
#

# use these addtional CFLAGs for binaries should be executed using a debugger like gdb
#
#DEBUG_CFLAGS="-g -O0"

DEBUG_CFLAGS=""

# these configure options are required to compile the source code of a lot of  "old" unix programs with clang
#
CFLAGS="${CFLAGS} -Wno-implicit-function-declaration -Wno-int-conversion ${DEBUG_CFLAGS}"


# add additional CFLAGS for the compiler here
#
CFLAGS="${CFLAGS} "

# C++ compiler options
#
CXXFLAGS="${CFLAGS}"

# add additional LDFLAGS for the linker here
#
LDFLAGS="${LDFLAGS} "


# add additional configure options here (either one per line or multiple options on one line)
#
CONFIGURE_OPTIONS="
"

# target directory used by "make install"
#
TARGET_ROOT="${TARGET_ROOT:=/data/local/tmp/develop/sysroot}"

#
# set PREFIX to "echo" or something similar before calling this script to run it in dry-run mode
#
${PREFIX}  ./configure  --prefix=${TARGET_ROOT}/usr/ CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" ${CONFIGURE_OPTIONS} $*

[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot $



General trouble shooting hints


This section contains general hints for trouble shooting compile errors.



How to print the default settings for the compiler



To print the default settings for gcc or clang execute the command:

$CC -v -E -x - 2>&1

(where $CC is the compiler used - either gcc or clang)



How to list the compiler builtin macros


To print all gcc or clang builtin Macros use the command

$CC  -dM -E - < /dev/null

(where $CC is the compiler used - either gcc or clang)



How to print the search path for include files


To print the search path for include files used by the compiler execute this command:

echo | $CC -E -Wp,-v - 2>&1 | grep -A 20 "search starts"

(where $CC is the compiler used - either gcc or clang)


To check, if an include file is found, use this command:

echo '#include <curses.h>' | gcc -E - 2>&1 | head -5

e.g

ASUS_I006D:/ $ echo '#include <curses.h>' | gcc -E - 2>&1 | head -5
<stdin>:1:10: fatal error: curses.h: No such file or directory
compilation terminated.
# 0 "<stdin>"
# 0 "<built-in>"
# 0 "<command-line>"
ASUS_I006D:/ $

ASUS_I006D:/ $ ( export CPATH=/data/local/tmp/develop/sysroot/usr/include ;echo '#include <curses.h>' | gcc -E - 2>&1 | head -5 )
# 0 "<stdin>"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "<stdin>"
# 1 "/data/local/tmp/develop/sysroot/usr/include/curses.h" 1
ASUS_I006D:/ $


Or use this syntax:

echo '#include <glob.h>' | clang  $CFLAGS -E -H - 1>/dev/null

echo '#include <glob.h>' | gcc  $CFLAGS -E -H - 1>/dev/null


Example
[clang19 toolchain] [shell@localhost /data/local/tmp/develop]$  echo '#include <glob.h>' | clang  $CFLAGS -E -H - 1>/dev/null
. /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/glob.h
.. /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/sys/cdefs.h
... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/android/versioning.h
... /data/local/tmp/sysroot/usr/include/android/api-level.h
... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/android/ndk-version.h
.. /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/sys/types.h
... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/stddef.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_header_macro.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_ptrdiff_t.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_size_t.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_wchar_t.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_null.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_max_align_t.h
.... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_offsetof.h
... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/stdint.h
.... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdint.h
..... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/bits/wchar_limits.h
..... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/stddef.h
...... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_ptrdiff_t.h
...... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_size_t.h
...... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_wchar_t.h
...... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_null.h
...... /data/local/tmp/sysroot/usr/clang19/lib/clang/19/include/__stddef_offsetof.h
... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/linux/types.h
.... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/aarch64-linux-android/asm/types.h
..... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/asm-generic/types.h
...... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/asm-generic/int-ll64.h
....... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/aarch64-linux-android/asm/bitsperlong.h
........ /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/asm-generic/bitsperlong.h
.... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/linux/posix_types.h
..... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/linux/stddef.h
...... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/linux/compiler_types.h
....... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/linux/compiler.h
..... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/aarch64-linux-android/asm/posix_types.h
...... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/asm-generic/posix_types.h
... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/bits/pthread_types.h
.... /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/sys/types.h
[clang19 toolchain] [shell@localhost /data/local/tmp/develop]$

[clang19 toolchain] [shell@localhost /data/local/tmp/develop]$  echo '#include <glob.h>' | gcc  $CFLAGS -E -H - 1>/dev/null
. /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/glob.h
.. /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/sys/cdefs.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/android/versioning.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/android/api-level.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/android/ndk-version.h
.. /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/sys/types.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/lib/gcc/aarch64-unknown-linux-android/16/include/stddef.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/lib/gcc/aarch64-unknown-linux-android/16/include/stdint.h
.... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/stdint.h
..... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/bits/wchar_limits.h
..... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/lib/gcc/aarch64-unknown-linux-android/16/include/stddef.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/linux/types.h
.... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/aarch64-unknown-linux-android/asm/types.h
..... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/asm-generic/types.h
...... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/asm-generic/int-ll64.h
....... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/aarch64-unknown-linux-android/asm/bitsperlong.h
........ /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/asm-generic/bitsperlong.h
.... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/linux/posix_types.h
..... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/linux/stddef.h
...... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/linux/compiler_types.h
....... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/linux/compiler.h
..... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/aarch64-unknown-linux-android/asm/posix_types.h
...... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/asm-generic/posix_types.h
... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/bits/pthread_types.h
.... /data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/sys/types.h
Multiple include guards may be useful for:
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/aarch64-unknown-linux-android31/include/aarch64-unknown-linux-android/asm/types.h
[clang19 toolchain] [shell@localhost /data/local/tmp/develop]$




Note

Please note that the gcc and g++ compiler from https://github.com/AmanoTeam/android-gcc-cross (which is part of the clang19 toolchain) ignore include directories whose names contain sysroot.

Update 11.10.2026

This restriction does not exist anymore in the gcc compiler from the recent gcc toolchain.



How to print the output files the C compiler would create



To print the files a C compiler would create without really creating the files execute these commands


# with gcc or g++ from the gcc toolchain
#
[xtrnaw7@t15g /data/develop/android/cross_compile]$ aarch64-unknown-linux-android37-g++  -###  helloworld_in_c++.c  -o helloworld_in_c++ 2>&1 | grep -oP '(?<=-o )\S+'
/tmp/ccnTnC5C.s
/tmp/ccISxnX5.o
"helloworld_in_c++"
[xtrnaw7@t15g /data/develop/android/cross_compile]$


# with clang from the gcc toolchain
#
xtrnaw7@t15g /data/develop/android/cross_compile]$ aarch64-unknown-linux-android37-clang++  -###  helloworld_in_c++.c  -o helloworld_in_c++ 2>&1 | grep -oP '(?<=-o )\S+'
/tmp/ccjESwGe.s
/tmp/ccoafZdj.o
"helloworld_in_c++"
[xtrnaw7@t15g /data/develop/android/cross_compile]$


# with clang from the clang19 toolchain
#
[clang19 toolchain] [shell@localhost /data/local/tmp/sysroot]$ clang -### examples/helloworld_in_c.c -o out/helloworld_in_c++ 2>&1   | ggrep -oP '(?<="-o" ")[^"]+'
/data/local/tmp/helloworld_in_c-425f03.o
out/helloworld_in_c++
[clang19 toolchain] [shell@localhost /data/local/tmp/sysroot]$




How to print verbose make messages to STDOUT


Use the parameter

V=1

or

VERBOSE=1

for make to enable verbose compile messages



How to print verbose cmake messages to STDOUT


Use the parameter

-DCMAKE_VERBOSE_MAKEFILE=ON

for cmake to enable verbose make messages

 

How to print messages to STDOUT while compiling


To instruct the preprocessor to print messages while compiling C source files, these instructions can be used:

#pragma message "Compiling my program..."

#warning "This is just a test warning"

#ifndef MY_DEFINE
    #error "MY_DEFINE is not set!"
#endif

Note that the compile fails after printing the error message!



How to print messages to the Android log


To print a message into the Android log (logcat) from an C/C++ program while executing the programm use these instructions


#ifdef __ANDROID__
#include <android/log.h>
#endif

/*
 

 "ANDROID_LOG_INFO" is the priority for the log message

  see the file

   ./sysroot/usr/include/android/log.h

  in the Android NDK for other log priorities
 
 "btop" can be any string to distinguish the messages in logcat

 "val" is the variable to print in this example

 "debut: value=%d" is the string to print to logcat


*/

#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, "btop", "debug: value=%d", val);
#endif


and add the log library to the LDFLAGS:

LDFLAGS += -llog


To view the messages when executing the program use this command:

logcat -s btop

or print only messages with the priority Info and higher:

logcat btop:I* *:S


e.g.:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot $ logcat -d -s btop
--------- beginning of main
09-20 10:26:28.281 17652 17652 I btop    : debug: value=55555
09-20 10:32:46.376 18060 18060 I btop    : debug: value=6666
09-20 10:33:25.832 18075 18075 I btop    : debug: value=6666
[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot $

[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot $ logcat -d btop:I* *:S
--------- beginning of main
09-20 10:26:28.281 17652 17652 I btop    : debug: value=55555
09-20 10:32:46.376 18060 18060 I btop    : debug: value=6666
09-20 10:33:25.832 18075 18075 I btop    : debug: value=6666
[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot $



Useful parameter for CMAKE



Use the parameter -L for CMAKE to list the variables supported by the current CMAKE config file:

-L[A][H]

    List non-advanced cached variables.

    List CACHE variables will run CMake and list all the variables from the CMake CACHE that are not marked as INTERNAL or ADVANCED. This will effectively display current CMake settings, which can then be changed with -D option. Changing some of the variables may result in more variables being created. If A is specified, then it will display also advanced variables. If H is specified, it will also display help for each variable.

-LR[A][H] <regex>

    Added in version 3.31.

    Show specific non-advanced cached variables

    Show non-INTERNAL nor ADVANCED variables from the CMake CACHE that match the given regex. If A is specified, then it will also show advanced variables. If H is specified, it will also display help for each variable.


examples:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/popt/build $ cmake -L ..
-- Could NOT find Iconv (missing: Iconv_LIBRARY Iconv_INCLUDE_DIR)
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /data/local/tmp/develop/popt/build
-- Cache values
BUILD_DOCUMENTATION:BOOL=OFF
BUILD_TESTING:BOOL=ON
CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
CMAKE_INSTALL_PREFIX:PATH=/usr/local
ENABLE_ASAN:BOOL=OFF
ENABLE_UBSAN:BOOL=OFF
ENABLE_WERROR:BOOL=OFF
SH_PROGRAM:FILEPATH=/system/bin/sh
[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/popt/build $

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/popt/build $ cmake -LH ..                                                                                               
-- Could NOT find Iconv (missing: Iconv_LIBRARY Iconv_INCLUDE_DIR)
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /data/local/tmp/develop/popt/build
-- Cache values
// Create and install the HTML based API documentation (requires Doxygen)
BUILD_DOCUMENTATION:BOOL=OFF

// Build the testing tree.
BUILD_TESTING:BOOL=ON

// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=RelWithDebInfo

// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local

// Enable address-sanitizer
ENABLE_ASAN:BOOL=OFF

// Enable undefined behavior-sanitizer
ENABLE_UBSAN:BOOL=OFF

// Stop build on warnings
ENABLE_WERROR:BOOL=OFF

// Path to a program.
SH_PROGRAM:FILEPATH=/system/bin/sh

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/popt/build $



cmake -LAH  .. 
[clang19 toolchain] 130|ASUS_I006D:/data/local/tmp/develop/popt/build $ cmake -LAH  ..
-- Could NOT find Iconv (missing: Iconv_LIBRARY Iconv_INCLUDE_DIR)
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /data/local/tmp/develop/popt/build
-- Cache values
// Create and install the HTML based API documentation (requires Doxygen)
BUILD_DOCUMENTATION:BOOL=OFF

// Build the testing tree.
BUILD_TESTING:BOOL=ON

// Path to a program.
CMAKE_ADDR2LINE:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-addr2line

// Path to a program.
CMAKE_AR:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-ar

// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=RelWithDebInfo

// Enable/Disable color output during build.
CMAKE_COLOR_MAKEFILE:BOOL=ON

// C compiler
CMAKE_C_COMPILER:STRING=/data/local/tmp/sysroot/usr/clang19/bin/clang

// LLVM archiver
CMAKE_C_COMPILER_AR:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-ar

// `clang-scan-deps` dependency scanner
CMAKE_C_COMPILER_CLANG_SCAN_DEPS:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/clang-scan-deps

// Generate index for LLVM archive
CMAKE_C_COMPILER_RANLIB:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-ranlib

// Flags used by the C compiler during all build types.
CMAKE_C_FLAGS:STRING=-I/data/local/tmp/sysroot/usr/include -I/data/local/tmp/sysroot/usr/clang19/include -I/data/local/tmp/sysroot/usr/ndk/r27d/include -I/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/aarch64-linux-android -I/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include -I/data/local/tmp/develop/sysroot/usr/include --sysroot=/data/local/tmp/sysroot/usr/ndk/r27d/sysroot

// Flags used by the C compiler during DEBUG builds.
CMAKE_C_FLAGS_DEBUG:STRING=-g

// Flags used by the C compiler during MINSIZEREL builds.
CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG

// Flags used by the C compiler during RELEASE builds.
CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG

// Flags used by the C compiler during RELWITHDEBINFO builds.
CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

// Path to a program.
CMAKE_DLLTOOL:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-dlltool

// Flags used by the linker during all build types.
CMAKE_EXE_LINKER_FLAGS:STRING=-L/data/local/tmp/sysroot/usr/lib -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31 -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android -L/data/local/tmp/develop/sysroot/usr/lib -B/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31/ --sysroot=/data/local/tmp/sysroot/usr/ndk/r27d/sysroot -lc

// Flags used by the linker during DEBUG builds.
CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=

// Flags used by the linker during MINSIZEREL builds.
CMAKE_EXE_LINKER_FLAGS_MINSIZEREL:STRING=

// Flags used by the linker during RELEASE builds.
CMAKE_EXE_LINKER_FLAGS_RELEASE:STRING=

// Flags used by the linker during RELWITHDEBINFO builds.
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO:STRING=

// Enable/Disable output of compile commands during generation.
CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=

// User executables (bin)
CMAKE_INSTALL_BINDIR:PATH=bin

// Read-only architecture-independent data (DATAROOTDIR)
CMAKE_INSTALL_DATADIR:PATH=

// Read-only architecture-independent data root (share)
CMAKE_INSTALL_DATAROOTDIR:PATH=share

// Documentation root (DATAROOTDIR/doc/PROJECT_NAME)
CMAKE_INSTALL_DOCDIR:PATH=

// C header files (include)
CMAKE_INSTALL_INCLUDEDIR:PATH=include

// Info documentation (DATAROOTDIR/info)
CMAKE_INSTALL_INFODIR:PATH=

// Object code libraries (lib)
CMAKE_INSTALL_LIBDIR:PATH=lib

// Program executables (libexec)
CMAKE_INSTALL_LIBEXECDIR:PATH=libexec

// Locale-dependent data (DATAROOTDIR/locale)
CMAKE_INSTALL_LOCALEDIR:PATH=

// Modifiable single-machine data (var)
CMAKE_INSTALL_LOCALSTATEDIR:PATH=var

// Man documentation (DATAROOTDIR/man)
CMAKE_INSTALL_MANDIR:PATH=

// C header files for non-gcc (/usr/include)
CMAKE_INSTALL_OLDINCLUDEDIR:PATH=/usr/include

// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local

// Run-time variable data (LOCALSTATEDIR/run)
CMAKE_INSTALL_RUNSTATEDIR:PATH=

// System admin executables (sbin)
CMAKE_INSTALL_SBINDIR:PATH=sbin

// Modifiable architecture-independent data (com)
CMAKE_INSTALL_SHAREDSTATEDIR:PATH=com

// Read-only single-machine data (etc)
CMAKE_INSTALL_SYSCONFDIR:PATH=etc

// Path to a program.
CMAKE_LINKER:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/ld.lld

// Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/make

// Flags used by the linker during the creation of modules during all build types.
CMAKE_MODULE_LINKER_FLAGS:STRING=-L/data/local/tmp/sysroot/usr/lib -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31 -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android -L/data/local/tmp/develop/sysroot/usr/lib -B/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31/ --sysroot=/data/local/tmp/sysroot/usr/ndk/r27d/sysroot -lc

// Flags used by the linker during the creation of modules during DEBUG builds.
CMAKE_MODULE_LINKER_FLAGS_DEBUG:STRING=

// Flags used by the linker during the creation of modules during MINSIZEREL builds.
CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL:STRING=

// Flags used by the linker during the creation of modules during RELEASE builds.
CMAKE_MODULE_LINKER_FLAGS_RELEASE:STRING=

// Flags used by the linker during the creation of modules during RELWITHDEBINFO builds.
CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO:STRING=

// Path to a program.
CMAKE_NM:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-nm

// Path to a program.
CMAKE_OBJCOPY:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-objcopy

// Path to a program.
CMAKE_OBJDUMP:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-objdump

// Path to a program.
CMAKE_RANLIB:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-ranlib

// Path to a program.
CMAKE_READELF:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-readelf

// Flags used by the linker during the creation of shared libraries during all build types.
CMAKE_SHARED_LINKER_FLAGS:STRING=-Wl,--undefined-version  -L/data/local/tmp/sysroot/usr/lib -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31 -L/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android -L/data/local/tmp/develop/sysroot/usr/lib -B/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31/ --sysroot=/data/local/tmp/sysroot/usr/ndk/r27d/sysroot -lc

// Flags used by the linker during the creation of shared libraries during DEBUG builds.
CMAKE_SHARED_LINKER_FLAGS_DEBUG:STRING=

// Flags used by the linker during the creation of shared libraries during MINSIZEREL builds.
CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL:STRING=

// Flags used by the linker during the creation of shared libraries during RELEASE builds.
CMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING=

// Flags used by the linker during the creation of shared libraries during RELWITHDEBINFO builds.
CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO:STRING=

// If set, runtime paths are not added when installing shared libraries, but are added when building.
CMAKE_SKIP_INSTALL_RPATH:BOOL=NO

// If set, runtime paths are not added when using shared libraries.
CMAKE_SKIP_RPATH:BOOL=NO

// Flags used by the linker during the creation of static libraries during all build types.
CMAKE_STATIC_LINKER_FLAGS:STRING=

// Flags used by the linker during the creation of static libraries during DEBUG builds.
CMAKE_STATIC_LINKER_FLAGS_DEBUG:STRING=

// Flags used by the linker during the creation of static libraries during MINSIZEREL builds.
CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL:STRING=

// Flags used by the linker during the creation of static libraries during RELEASE builds.
CMAKE_STATIC_LINKER_FLAGS_RELEASE:STRING=

// Flags used by the linker during the creation of static libraries during RELWITHDEBINFO builds.
CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO:STRING=

// Path to a program.
CMAKE_STRIP:FILEPATH=/data/local/tmp/sysroot/usr/clang19/bin/llvm-strip

// Path to a program.
CMAKE_TAPI:FILEPATH=CMAKE_TAPI-NOTFOUND

// If this value is on, makefiles will be generated without the .SILENT directive, and all commands will be echoed to the console during the make.  This is useful for debugging only. With Visual Studio IDE projects all commands are done without /nologo.
CMAKE_VERBOSE_MAKEFILE:BOOL=FALSE

// Path to the coverage program that CTest uses for performing coverage inspection
COVERAGE_COMMAND:FILEPATH=COVERAGE_COMMAND-NOTFOUND

// Extra command line flags to pass to the coverage tool
COVERAGE_EXTRA_FLAGS:STRING=-l

// How many times to retry timed-out CTest submissions.
CTEST_SUBMIT_RETRY_COUNT:STRING=3

// How long to wait between timed-out CTest submissions.
CTEST_SUBMIT_RETRY_DELAY:STRING=5

// Maximum time allowed before CTest will kill the test.
DART_TESTING_TIMEOUT:STRING=1500

// Dot tool for use with Doxygen
DOXYGEN_DOT_EXECUTABLE:FILEPATH=DOXYGEN_DOT_EXECUTABLE-NOTFOUND

// Doxygen documentation generation tool (https://www.doxygen.nl)
DOXYGEN_EXECUTABLE:FILEPATH=DOXYGEN_EXECUTABLE-NOTFOUND

// Enable address-sanitizer
ENABLE_ASAN:BOOL=OFF

// Enable undefined behavior-sanitizer
ENABLE_UBSAN:BOOL=OFF

// Stop build on warnings
ENABLE_WERROR:BOOL=OFF

// Path to a program.
GITCOMMAND:FILEPATH=/data/local/tmp/sysroot/usr/bin/git

// iconv include directory
Iconv_INCLUDE_DIR:PATH=Iconv_INCLUDE_DIR-NOTFOUND

// iconv library (if not in the C library)
Iconv_LIBRARY:FILEPATH=Iconv_LIBRARY-NOTFOUND

// Command to build the project
MAKECOMMAND:STRING=/data/local/tmp/sysroot/usr/bin/cmake --build . --config "${CTEST_CONFIGURATION_TYPE}"

// Path to the memory checking command, used for memory error detection.
MEMORYCHECK_COMMAND:FILEPATH=MEMORYCHECK_COMMAND-NOTFOUND

// File that contains suppressions for the memory checker
MEMORYCHECK_SUPPRESSIONS_FILE:FILEPATH=

// Path to a program.
SH_PROGRAM:FILEPATH=/system/bin/sh

// Name of the computer/site where compile is being run
SITE:STRING=localhost

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/popt/build $

---

Use the CMAKE parameter -D or -U to define or undefine one of the variables

Use the CMAKE parameter --install-prefix to define the installation directory.


see also https://cmake.org/cmake/help/latest/manual/cmake.1.html 


cmake Logfiles



The log files from cmake are in the directory ./CMakeFiles in the build directory.

The important log files are:

CMakeOutput.log

CMakeError.log

and

CMakeConfigureLog.yaml


check the file CMakeConfigureLog.yaml if the file CMakeError.log does not exist.




How to debug a binary in Android


To debug a program in Android on the phone the debugger gdb can be used.

The gdb binary compiled for arm64 CPUs is available for download from various locations:


The gdb binary is part of the clang19 toolchain

The statically linked gdb binary is also available here:

http://bnsmb.de/files/public/Android/binaries_for_arm64/static_gdb/

and in my git repository with binaries for Android:

https://github.com/bnsmb/binaries-for-Android/


And the gdb binary for arm64 CPUs is part of the Magisk Module with Debug Tools.


See the gdb home page https://sourceware.org/gdb/documentation for the documentation for gdb.


Some short hints:


Use the parameter -tui for the text user interface:

gdb -tui  bvi 

e.g.:




Do not add the parameter for the program to debug in the gdb command line; add the parameter for the program after the gdb command run.

A cheat sheet for gdb is available here: https://github.com/reveng007/GDB-Cheat-Sheet?tab=readme-ov-file 

See https://sourceware.org/gdb/documentation for the detailed documentation for gdb.

The warning messages about missing sections in the standard OS libraries can be ignored.

In this example, the binary was compiled on the phone using the clang19 toolchain and therefore the source code is available on the phone.

It's recommended to compile the binary with the compiler options for debugging support .



Note


If gdb prints garbage, such as




either change the value of the environment variable TERM before calling gdb or set the environment variable NO_COLOR to 1 before starting gdb , e.g

NO_COLOR=1 ../../sysroot/usr/bin/gdb -tui <other_parameter_for_gdb>


Or create a config file for gdb with the necessary setting to disable color detection:

mkdir -p ~/.config/gdb
echo "set style enabled off" > ~/.config/gdb/gdbearlyinit



How to debug a binary from Android with gdb


URL: https://xdaforums.com/t/guide-how-to-debug-a-binary-from-android-with-gdb.4763780/


How to debug a binary from Android

In most Android OS versions, the executable vi fails with a segmentation fault after executing these vi commands:

:w
:q


To find the reason for this error, you can use the debugger gdb. (gdb is NOT part of the standard Android OS -- see here for instructions to get gdb for Android.

Note that root access is not required for any of the instructions in this guide.
And as in all my guides, the commands for the PC apply to the Linux operating system.


To debug vi using the debugger gdb, load the binary "/system/bin/vi" into the debugger gdb (ignore the gdb warnings about missing sections):

gdb -tui /system/bin/vi





Now start vi to edit a text file (/data/local/tmp/get-pip.py in this example) in gdb using the gdb command run at the (gdb) prompt:


run /data/local/tmp/get-pip.py


and execute the vi commands

:w
:q

in vi to force the segmentation fault.


The segmentation fault is caught by gdb, but the output from gdb is not that useful:





The reason for this is that the vi binary file from Android does not contain debug information.

In most Android operating systems, vi is implemented as a function of toybox:

ASUS_I006D:/ $ file $( which vi )
/system/bin/vi: symbolic link to toybox
ASUS_I006D:/ $

and toybox does not contain debug infos:

ASUS_I006D:/ $ file $( which toybox )                                                                                    
/system/bin/toybox: ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64), for Android 35, BuildID=56478129a3bc7c3d98a67bf34f70f927, stripped
ASUS_I006D:/ $


Therefore, we need a toybox binary with debug infos to find the reason for the error with gdb.

Notes:

Tthe following instructions assume that you have a copy of the repositories containing the source code for the Custom ROM you're using (see below if you do not have a local repository for a Custom ROM)



AFAIK, the toybox binary in Android is always created without debug infos (even in -eng or -userdebug builds). But there might be a toybox binary with debug infos in the intermediate directory in the ./out directory tree; use the find command to check this:


# change the working directory to the top level directory with the repositories for the Custom ROM
#
[xtrnaw7@t15g ~]$  cd /devpool001/develop/LineageOS_22.2_original
[xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$


[xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$ find out/soong/.intermediates/external/toybox/ -type f -name toybox -exec file {} \; | grep ARM | sed "s/$/\n/g"

out/soong/.intermediates/external/toybox/toybox_recovery/android_recovery_arm64_armv8-2a-dotprod_cortex-a76/unstripped/toybox: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[md5/uuid]=56478129a3bc7c3d98a67bf34f70f927, with debug_info, not stripped

out/soong/.intermediates/external/toybox/toybox_recovery/android_recovery_arm64_armv8-2a-dotprod_cortex-a76/toybox: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[md5/uuid]=56478129a3bc7c3d98a67bf34f70f927, stripped

out/soong/.intermediates/external/toybox/toybox/android_arm64_armv8-2a-dotprod_cortex-a76/unstripped/toybox: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[md5/uuid]=65e4e2892265c241925096fc364677be, with debug_info, not stripped

out/soong/.intermediates/external/toybox/toybox/android_arm64_armv8-2a-dotprod_cortex-a76/toybox: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[md5/uuid]=1258d51891b5d0fa2db0cc0b89ec5449, with debug_info, not stripped

[xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$


In this example the file

out/soong/.intermediates/external/toybox/toybox/android_arm64_armv8-2a-dotprod_cortex-a76/unstripped/toybox

can be used.

Note

If you don't have a toybox binary file with debug infos, you can compile one -- see below for instructions



To use the toybox binary with debug infos, copy the file to the phone and create a symbolic link for vi:


adb push out/soong/.intermediates/external/toybox/toybox/android_arm64_armv8-2a-dotprod_cortex-a76/unstripped/toybox /data/local/tmp/toybox

adb shell ln -s ./toybox /data/local/tmp/vi


Load the vi with debug infos into gdb

gdb -tui /data/local/tmp/vi

and execute it in gdb using the gdb command run at the (gdb) prompt:

run /data/local/tmp/get-pip.py

Then execute these commands in vi to force the segmentation fault

:w
:q


The segmentation fault is again caught by gdb but the output from gdb is now more detailed :





To get more infos, the gdb command bt can be used :





With this information, you can check the source code of toybox in the repositories on the PC:


vi -c ":set number" +329 external/toybox/toys/pending/vi.c





So the error occurs in the function modified. To debug that function in gdb, the source code for toybox must be copied to the phone:


adb shell mkdir /data/local/tmp/external

# Note that "adb push external/toybox" does not work because non-root user on the phone are not allowed to create symbolic links
#
( cd external && tar -cf - ./toybox/ ) | adb shell "cd /data/local/tmp/external && tar -xf - "


Note:

use the same directory structure on your phone as in the repository with the source code



Now gdb can use the source code of toybox :


# load the new vi binary into gdb
#
# Note: The parameter "--directory=/data/local/tmp" points to the directory with the source code
#
gdb -tui --directory=/data/local/tmp  /data/local/tmp/vi

 



Set a breakpoint to the start of the function modified at the (gdb) prompt:

break modified

and run the binary:

run /data/local/tmp/get-pip.py


The execution now stops in the function modified so that you can use the debugger commands to execute the function in single step mode, view variable contents, or what ever is necessary to find the reason for the error:
(see a https://github.com/reveng007/GDB-Cheat-Sheet?tab=readme-ov-file or https://www.yolinux.com/TUTORIALS/GDB-Commands.html for a gdb command overview)





Note that the function modified is executed two times in this example: Once after entering the vi command ":w" and again after entering the command ":q".

To set an additional break point, for example in the function, calling the function modified, use the gdb command break again:

break run_ex_cmd



Notes


The workaround for this error is to use the vi command ":q!" instead of ":q" .




To create a toybox binary with debug infos, these instructions can be used:

Change the config file for creating the toybox binary to force soong to create a non-stripped binary:

Add the lines

    cflags: [
        "-O0",
        "-g3",
        "-fno-omit-frame-pointer",
        "-fno-inline",
    ],

    strip: {
        none: true,
    },


to the section cc_binary for toybox in the file external/toybox/Android.bp, e.g.:


# old definition:

cc_binary {
    name: "toybox",
    defaults: ["toybox-shared-defaults"],
    host_supported: true,
    vendor_ramdisk_available: true,
}

# new definition:

cc_binary {
    name: "toybox",
    defaults: ["toybox-shared-defaults"],
    host_supported: true,
    vendor_ramdisk_available: true,
    cflags: [
        "-O0",
        "-g3",
        "-fno-omit-frame-pointer",
        "-fno-inline",
    ],

    strip: {
        none: true,
    },
}


or as patch:

xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$ cat toybox_debug.patch
--- external/toybox/Android.bp.org    2025-10-13 10:21:10.693029636 +0200
+++ external/toybox/Android.bp    2025-10-13 10:21:35.510271798 +0200
@@ -528,6 +528,16 @@
     defaults: ["toybox-shared-defaults"],
     host_supported: true,
     vendor_ramdisk_available: true,
+    cflags: [
+        "-O0",
+        "-g3",
+        "-fno-omit-frame-pointer",
+        "-fno-inline",
+    ],
+
+    strip: {
+        none: true,
+    },
 }
 
 cc_binary {
[xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$



Execute

patch -p0  <toybox_debug.patch

in the top directory with the Custom ROM source files to apply the patch.


Now recreate the toxbox binary:

source build/envsetup.sh

lunch lineage_sake-bp1a-user

m toybox


The result should look like this:

[xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$  file  out/target/product/sake/system/bin/toybox
out/target/product/sake/system/bin/toybox: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[md5/uuid]=1258d51891b5d0fa2db0cc0b89ec5449, with debug_info, not stripped
[xtrnaw7@t15g /devpool001/develop/LineageOS_22.2_original]$



The instructions are for working with the ASUS Zenfone 8 (= "sake"); to use the commands for other phones simply replace the string "sake" in the commands with the code for your phone.


For those who do not have a local repository for a Custom ROM:

A toybox binary for arm64 CPUs with debug infos for testing the debugger is available here:

http://bnsmb.de/files/public/Android/binaries_for_arm64/toybox_with_debug_infos

and here

https://github.com/bnsmb/binaries-for-Android/raw/refs/heads/main/binaries/toybox_with_debug_infos


The source code for toybox can be downloaded from the original google repository using this command:

git clone https://android.googlesource.com/platform/external/toybox

The size of the repository with toybox is approximately 45 MB.
A git binary for Android is part of the clang19 toolchain.


Trouble Shooting

If you get an error like this starting gdb:

ASUS_I006D:/ $ gdb -tui /data/local/tmp/vi
Cannot enable the TUI: error opening terminal [TERM=xterm-256color]
1|ASUS_I006D:/ $

set the variable TERMINFO before starting gdb, e.g.

export TERMINFO=/system_ext/etc/terminfo


The directory with the terminfo database must exist. The directory might be in another location, use the command

ls -ld /*/etc/terminfo

to find the location.

Check whether the config file for the configured terminal ($TERM) exists:

ASUS_I006D:/data/local/tmp $ ls -ld $TERMINFO/*/$TERM
-rw-r--r-- 1 root root 1282 2009-01-01 01:00 /system_ext/etc/terminfo/v/vt100
ASUS_I006D:/data/local/tmp $


If the configuration for the terminal is missing from the terminfo database, change the value of the variable TERM to one of the existing configurations in the terminfo database, this config should always work:

export TERM=vt100



If there is no terminfo directory on your phone, download the tar file with a terminfo database

http://bnsmb.de/files/public/Android/tar_files/terminfo.tar

Copy it to the phone and extract the tar file in the directory /data/local/tmp.

Then use this command to start gdb:

TERMINFO=/data/local/tmp/terminfo TERM=vt100 gdb -tui --directory=/data/local/tmp /data/local/tmp/vi

or, if your terminal supports colors:

TERMINFO=/data/local/tmp/terminfo TERM=xterm-256color gdb -tui --directory=/data/local/tmp /data/local/tmp/vi


If the window displaying the gdb output is corrupted, resize the window to force gdb to redraw the screen.





History of this entry

14.10.2025

initial release



 

Functions and symbols defined in the standard Android OS libraries


Not all standard functions and symbols defined in the standard OS libraries of Linux are also defined in the standard libraries for Android.
Some standard functions are only defined in the standard libraries for Android from a certain Android version onwards.

To test in which version of Android a function is defined, the following command can be used:

Check in which Android version the function __fpending is defined:

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/android-ndk-r25.2/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib ] $ for i in ./aarch64-linux-android/*/libc.so ; do echo "** $i:" ;  nm $i | grep  __fpending ; done
** ./aarch64-linux-android/21/libc.so:
** ./aarch64-linux-android/22/libc.so:
** ./aarch64-linux-android/23/libc.so:
00000000000180c8 T __fpending
** ./aarch64-linux-android/24/libc.so:
00000000000190c8 T __fpending
** ./aarch64-linux-android/26/libc.so:
000000000001a0c8 T __fpending
** ./aarch64-linux-android/27/libc.so:
000000000001a0c8 T __fpending
** ./aarch64-linux-android/28/libc.so:
000000000001c0c8 T __fpending
** ./aarch64-linux-android/29/libc.so:
000000000001c0c8 T __fpending
** ./aarch64-linux-android/30/libc.so:
000000000001d0c8 T __fpending
** ./aarch64-linux-android/31/libc.so:
000000000001e0c8 T __fpending
** ./aarch64-linux-android/32/libc.so:
000000000001e0c8 T __fpending
** ./aarch64-linux-android/33/libc.so:
000000000001e0c8 T __fpending
[aarch64-linux-android] [ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/android-ndk-r25.2/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib ] $


-> The symbol __fpending is defined in the libc.so from the API version 23 and newer.

Be aware, that a program compiled with the libc.so from the API version 23 will only work on Android compiled with API version 23 or newer (API version 23 is Android 6.0)

The list of the API versions for the various Android versions is available here:  https://source.android.com/docs/setup/reference/build-number 

The nm binary for this check can be the standard nm binary for Linux.



Examples for compiling C/C++ programs for Android


Find below a list of examples with instructions to compile C/C++ programs for Androd using the different methods.


Examples for compiling C/C++ programs with the GCC or clang from the Android NDK on the PC

Compiling C programs for Android using the gcc from the Android NDK

Compiling C programs for Android using the clang from the Android NDK



Examples for compiling C/C++ programs in Termux on the phone

Compiling C programs in Android in Termux


Examples for compiling C/C++ programs with the GCC from the Magisk Module on the phone

Compiling C programs in Android using gcc


Examples for compiling C/C++ programs with the clang from the clang19 toolchain on the phone

Compiling C programs in Android using clang



Documentation for the compiler and tools


Some documentation for the compiler and tools can be found here:


URL
Content
Comment
https://clang.llvm.org/docs/DiagnosticsReference.html
documentation for the diagnostic flags from clang




https://devhints.io/makefile
make file cheat sheet

https://gist.github.com/rueycheng/42e355d1480fd7a33ee81c866c7fdf78
another make file cheat sheet




https://cmake.org/cmake/help/latest/manual/cmake.1.html
documentation for the parameter of cmake




https://sourceware.org/gdb/current/onlinedocs/gdb.html/
documentation for the debugger gdb










General hints


A list of common problems and solutions for compiling with the Android NDK is here: https://developer.android.com/ndk/guides/common-problems  


To avoid problems because of invalid settings for the script ./configure it's recommended to delete all cache files before starting ./configure, e.g:

xtrnaw7@fedora01:/data/develop/android/source/ngrep-1.47$ find . -name config.cache -ls
    19970      1 -rw-r--r--   1 xtrnaw7  xtrnaw7         0 Feb 19 08:19 ./regex-0.12/config.cache
xtrnaw7@fedora01:/data/develop/android/source/ngrep-1.47$


# delete the config.cache files
#
find . -name config.cache -exec rm {} \;

# check:
#
find . -name config.cache -ls



Memory usage



If a program fails to compile or compiles extremely slowly due to insufficient RAM, you can expand the swap space using a compressed zram device.

See this guide for instructions to use zram in Linux.

See this guide for instructions to increase the swap using zram in Android.




Specific errors


Below is a list of errors I have encountered so far when compiling C/C++ programs for Android, including information on how to fix them.



ERROR: "cannot find crtbegin_dynamic.o: No such file or directory"


If clang complains about missing runtime libraries like this:

ERROR: "cannot find crtbegin_dynamic.o: No such file or directory" or "cannot find crtend_android.o: No such file or directory"

check that the LDFLAGS contain the parameter -B with the correct value, e.g:

-B/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31/

The directory must contain these files:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/test $ ls  -l /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31/crt*o
-rw-r--r-- 1 shell shell  784 2025-08-03 12:22 /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31//crt_pad_segment.o
-rw-r--r-- 1 shell shell 3904 2025-08-03 12:22 /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31//crtbegin_dynamic.o
-rw-r--r-- 1 shell shell 3648 2025-08-03 12:22 /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31//crtbegin_so.o
-rw-r--r-- 1 shell shell 3720 2025-08-03 12:22 /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31//crtbegin_static.o
-rw-r--r-- 1 shell shell  840 2025-08-03 12:22 /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31//crtend_android.o
-rw-r--r-- 1 shell shell  840 2025-08-03 12:22 /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/lib/aarch64-linux-android/31//crtend_so.o
[clang19 toolchain] ASUS_I006D:/data/local/tmp/test $


If adding the parameter -B to the LDFLAGS does not fix the error, copy the crt*.o files to the working directory. That are the files

crtbegin_dynamic.o 
crtbegin_so.o 
crtbegin_static.o 
crtend_android.o 
crtend_so.o


In the clang19 toolchain is an alias for the necessary copy command:

[clang19 toolchain] 1|ASUS_I006D:/data/local/tmp/test $ alias cp_crt_libs
cp_crt_libs='cp ${NDK_DIR}/sysroot/usr/lib/${CPU_TYPE}-linux-android/${API}/crt*.o .'
[clang19 toolchain] ASUS_I006D:/data/local/tmp/test $



ERROR: fatal error:sys/fcntl.h:No such file or directory


Solution:

Use the headers

#include <fcntl.h>
#include <asm-generic/fcntl.h

 

ERROR: undefined reference to `getsubopt'


Solution:

Use a newer API version: getsubopt is defined in libc.so for android-26 and newer, but not for earlier platforms:

trnaw7@fedora01:/data/develop/android/source/libnl-3.9.0$ cd /data/develop/android/android-ndk-r25.2/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/
xtrnaw7@fedora01:/data/develop/android/android-ndk-r25.2/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib$

xtrnaw7@fedora01:/data/develop/android/android-ndk-r25.2/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib$ for i in ./aarch64-linux-android/*/libc.so ; do echo "** $i:" ;  nm $i | grep  getsubopt ; done
** ./aarch64-linux-android/21/libc.so:
** ./aarch64-linux-android/22/libc.so:
** ./aarch64-linux-android/23/libc.so:
** ./aarch64-linux-android/24/libc.so:
** ./aarch64-linux-android/26/libc.so:
000000000001c440 T getsubopt
** ./aarch64-linux-android/27/libc.so:
000000000001c440 T getsubopt
** ./aarch64-linux-android/28/libc.so:
000000000001e450 T getsubopt
** ./aarch64-linux-android/29/libc.so:
000000000001e450 T getsubopt
** ./aarch64-linux-android/30/libc.so:
000000000001f450 T getsubopt
** ./aarch64-linux-android/31/libc.so:
0000000000020450 T getsubopt
** ./aarch64-linux-android/32/libc.so:
0000000000020450 T getsubopt
** ./aarch64-linux-android/33/libc.so:
0000000000020450 T getsubopt
xtrnaw7@fedora01:/data/develop/android/android-ndk-r25.2/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib$



ERROR: error: undefined reference to 'stderr'


Solution: 

The most likely reason for error messsages like this

scanner.c:(.text+0x8c): undefined reference to `stdin'
scanner.c:(.text+0x90): undefined reference to `stdin'
scanner.c:(.text+0xbc): undefined reference to `stdout'
scanner.c:(.text+0xc0): undefined reference to `stdout'
/data/develop/android/android_root/usr/lib/libpcap.a(scanner.o): In function `yy_fatal_error':
scanner.c:(.text+0x259c): undefined reference to `stderr'
scanner.c:(.text+0x25a0): undefined reference to `stderr'

is that you built for android-23+ but the lib used has a minSdkVersion below that.


Credits: https://stackoverflow.com/questions/51767214/android-studio-with-ndk-link-error-undefined-reference-to-stderr



ERROR: faddrs.h: No such file or directory


Solution:

Use a more current API -- the file ifaddrs.h was added to Android in API 24. The file is only available in the sysroot for clang.



ERROR: ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]


Solution:

Add the option -Wno-implicit-function-declaration to the environment variable CFLAGS before executing ./configure:

export CFLAGS="$CFLAGS -Wno-implicit-function-declaration"



ERROR: error: "./nmap": executable's TLS segment is underaligned: alignment is 8, needs to be at least 64 for ARM64 Bionic


Executing a binary on the phone aborts with an error message like this:

ASUS_I006D:/data/local/tmp $  ./nmap
error: "./nmap": executable's TLS segment is underaligned: alignment is 8, needs to be at least 64 for ARM64 Bionic
Aborted
134|ASUS_I006D:/data/local/tmp $

Solution:

Either add the LDFLAGS 

-ffunction-sections -fdata-sections -Wl,--gc-sections

and rebuild the binary or correct the alignment using the script align_fix.py, e.g.:

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/source/nmap-7.93 ] $ ../align_fix.py ./nmap
Found TLS segment with align = 8
TLS segment is underaligned, patching
[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/source/nmap-7.93 ]

The source for the script align_fix.py was copied from this webpage: https://stackoverflow.com/questions/72496162/how-to-cross-compile-kernel-exec-tools-for-android


Note that the python script align_fix.py might not change all necessary alignments in the binary - in this case re-link the binary using the additional options listed above.


If you are using the clang compiler from the Android NDK toolkit, make sure you are using aarch64-linux-android29-clang++ or a newer version



ERROR: ld: error: undefined symbol: index


Solution:

The index() function is used in old C programs but is now obsolete. The modern equivalent of index() is the strchr() function.

To fix the error replace index with strchr, example

// old line:
char *pos = index(string, char);

// new line:
char *pos = strchr(string, char);



ERROR: ld: error: undefined symbol: rindex


Solution:

The rindex() function is used in old C programs but is now obsolete. The modern equivalent of rindex() is the strrchr() function.

To fix the error replace rindex with strrchr.



ERROR: ld: error: undefined symbol: strverscmp


The function strverscmp is not defined in the file string.h in Android

Solution:

Add the definition of the function to the source file using it; a simple version of that function (which is not 100% compatibel!) is:

int strverscmp(const char *s1, const char *s2) { return strcmp(s1, s2); }



ERROR: use of undeclared identifier 'ushort'; did you mean 'short'?


Solution:

Either replace ushort with

unsigned short var;

or add a definition for ushort:

typedef unsigned short ushort;



ERROR: use of undeclared identifier 'versionsort'


Solution:

Add the missing function versionsort to the source code; an implementation of this function can be found here.



ERROR: libutil.so missing


Solution

libutil.so is not necessary in Android, all functions in that library are in the standard libc library in Android. To get around this error either remove the library from the Makefile or create a dummy library.




ERROR: ld: error: undefined symbol: getdtablesize


The function getdtablesize() retrieves the maximum number of file descriptors that a process can have open at any time. In Linux-based systems (including Android), it is not always available directly and is often replaced by other methods to achieve the same result.

Solution:

To replace it in programs for Android, this code can be used:

#include <unistd.h>
long open_max = sysconf(_SC_OPEN_MAX);



ERROR: the static libraries for building a static binary are missing


Workaround

If the static libraries for compiling a static binary are missing, they can be created from the dynamic library using the tools objcopy and ar.

However, recompiling the library as a static library from source is the more stable approach, especially since dynamic libraries often rely on dynamic linking mechanisms incompatible with static libraries.

Example

ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib # ls -l libm.*
-rw-r--r-- 1 root root 74520 2024-10-27 15:02 libm.so
ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib #

ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib # /system/usr/bin/objcopy --extract-symbol libm.so
ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib #

ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib # /system/usr/bin/ar rcs libm.a libm.so
WARNING: linker: Warning: "/system/usr/bin/ar" unused DT entry: DT_RPATH (type 0xf arg 0x3e1) (ignoring)
ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib #

ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib # ls -l libm*
-rw-r--r-- 1 root root 77108 2024-10-27 15:14 libm.a
-rw-r--r-- 1 root root 74520 2024-10-27 15:14 libm.so
ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib #

ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib # file libm.a libm.so
libm.a:  ar archive
libm.so: ELF shared object, 64-bit LSB arm64, static, not stripped
ASUS_I006D:/data/adb/modules/gcc_on_android/system/usr/share/gcc/aarch64-linux-android/lib #



ERROR: src/ls.c:2276:6: error: function-like macro '__GNUC_PREREQ' is not defined


Solution

Answer from ChatGPT:

__GNUC_PREREQ is a macro typically used in GNU-compatible compilers to check for specific GCC (GNU Compiler Collection) version requirements, but it's not always available in non-GCC or non-standard environments.


Define __GNUC_PREREQ Manually: If you're not using GCC, you can define this macro conditionally in your code to check for GNU versions only when __GNUC__ is defined:

#ifndef __GNUC_PREREQ
#define __GNUC_PREREQ(maj, min) ((__GNUC__ > (maj)) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
#endif



ERROR: unused DT entry: DT_RPATH (type 0xf arg 0xf7a)


Solution:

Answer from ChatGPT:

The message unused DT entry: DT_RPATH is a linker warning indicating that the specified RPATH in the ELF binary is not used by the dynamic linker. The RPATH (runtime library search path) is an embedded path that helps locate shared libraries when executing the binary, but if it doesn't match any needed paths, it's considered "unused."

This warning usually doesn't affect functionality but can be resolved by adjusting the linking options. Here are some ways to address it:

    Use DT_RUNPATH instead of DT_RPATH: Adding -Wl,--enable-new-dtags to your linker options will replace DT_RPATH with DT_RUNPATH, which is more modern and can be overridden by the environment variable LD_LIBRARY_PATH.

    Remove the RPATH: If the RPATH is unnecessary, you can remove it by using chrpath -d <binary> on Linux. Alternatively, use the -Wl,--disable-new-dtags flag during linking.

    Specify necessary paths only: Check if specific paths are needed, and then set RPATH only for them to avoid this warning.



ERROR: cannot find entry symbol _start

An error message like this

/system/usr/share/gcc/bin/../lib/gcc/aarch64-linux-android/10.2.0/../../../../aarch64-linux-android/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000000e90

is printed when you compile a shared library with the linker option "-pie": When using this linker option, the linker searches for the symbol _start in the library but that symbol does not exist for a library.

To fix this error, remove the linker option "-pie". Alternative you can add the linker option "-nostartfiles".



ERROR: non-root user can not create hard links


Some Makefile configurations use hard links but in Android non-root user are not allowed to create hard links.

To fix this "error:" add the missing SELinux context for creating hardlinks by non-root user, execute as user root:

magiskpolicy --live "allow shell shell_data_file file link" 



ERROR: ld: error: unable to find library -lrt


On Android librt has been integrated into libc, and the -lrt option is unnecessary.



ERROR: ld: error: undefined reference due to --no-allow-shlib-undefined: wmempcpy


Solution:

Use a newer API version: wmempcpy is defined in libc.so for android-23 and newer, but not for earlier platforms:

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/ModuleSrc/wget ] $ /data/develop/android/Scripts/check_android_glibc_symbol.sh  wmempcpy
Checking the symbol  ...
** ./aarch64-linux-android/21/libc.so:
** ./aarch64-linux-android/22/libc.so:
** ./aarch64-linux-android/23/libc.so:
000000000001a240 T wmempcpy
** ./aarch64-linux-android/24/libc.so:
000000000001b240 T wmempcpy
** ./aarch64-linux-android/26/libc.so:
000000000001c240 T wmempcpy
** ./aarch64-linux-android/27/libc.so:
000000000001c240 T wmempcpy
** ./aarch64-linux-android/28/libc.so:
000000000001e250 T wmempcpy
** ./aarch64-linux-android/29/libc.so:
000000000001e250 T wmempcpy
** ./aarch64-linux-android/30/libc.so:
000000000001f250 T wmempcpy
** ./aarch64-linux-android/31/libc.so:
0000000000020250 T wmempcpy
** ./aarch64-linux-android/32/libc.so:
0000000000020250 T wmempcpy
** ./aarch64-linux-android/33/libc.so:
0000000000020250 T wmempcpy
[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/ModuleSrc/wget ] $



Notes

/data/develop/android/Scripts/check_android_glibc_symbol.sh is a private script on my PC to check the libc libraries from all local NDKs for a symbol.
(see here for the command used for that)



ERROR: use of undeclared identifier 'nl_langinfo'


Solution:

The reason for errors like this

linux-low.cc:7015:22: error: use of undeclared identifier 'nl_langinfo'
 7015 |   const char *cset = nl_langinfo (CODESET)

is the missing support for nl_langinfo in Android

To fix the error use a hardcoded value in Android:

Replace the line

const char *cset = nl_langinfo (CODESET);

with

#ifdef __ANDROID__
// Android fallback for character set
const char *cset = "UTF-8";
#else
#include <langinfo.h>
const char *cset = nl_langinfo(CODESET);
#endif



ERROR: Pointer tag for 0xdbcd000000060106 was truncated


If you get an error like this:

clang++: error: unable to execute command: Aborted
clang++: error: linker command failed due to signal (use -v to see invocation)
usr/ndk/r27b/sysroot/usr/lib/aarch64-linux-android/35 /data/local/tmp/develop/sysroot/usr/lib/libgmp.a -lc -ldl -pthread                                                            <
Pointer tag for 0xdbcd000000060106 was truncated, see 'https://source.android.com/devices/tech/debug/tagged-pointers'.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
clang++: error: unable to execute command: Aborted
clang++: error: linker command failed due to signal (use -v to see invocation)
[clang19 toolchain] 1|ASUS_I006D:/data/local/tmp/develop/gdb-16.1/gdb $

repeat the make command and check the value after the Pointer tag for : If this value is different for each execution, there is most likely not enough free memory to compile or link the executable.

Try to decrease the number of parallel processes used by make, e.g. to use only 4 CPUs:

make -j4

This error may disappear after a reboot of the phone.


Update 01.10.2026

see this guide for instruction to increase the swap in Android using a zram device.



ERROR: ld.lld: error: undefined symbol: __assert_fail


To fix the error

ld.lld: error: undefined symbol: __assert_fail
>>> referenced by libunwind.cpp
>>>               libunwind.cpp.o:

add the libc to the Linker and compiler Flags:

LDFLAGS="${LDFLAGS} -lc"
CXXFLAGS="${CXXFLAGS} -lc" 



ERROR: ld.lld: error: undefined reference: frexp


To fix the errors

ld.lld: error: undefined reference: frexp
>>> referenced by ../lib/.libs/libunistring.so (disallowed by --no-allow-shlib-undefined)

ld.lld: error: undefined reference: frexpl
>>> referenced by ../lib/.libs/libunistring.so (disallowed by --no-allow-shlib-undefined)

ld.lld: error: undefined reference: ldexpl
>>> referenced by ../lib/.libs/libunistring.so (disallowed by --no-allow-shlib-undefined)


add the libm to the linker and compiler flags

LDFLAGS="${LDFLAGS} -lm"
CXXFLAGS="${CXXFLAGS} -lm" 



How to define the value used by the marco  __BIONIC_AVAILABILITY_GUARD


The macro __BIONIC_AVAILABILITY_GUARD is part of the Android Bionic headers and is used to conditionally include APIs based on the API level. It evaluates to a boolean depending on whether the current value for the definition __ANDROID_API__ Is high enough.

E.g.

#if __BIONIC_AVAILABILITY_GUARD(23)
   // Only compiled if __ANDROID_API__ >= 23
#endif

The code in this if clause is only used if the value for __ANDROID_API__ is 23 or higher. To define the value for __ANDROID_API__, use the the compiler parameter

-D__ANDROID_API__=23

or, if using configure,

CFLAGS="${CFLAGS} -D__ANDROID_API__=23" ./configure


To check, if __ANDROID_API__ is already defined by default in the C compiler use this command:

$CC  -dM -E - < /dev/null | grep ANDROID


To print the current value of __ANDROID_API__ while compiling the source code, add this statement to the source file:

#define STR(x) #x
#define XSTR(x) STR(x)

#pragma message("__ANDROID_API__ = " XSTR(__ANDROID_API__))

Example output of the pragma message

openat-die.c:21:9: warning: __ANDROID_API__ = 31 [-W#pragma-messages]



ERROR: openat-die.c:37:3: error: use of undeclared identifier 'error'


In the source code for some programs (like for example nano) the files used to define common functions (like error) are only included if the variable GNULIB_LIBPOSIX is defined but the configure script doesnot define this variable

To fix this kind of error add the definitions

-DGNULIB_LIBPOSIX

to the CFLAGS manually.



ERROR: error: expected ')' in the statement __attribute__((__sentinel__(1)));


If you get an error regarding the usage of the macro __sentinel__ like this

In file included from bsd-getentropy.c:33:
/data/local/tmp/sysroot/usr/ndk/r27b/sysroot/usr/include/unistd.h:151:20: error: expected ')'
  151 |     __attribute__((__sentinel__(1)));
      |                    ^
1 error generated.

check if the source code supports the variable HAVE_ATTRIBUTE__SENTINEL__ :

grep -r HAVE_ATTRIBUTE__SENTINEL__ *

If this is the case add

-DHAVE_ATTRIBUTE__SENTINEL__=1

to the variable CFLAGS.

If the variable is not used in the source code, re-define the macro __sentinel__ in the CFLAGS variable:

export CFLAGS="$CFLAGS -D\"__sentinel__(x)\"="

Note:

The character ( and ) are special characters for the shell. Therefor it might be necessary to un-define this macro via #undef in the source files using the macro.



ERROR: libintl.h not found


e.g.

include/gettext.h:27:11: fatal error: 'libintl.h' file not found
   27 | # include <libintl.h>

The include file libintl.h is used for internationalization (i18n), i.e. to support multilingual output. This is not supported in Android.

To fix it, add the configure option --disable-nls if supported by the configure script.


Or check the source files to see whether the NO_LOCALE definition is supported. In this case, try this configuration:

export CPPFLAGS="${CPPFLAGS} -DNO_LOCALE".

If nothing helps, create a dummy libintl.h:

// stub-libintl.h
#ifndef _LIBINTL_H
#define _LIBINTL_H
#define gettext(Msgid) (Msgid)
#define dgettext(Domainname, Msgid) (Msgid)
#define dcgettext(Domainname, Msgid, Category) (Msgid)
#define bindtextdomain(Domainname, Dirname) (Domainname)
#define textdomain(Domainname) (Domainname)
#endif



ERROR: ld.lld: error: undefined symbol: bzero


bzero()
is outdated and not supported in Android libc. The function can be defined like this if required:

void bzero(void *s, size_t n) {
    memset(s, 0, n);
}



ERROR: ./include/fileutils.h:90:19: error: static declaration of 'close_range' follows non-static declaration


Since API 34 close_range is an official system call in the Android NDK. If the configure script does not detect that, replace the code in the source file defining close_range with

/* in include/fileutils.h */
#if !defined(__ANDROID__) || __ANDROID_API__ < 34
static inline int close_range(unsigned int first, unsigned int last, int flags)
{
    errno = ENOSYS;
    return -1;
}
#endif



ERROR: ld.lld: error: duplicate symbol: memcmp


ld.lld: error: duplicate symbol: memcmp
>>> defined at memcmp.c
>>>            memcmp.o:(memcmp) in archive ./libdnet-stripped/src/.libs/libdnet.a
>>> defined at static_function_dispatch.S:37 (bionic/libc/arch-arm64/static_function_dispatch.S:37)
>>>            static_function_dispatch.o:(.text+0x10) in archive /data/develop/android/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libc.a

To fix this error, either remove the definition for the duplicate function from one of the source files. Or add the compiler option

-Wl,--allow-multiple-definition

Please note that using this compiler option is only a hack, as you will not know which of the functions will be used by the linker.



ERROR: ld.lld: error: undefined symbol: tzfree



ld.lld: error: undefined symbol: tzalloc
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> did you mean: xzalloc
>>> defined in: ../gl/lib/libgnulib.a(libgnulib_a-xmalloc.o)

ld.lld: error: undefined symbol: tzfree
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a

ld.lld: error: undefined symbol: localtime_rz
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced 1 more times

ld.lld: error: undefined symbol: mktime_z
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced by parse-datetime.c
>>>               libgnulib_a-parse-datetime.o:(parse_datetime_body) in archive ../gl/lib/libgnulib.a
>>> referenced 2 more times


These functions are defined in the libc from Android in API 35 and newer only.

Note that API 35 is Android 15 -- programs compiled with API=35 and using these functions from the Android libc will only run in Android 15 or newer Android versions.


To compile an binary or library using these functions with API 34 or older use dummy definitions:

#if !defined(__ANDROID__) || __ANDROID_API__ < 35

#include <time.h>
#include <stdlib.h>

void *tzalloc(const char *tz) {
    if (tz) setenv("TZ", tz, 1);
    tzset();
    return (void*)1; // Dummy TZ handle
}

void tzfree(void *tzobj) {
    (void)tzobj; // nichts tun
}

struct tm *localtime_rz(void *tz, const time_t *timep, struct tm *result) {
    (void)tz; // ignorieren, global TZ
    return localtime_r(timep, result);
}

time_t mktime_z(void *tz, struct tm *tm) {
    (void)tz; // ignorieren, global TZ
    return mktime(tm);
}

#endif


Some source codes might support these variables in the file config.h:

#define HAVE_TZALLOC 0
#define HAVE_TZFREE 0
#define HAVE_LOCALTIME_RZ 0
#define HAVE_MKTIME_Z 0

to suppress using the functions.



ERROR: ld.lld: error: undefined symbol: backtrace


ld.lld: error: undefined symbol: backtrace
>>> referenced by CRT.c
>>>               CRT.o:(print_backtrace)

ld.lld: error: undefined symbol: backtrace_symbols_fd
>>> referenced by CRT.c
>>>               CRT.o:(print_backtrace)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [Makefile:1473: htop] Error 1
make[1]: Leaving directory '/data/local/tmp/develop/done_clang/htop-3.4.1'


The libc from Android does not implement the backtrace() function.


To fix this error check the parameter for the configure script for a parameter to enable or disable backtraces, e.g.:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/done_clang/htop-3.4.1 # ./configure --help | grep back                                                          
  --enable-unwind         enable unwind support for printing backtraces;
[clang19 toolchain] ASUS_I006D:/data/local/tmp/develop/done_clang/htop-3.4.1 #

If there is a parameter to disable backtraces, add that parameter to the parameter for the script configure.


If that does not fix the error, check if the variable HAVE_EXEC_INFO is used in the source files and, if yes, add the parameter

-DHAVE_EXEC_INFO=0

to the CFLAGS in the makefile.


Alternatively create a dummy library with the missing backtrace function:

# instructions to create the library with backtrace with the clang19 toolchain  
#
git clone https://github.com/mikroskeem/libexecinfo.git

cd libexecinfo


clang ${CFLAGS} ${LDFLAGS} -fPIC -c *.c


ar rcs libexecinfo.a *.o



and add the statically linked library libexecinfo.a to the parameter for clang.

A ready-to-use version of the library libexecinfo.a for arm64 CPU is available in the repository for the clang19 toolchain: 

https://github.com/bnsmb/clang19_toolchain_for_android/raw/refs/heads/main/sysroot/usr/lib/libexecinfo.a

(As of 16.10.2025 the file libexecinfo.a is not yet part of the tar file with the clang19 toolchain)



ERROR: error: use of undeclared identifier 'pthread_cond_clockwait'


[clang19 toolchain] 130|ASUS_I006D:/data/local/tmp/sysroot # clang++ ${CPPFLAGS} ${LDFLAGS}  -o helloworld_in_c++ ./helloworld_in_c++.cpp && ./helloworld_in_c++
In file included from ./helloworld_in_c++.cpp:1:
In file included from /data/local/tmp/sysroot/usr/ndk/r29/sysroot/usr/include/c++/v1/iostream:45:
In file included from /data/local/tmp/sysroot/usr/ndk/r29/sysroot/usr/include/c++/v1/ios:888:
In file included from /data/local/tmp/sysroot/usr/ndk/r29/sysroot/usr/include/c++/v1/mutex:194:
/data/local/tmp/sysroot/usr/ndk/r29/sysroot/usr/include/c++/v1/__condition_variable/condition_variable.h:226:14: error: use of undeclared identifier 'pthread_cond_clockwait'
  226 |   int __ec = pthread_cond_clockwait(&__cv_, __lk.mutex()->native_handle(), CLOCK_MONOTONIC, &__ts);
      |              ^

1 error generated.
[clang19 toolchain] 1|ASUS_I006D:/data/local/tmp/sysroot #


pthread_cond_clockwait is defined in the libc for API 30 or newer only, use the libraries for that API (or a newer one) and, if that does not fix the error, use the clang parameter

--target=aarch64-linux-android30

to force clang to use a specific API version.


Or set the variable __ANDROID_API__ used by the macro __BIONIC_AVAILABILITY_GUARD to value less then 30, e.g.

-D__ANDROID_API__=21


The function pthread_cond_clockwait is only used if the API is 30 or higher:

[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot # grep -B1 -A2  "int pthread_cond_clockwait"  /data/local/tmp/sysroot/usr/ndk/r29/sysroot/usr/include/pthread.h
#if __BIONIC_AVAILABILITY_GUARD(30)
int pthread_cond_clockwait(pthread_cond_t* _Nonnull __cond, pthread_mutex_t* _Nonnull __mutex, clockid_t __clock,
                           const struct timespec* _Nullable __timeout) __INTRODUCED_IN(30);
#endif /* __BIONIC_AVAILABILITY_GUARD(30) */
[clang19 toolchain] ASUS_I006D:/data/local/tmp/sysroot #

(see here for details)



ERROR: crtbegin.c:(.text+0x20): undefined reference to `main'


if the linker complains about an "undefined reference to main" while creating a library, remove the option "-pie".

Alternativley, you can add the option -nostartfiles and ignore the warning " warning: cannot find entry symbol _start"



ERROR: /data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdlib.h:128:1: error: unknown type name 'uint32_t'


/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdlib.h:128:1: error: unknown type name 'uint32_t'
  128 | uint32_t arc4random(void);
      | ^
/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdlib.h:129:1: error: unknown type name 'uint32_t'
  129 | uint32_t arc4random_uniform(uint32_t __upper_bound);
      | ^
/data/local/tmp/sysroot/usr/ndk/r27d/sysroot/usr/include/stdlib.h:129:29: error: unknown type name 'uint32_t'
  129 | uint32_t arc4random_uniform(uint32_t __upper_bound);


uint32_t is defined in stdint.h but this file is not always included in the source files. Therefore, to fix this error add the statement:


#include <stdint.h>


to the source file.


ERROR: rm: /data/local/tmp/develop/ksh/arch/android.arm64-64/lib/package/gen/make.out.fifo: Permission denied


package: make output captured in /data/local/tmp/develop/ksh/arch/android.arm64-64/lib/package/gen/make.out

rm: /data/local/tmp/develop/ksh/arch/android.arm64-64/lib/package/gen/make.out.fifo: Permission denied
mkfifo: /data/local/tmp/develop/ksh/arch/android.arm64-64/lib/package/gen/make.out.fifo: File exists


Some make scripts for Unix tools use FIFOs to coordinate the make process. In Android, the SELinux policies do not allow the user shell to use FIFOs.

On phones with Magisk installed, the missing SELinux policy can be defined with this command:

magiskpolicy --live "allow shell shell_data_file fifo_file { create read open write getattr unlink ioctl }" '

(Note that this SELinux configuration is only valid until the next reboot)

On phones without Magisk but with root access, either temporary disable SELinux by running the following command as user root

setenforce 0

or run the make command as user root.

(To enable SELinux again execute the command "setenforce 1" as user root or reboot the phone)


On phones without root access, the make scripts must be modified to remove the commands to use FIFOs.



ERROR: ld.lld: error: undefined symbol: bcmp



bcmp is an old glibc function that is not implemented in the libc from Android. To fix this error, simply define a macro for bcmp:

#ifdef __ANDROID__
#define bcmp(a,b,n) memcmp((a),(b),(n))
#endif


ERROR: incomplete definition of type 'struct udphdr'


To fix the error simply

error: incomplete definition of type 'struct udphdr'

add the include statement

#include <netinet/udp.h>



ERROR: config.guess does not know Android


If the configure script fails with an error message like this

checking build system type... ./config.guess: unable to guess system type

the script config.guess is too old and does not know the Android OS.

To fix the error, install the current version of config.guess:

wget https://git.savannah.gnu.org/cgit/config.git/plain/config.guess
wget https://git.savannah.gnu.org/cgit/config.git/plain/config.sub


If the URLs do not work try these URLs:

wget https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.guess

wget https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub



ERROR: undefined reference to rpl_malloc / rpl_realloc / rpl_free


The compile fails with error message like this:

/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: io.c:(.text+0x7e8): undefined reference to `rpl_malloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: io.c:(.text+0x808): undefined reference to `rpl_malloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: io.o: in function `enlarge':
io.c:(.text+0xf14): undefined reference to `rpl_malloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: io.c:(.text+0xf4c): undefined reference to `rpl_realloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: io.o: in function `alloc_buf':
io.c:(.text+0x10b8): undefined reference to `rpl_malloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: io.c:(.text+0x10dc): undefined reference to `rpl_realloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: edit.o: in function `fileinfo':
edit.c:(.text+0x2c38): undefined reference to `rpl_malloc'
/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-ld.bfd: edit.c:(.text+0x2c7c): undefined reference to `rpl_malloc'


The configure script uses rpl* functions that are not defined in the current environment.

To fix the error, run the configure script again with thes additional settings.

export CFLAGS="$CFLAGS -DHAVE_CONFIG_H -Drpl_malloc=malloc -Drpl_realloc=realloc -Drpl_free=free"



ERROR: ./asm/types.h not found


If the build fails because of a file in the directory ./asm was not found, check if the directory ./asm-generic in one of the directories with include files exists. If yes, create the symbolic link ./asm -> ./asm-generic.



ERROR: ld.lld: error: undefined symbol: dlopen


Statically linking an executable fails with an error message like this:

ld.lld: error: undefined symbol: dlerror
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)
>>> referenced 1 more times

ld.lld: error: undefined symbol: dlopen
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)

ld.lld: error: undefined symbol: dlsym
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)

ld.lld: error: undefined symbol: dlclose
>>> referenced by os_unix.c
>>>               objects/os_unix.o:(mch_libcall)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

To fix this error, add the option "-ldl" to the flags for clang.



ERROR: checking build system type... build-aux/config.guess: unable to guess system type



If the configure script for a Linux tool does not know the Android OS even after updating config.guess and config.sub with the current version (see here) try to compile for standard Linux:

 ./configure --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu



ERROR: ./time.h:1057:19: error: conflicting types for 'tzalloc'


To fix errors about duplicate definitions for _tzalloc or _tzfree such as:

./time.h:1057:19: error: conflicting types for 'tzalloc'
./time.h:1065:19: error: conflicting types for 'tzfree'

compile and link the binary with an older API version. These functions are defined in the API 35 and newer.




ERROR:  error: use of undeclared identifier 'CPU_SETSIZE'


if the compiler complains about missing definitions for one these identifiers:

cpu_set_t
CPU_SETSIZE   
CPU_SET
CPU_ZERO
CPU_ISSET
CPU_COUNT
sched_getaffinity
sched_setaffinity   
sched_getcpu   
pthread_getname_np  (since API 26)

you can simply add the missing definitions to the source file. These identifiers are supported by the Android NDK but Google removed the definitions from the include file sched.h.

To add the definitions, download the file http://bnsmb.de/files/public/Android/android_affinity_fix.h and add an include statement for the file to your source code.


Or add the missing definitions to the source file:

/android_affinity_fix.h
/* Last Update: 28.09.2026 */

#ifndef ANDROID_AFFINITY_FIX_H
#define ANDROID_AFFINITY_FIX_H


#ifdef __ANDROID__

#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <pthread.h>

/* ====================================================================
 * CPU_SET type and macros
 * ====================================================================
 *
 * Bionic supports cpu_set_t, but the NDK removes the definitions from
 * sched.h. The definitions below are taken from the Bionic source and
 * from official NDK workarounds.
 */
#define CPU_SETSIZE 1024
#define __NCPUBITS  (8 * sizeof (unsigned long))

typedef struct {
    unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
} cpu_set_t;

/* CPU_SET: mark a CPU in the set */
#define CPU_SET(cpu, cpusetp) \
    ((cpusetp)->__bits[(cpu) / __NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))

/* CPU_ZERO: clear the set */
#define CPU_ZERO(cpusetp) \
    memset((cpusetp), 0, sizeof(cpu_set_t))

/* CPU_ISSET: check whether a CPU is set in the set */
static inline int CPU_ISSET(int cpu, const cpu_set_t *set) {
    return (set->__bits[cpu / __NCPUBITS] >> (cpu % __NCPUBITS)) & 1UL;
}

/* CPU_COUNT: count the number of CPUs set in the set */
static inline int CPU_COUNT(const cpu_set_t *set) {
    int count = 0;
    int i;
    for (i = 0; i < (CPU_SETSIZE / __NCPUBITS); i++) {
        count += __builtin_popcountl(set->__bits[i]);
    }
    return count;
}

/* ====================================================================
 * Affinity functions
 * ====================================================================
 *
 * These functions exist at runtime in Android's libc (Bionic), but the
 * NDK does not declare them in sched.h.
 */
extern int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *cpuset);
extern int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *cpuset);
extern int sched_getcpu(void);

/* ====================================================================
 * pthread_getname_np fallback
 * ====================================================================
 *
 * The function was introduced in Bionic with API 26 and is not declared
 * in older NDK headers. At runtime it is available from API 26 onwards;
 * for older API levels, prctl(PR_GET_NAME) is used instead.
 *
 * Note: calling prctl(PR_GET_NAME) fills the buffer with a
 * null-terminated string of at most 16 bytes.
 */
#ifndef HAVE_PTHREAD_GETNAME_NP
static inline int pthread_getname_np(pthread_t thread, char *name, size_t len) {
    (void)thread;
    if (name == NULL || len == 0) {
        return -1;
    }
    return prctl(PR_GET_NAME, (unsigned long)name, (unsigned long)len, 0, 0);
}
#endif

#endif /* __ANDROID__ */
#endif /* ANDROID_AFFINITY_FIX_H */




ERROR : call to undeclared function 'pipe2'


The function pipe2 is available in the Android NDK but the definition is only included if _GNU_SOURCE is defined.



ERROR: The linker does not find an existing library


If you get an error such as

CANNOT LINK EXECUTABLE "/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-as": cannot find "libz.so" from verneed[0] in DT_NEEDED list for "/data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-as"

but the file libz.so exists in the library path :

[gcc toolchain] ASUS_I006D:/data/local/tmp/develop/curl-8.22.0/build $ ls -l /data/local/tmp/sysroot/usr/lib/libz.so*
lrwxrwxrwx 1 shell shell    11 2026-03-25 16:58 /data/local/tmp/sysroot/usr/lib/libz.so -> ./libz.so.1
-rw-rw-r-- 1 shell shell 96808 2026-03-25 16:58 /data/local/tmp/sysroot/usr/lib/libz.so.1
[gcc toolchain] ASUS_I006D:/data/local/tmp/develop/curl-8.22.0/build $


check the SONAME used in the binary and in the library:

[gcc toolchain] 1|ASUS_I006D:/data/local/tmp/develop/curl-8.22.0/build $ readelf -d /data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-as | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libz.so]
 0x0000000000000001 (NEEDED)             Shared library: [libzstd.so]
 0x0000000000000001 (NEEDED)             Shared library: [libegcc.so]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so]
[gcc toolchain] 1|ASUS_I006D:/data/local/tmp/develop/curl-8.22.0/build $

[gcc toolchain] 127|ASUS_I006D:/data/local/tmp/develop/curl-8.22.0/build $ readelf -d /data/local/tmp/sysroot/usr/lib/libz.so | grep SONAME
 0x000000000000000e (SONAME)             Library soname: [libz.so.1]
[gcc toolchain] ASUS_I006D:/data/local/tmp/develop/curl-8.22.0/build $


The linker will not use the library if the names are different.


To fix the issue either correct the library name in the binary (this is the preferred method):

patchelf --replace-needed libz.so libz.so.1 /data/local/tmp/sysroot/usr/lib/android-gcc-cross/bin/aarch64-unknown-linux-android-as   

or change the SONAME in the library:

patchelf --set-soname libz.so /data/local/tmp/sysroot/usr/lib/libz.so.1

(see also here)



back to top