Linker cannot find -lsqlite3

I'm at macOS trying to compile rust lib for android, using sqlite3, I defined the linker properly before intending to add the sqlite3 dependencies, at it worked very well with me.

Once I added the rustqlite dependences, and wanted to compile, I got the below error, that is telling the linker is not finding the sqlite3 lib

cargo build --target x86_64-linux-android --release

= note: /Users/../NDK/x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lsqlite3
clang70: error: linker command failed with exit code 1 (use -v to see invocation)

Knowing that my sqlite3 is avaiable at /usr/bin/sqlite3 I added it to ~/.profile as:

export LDFLAGS="-L//usr/bin/sqlite3"

Followed by source ~/.profile but the error still there!

1 Like

I'm not familiar with this: For LDFLAGS, are you supposed to specify the location of the BINARY, or the DIRECTORY where the *.so files are stored? [I thought it was the latter]

I'm not sure, I tried searching this error, and found one guy made like this when he was compiling c/c++, so thought same could be applicable here

That person is linking to a directory:

export LDFLAGS="-L/opt/local/lib"

You are linking to a binary.

I can't give you the correct answer as I'm on Linux. What you probably want to do is:

  1. type in locate sqlite3.so
  2. get something back of the form /foo/bar/.../blah/libsqlite3.so
  3. then do export LDFLAGS="/foo/bar/.../blah:$LDFLAGS"
Hasans-Air:rust h_ajsf$ locate sqlite3.so

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:

  sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.

How about
find /usr | grep sqlite3.so

?

Hasans-Air:rust h_ajsf$ sudo find /usr | grep sqlite3.so

/usr/local/Caskroom/android-sdk/4333796/ndk-bundle/prebuilt/darwin-x86_64/lib/python2.7/lib-dynload/_sqlite3.so

/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_sqlite3.so

same:

Hasans-Air:rust h_ajsf$ sudo find /usr | grep sqlite3.so
/usr/local/Caskroom/android-sdk/4333796/ndk-bundle/prebuilt/darwin-x86_64/lib/python2.7/lib-dynload/_sqlite3.so
/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_sqlite3.so
Hasans-Air:rust h_ajsf$ export LDFLAGS="/usr/local/Caskroom/android-sdk/4333796/ndk-bundle/prebuilt/darwin-x86_64/lib/python2.7/lib-dynload/_sqlite3.so:$LDFLAGS"
Hasans-Air:rust h_ajsf$  cargo build --target x86_64-linux-android --release
   Compiling rust_sql v0.1.0 (/Users/h_ajsf/IdeaProjects/rust_sql/rust)                                                                                                
error: linking with `/Users/h_ajsf/IdeaProjects/rust_sql/rust/NDK/x86_64/bin/x86_64-linux-android-clang` failed: exit code: 1                                          
  |                                                                                                                                                                    
  = note: "/Users/h_ajsf/IdeaProjects/rust_sql/rust/NDK/x86_64/bin/x86_64-linux-android-clang" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--allow-multiple-definition" "-m64" "-L" 

.
.
.
"/Users/h_ajsf/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-linux-android/lib/liballoc-2d44723f32308cf8.rlib" "/Users/h_ajsf/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-linux-android/lib/libcore-f2133e8b70369157.rlib" "-Wl,--end-group" "/Users/h_ajsf/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-linux-android/lib/libcompiler_builtins-71671e8958739d51.rlib" "-Wl,-Bdynamic" "-lsqlite3" "-ldl" "-llog" "-lgcc" "-lgcc" "-lc" "-lm" "-shared"
  = note: /Users/h_ajsf/IdeaProjects/rust_sql/rust/NDK/x86_64/bin/../lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: cannot find -lsqlite3
          clang70: error: linker command failed with exit code 1 (use -v to see invocation)                                                                            
                                                                                                                                                                       
                                                                                                                                                                       
error: aborting due to previous error                                                                                                                                  
                                                                                                                                                                       
error: Could not compile `rust_sql`.                                                                                                                                   

To learn more, run the command again with --verbose.
  1. I don't know how to debug this.

  2. Can you try:

If you use the bundled feature, libsqlite3-sys will use the gcc crate to compile SQLite from source and link against that. This source is embedded in the libsqlite3-sys crate and is currently SQLite 3.25.2 (as of rusqlite 0.15.0 / libsqlite3-sys 0.10.0). This is probably the simplest solution to any build problems. You can enable this by adding the following in your Cargo.toml file:

[dependencies.rusqlite]
version = "0.15.0"
features = ["bundled"]

This will cause rusqlite to download sqlite3 sources, compile it, and link against it (instead of looking for sqlite3 on your OS).

1 Like
Hasans-Air:rust h_ajsf$  cargo build --target x86_64-linux-android --release
   Compiling libsqlite3-sys v0.10.0                                                                                                                                                            
error: failed to run custom build command for `libsqlite3-sys v0.10.0`                                                                                                                         
process didn't exit successfully: `/Users/h_ajsf/IdeaProjects/rust_sql/rust/target/release/build/libsqlite3-sys-7a7e94f62531c244/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("x86_64-linux-android")
OPT_LEVEL = Some("3")
HOST = Some("x86_64-apple-darwin")
CC_x86_64-linux-android = None
CC_x86_64_linux_android = None
TARGET_CC = None
CC = None
CFLAGS_x86_64-linux-android = None
CFLAGS_x86_64_linux_android = None
TARGET_CFLAGS = None
CFLAGS = None
DEBUG = Some("false")
running: "x86_64-linux-android-clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=x86_64-linux-android" "-Wall" "-Wextra" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_HAVE_ISNAN" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-o" "/Users/h_ajsf/IdeaProjects/rust_sql/rust/target/x86_64-linux-android/release/build/libsqlite3-sys-60305c2ad4c7b159/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c"

--- stderr
thread 'main' panicked at '

Internal error occurred: Failed to find tool. Is `x86_64-linux-android-clang` installed?

', /Users/h_ajsf/.cargo/registry/src/github.com-1ecc6299db9ec823/cc-1.0.26/src/lib.rs:2295:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

I think that is the key line. We are trying to build for x86_64-linux-android, but it seems clang/android is not installed (or can not be found) on your system.

1 Like

I do have the NDK, and was able to compile for Android before trying to use sqlite

^^ This seems to suggest that wherever clang/android is installed, Cargo can't find it.

I do not know, I've all the required files for compiling to Android!!

Knowing that my sqlite3 is avaiable at /usr/bin/sqlite3 I added it to ~/.profile as:

What is your sqlite? Is sqlite crosscompiled for android or this is sqlite for MacOS?
sqlite for MacOS of course is not what you want and you can not use it.

Also rusqlite has bundled feature, may be just use this feature and not care about sqlite at all?

2 Likes

When you're compiling for Android, you can't use any libraries from /usr. Everything on your Mac outside the Android NDK directory is Mac-only and can't work.

You must specify paths to the copy of sqlite in the NDK, if it has any. And architecture of that NDK library has to match architecture you're cross compiling for (beware of incompatibility between ARM Android and x64 Android).

Similarly, any sys create has to be told not to use the default C compiler you have on your Mac, because it won't know how to make Android-compatible libraries. You will need to point the sqlite crate to use Android-targetting compiler from the NDK.

Generally everything native has to be built from the NDK for the NDK using NDKs tools and libraries and any "contamination" with any tool or library belonging to macOS will break it.

1 Like

features = ["bundled"]

should resolve this Sqlite3 issue, if the clang/android compiler can be found, right?

(since it's downloading sqlite3 source and building it from scratch)

2 Likes

Than @zeroexcuses I tried this, but got:

Internal error occurred: Failed to find tool. Is x86_64-linux-android-clang installed?

I add the folder that contains the x86_64-linux-android-clang to the path, and installed llvm and cmake but got the below error:

$ code $HOME/.bash_profile
$ source $HOME/.bash_profile
$ brew install cmake
$ brew install --with-toolchain llvm
$ cargo build --target x86_64-linux-android --release

   Compiling libsqlite3-sys v0.10.0                                                                                                                                                            
error: failed to run custom build command for `libsqlite3-sys v0.10.0`                                                                                                                         
process didn't exit successfully: `/Users/h_ajsf/IdeaProjects/rust_sql/rust/target/release/build/libsqlite3-sys-7a7e94f62531c244/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("x86_64-linux-android")
OPT_LEVEL = Some("3")
HOST = Some("x86_64-apple-darwin")
CC_x86_64-linux-android = None
CC_x86_64_linux_android = None
TARGET_CC = None
CC = None
CFLAGS_x86_64-linux-android = None
CFLAGS_x86_64_linux_android = None
TARGET_CFLAGS = None
CFLAGS = None
DEBUG = Some("false")
running: "x86_64-linux-android-clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=x86_64-linux-android" "-Wall" "-Wextra" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_HAVE_ISNAN" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-o" "/Users/h_ajsf/IdeaProjects/rust_sql/rust/target/x86_64-linux-android/release/build/libsqlite3-sys-60305c2ad4c7b159/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c"
cargo:warning=sqlite3/sqlite3.c:1068:10: fatal error: 'stdarg.h' file not found
cargo:warning=#include <stdarg.h>     /* Needed for the definition of va_list */
cargo:warning=         ^~~~~~~~~~
cargo:warning=1 error generated.
exit code: 1

--- stderr
thread 'main' panicked at '

Internal error occurred: Command "x86_64-linux-android-clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=x86_64-linux-android" "-Wall" "-Wextra" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_HAVE_ISNAN" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-o" "/Users/h_ajsf/IdeaProjects/rust_sql/rust/target/x86_64-linux-android/release/build/libsqlite3-sys-60305c2ad4c7b159/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c" with args "x86_64-linux-android-clang" did not execute successfully (status code exit code: 1).

', /Users/h_ajsf/.cargo/registry/src/github.com-1ecc6299db9ec823/cc-1.0.26/src/lib.rs:2295:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Thanks.

I got it solved by below:

  1. Add feature bundled as per your advise
[dependencies]
rusqlite = {version = "0.15.0", features = ["bundled"]}
  1. Adding the NDK folder that require the related xxx_android_clang file to the path:
$ code $HOME/.bash_profile
PATH=$PATH:/usr/local/ndk_for_rust/x86_64/bin
PATH=$PATH:/usr/local/ndk_for_rust/arm64/bin
PATH=$PATH:/usr/local/ndk_for_rust/arm/bin
PATH=$PATH:/usr/local/ndk_for_rust/x86/bin
$ source $HOME/.bash_profile
  1. Had copy of the ndk_for_rust in the project root /Users/h_ajsf/IdeaProjects/rust_sql/, so I used it as linked in the .cargo/config as:
[target.aarch64-linux-android]
ar = "ndk_for_rust/arm64/bin/aarch64-linux-android-ar"
linker = "ndk_for_rust/arm64/bin/aarch64-linux-android-clang"

[target.armv7-linux-androideabi]
ar = "ndk_for_rust/arm/bin/arm-linux-androideabi-ar"
linker = "ndk_for_rust/arm/bin/arm-linux-androideabi-clang"

[target.i686-linux-android]
ar = "ndk_for_rust/x86/bin/i686-linux-android-ar"
linker = "ndk_for_rust/x86/bin/i686-linux-android-clang"

[target.x86_64-linux-android]
ar = "ndk_for_rust/x86_64/bin/x86_64-linux-android-ar"
linker = "ndk_for_rust/x86_64/bin/x86_64-linux-android-clang"
  1. Then I was able to compile for the selected Android targets:
$ cargo build --target x86_64-linux-android --release
$ cargo build --target aarch64-linux-android --release
$ cargo build --target armv7-linux-androideabi --release
$ cargo build --target i686-linux-android --release
3 Likes

Congrats on getting clang / android / arm to work with sqlite3!

1 Like