Cross-compiling rust

Hello. I am newbish at cross-compiling but now I want to compile i686 and arm binaries from x64 system. What should I do?

(Assuming you've used rustup to install your Rust).

Run rustup target add <target>. Run rustup target list to get a list of available targets. Then run cargo build --target <target> --release to build. Also, rustup has a quite pleasant help system, if you forgot the command syntax, just run rustup help or rustup subcommand help.

You'll also need a linker for the target platform. The i686 can probably reuse the system one, so no steps necessary, but for arm, you'll need to install a package like arm-linux-gnueabihf-gcc.

Note that if your code needs external libraries to run, linking step will fail. You'd need the libraries from the target system to be somehow visible (eg. in /usr/lib (although that might not be a good idea)).

Edit: After quick googling I found this really nice manual:
https://github.com/japaric/rust-cross

cargo --target does not work unfortunately.

Oh, sorry. The target flag goes after build. I'll fix my response.

I am on archlinux and I have installed all the lib32 libraries and gcc-multilib but when I try to compile my project I see

Compiling project v0.21.0 (file:///home/fx/workspace/project)
error: linking with cc failed: exit code: 1
|
= note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m32" "-L" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/discord_pickupbot.0.o" "-o" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/discord_pickupbot" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps" "-L" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/build/openssl-5464f8f6e728c35a/out" "-L" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/build/openssl-sys-extras-5c7e4d8925825f00/out" "-L" "/usr/local/lib" "-L" "/usr/lib" "-L" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/build/miniz-sys-60c8d67696f63a43/out" "-L" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libregex-36c8e259ac5ba542.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/librusqlite-a221a739db38f0bf.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libedit_distance-18e11a0f71d67dd2.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblibsqlite3_sys-02dacca8e9891790.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libchallonge-3018afe7c5586005.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnanomsg-e7de00470a752e6b.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libthread_local-a3c0092e9fb6507d.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnanomsg_sys-0e817eee55150a80.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liburlshortener-a0fb0475c91a2ee9.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libchrono-7342810e34d1c30d.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libthread_id-bcd46c79a620a618.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libregex_syntax-6602c4e3d91326a4.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnum-1fa4854b44bb6a54.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnum_iter-50df698bc905252c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnum_integer-52fdddf28cd8e924.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libaho_corasick-d1dfd931d7cac82f.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libfern-59dcc518f0a5bd64.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libutf8_ranges-5c6a6dacba3be7ce.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblru_cache-5df7c7dfb11fae93.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libmemchr-c555f740a543880f.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libserde_json-55921106e25a0359.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libtoml-cf3bfced9e77aba4.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblinked_hash_map-8bca62f90f0f04c7.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libdiscord-0994965fd0e1742b.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libflate2-d719035eaa7c6a88.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libminiz_sys-722889de4af2439c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libbase64-3d59f87ca7bd5d6c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libmultipart-55f08ebabe33c7e3.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libtempdir-c5a9975021308003.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libmime_guess-c93837e7e949852a.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libphf-5bbeb1b565d2cd66.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libphf_shared-bfa1b811e6454b68.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libwebsocket-22256f500859e785.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnet2-755d3e4f87237d0e.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libcfg_if-72c1f992b13d5087.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libbyteorder-e4fc6ea0c49cabcf.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/librand-8ea7d489d4a383a0.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libserde_json-484a3bbf12530e06.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libdtoa-76ebb504ac31c05f.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libitoa-58ab3e11c7b36cd3.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libserde-5c748cb46e1cb43c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnum_traits-92bb90166cd1857c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libhyper-8c26afe2192885f2.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libmime-3c32775eb368d3f0.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblanguage_tags-abe5f88450030754.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libsolicit-b1e7a1a373a16061.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libtypeable-ad302c7c6ce39362.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libtraitobject-516160771d9db60e.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libnum_cpus-86764cdeac0ab7b8.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libhttparse-09a7134c96c51a1c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libunicase-87c5f8ba3db67fd3.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libcookie-1f264fccb02ef6c6.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libopenssl_verify-0e08dfffe26f151c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libopenssl-6d8c7b7707d12a7d.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libopenssl_sys_extras-5206753a3d8bd8ef.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libopenssl_sys-a4f86492ba1e2f09.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblazy_static-7f1b96a3a3eb529d.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libbitflags-0e272044714c8076.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liburl-30c51e30991892cc.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libidna-82ebdb343d6c1855.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libunicode_normalization-77ce7c413abacd3f.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libunicode_bidi-eaf420bc9a69d19f.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libmatches-3614821dd5ceb75c.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libtime-750bfdd52feafcb7.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblibc-e1db4c5f3a4f3c2f.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/librustc_serialize-6b938435173797f7.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libserde-036bf26779b9b9ba.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/libhpack-e4d9e946312af616.rlib" "/home/fx/workspace/project/target/i686-unknown-linux-gnu/debug/deps/liblog-bf16bb9a4912b11d.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libstd-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libpanic_unwind-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libunwind-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/librand-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libcollections-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/liballoc-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/liballoc_jemalloc-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/liblibc-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/librustc_unicode-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libcore-f5a209a9.rlib" "/home/fx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libcompiler_builtins-f5a209a9.rlib" "-l" "sqlite3" "-l" "sqlite3" "-l" "sqlite3" "-l" "nanomsg" "-l" "nanomsg" "-l" "ssl" "-l" "crypto" "-l" "util" "-l" "dl" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util"
= note: /usr/bin/ld: skipping incompatible /usr/lib/libsqlite3.so when searching for -lsqlite3
/usr/bin/ld: skipping incompatible /usr/lib/libsqlite3.so when searching for -lsqlite3
/usr/bin/ld: skipping incompatible /usr/lib/libsqlite3.so when searching for -lsqlite3
/usr/bin/ld: skipping incompatible /usr/lib/libnanomsg.so when searching for -lnanomsg
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/../../../libnanomsg.so when searching for -lnanomsg
/usr/bin/ld: skipping incompatible /usr/lib/libnanomsg.so when searching for -lnanomsg
/usr/bin/ld: cannot find -lnanomsg
/usr/bin/ld: skipping incompatible /usr/lib/libnanomsg.so when searching for -lnanomsg
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/../../../libnanomsg.so when searching for -lnanomsg
/usr/bin/ld: skipping incompatible /usr/lib/libnanomsg.so when searching for -lnanomsg
/usr/bin/ld: cannot find -lnanomsg
/usr/bin/ld: skipping incompatible /usr/lib/libssl.so when searching for -lssl
/usr/bin/ld: skipping incompatible /usr/lib/libcrypto.so when searching for -lcrypto
/usr/bin/ld: skipping incompatible /usr/lib/libutil.so when searching for -lutil
/usr/bin/ld: skipping incompatible /usr/lib/libutil.a when searching for -lutil
/usr/bin/ld: skipping incompatible /usr/lib/libdl.so when searching for -ldl
/usr/bin/ld: skipping incompatible /usr/lib/libdl.a when searching for -ldl
/usr/bin/ld: skipping incompatible /usr/lib/libpthread.so when searching for -lpthread
/usr/bin/ld: skipping incompatible /usr/lib/libpthread.a when searching for -lpthread
collect2: error: ld returned 1 exit status

Also I tried to add /usr/lib32/ into LD_LIBRARY_PATH for cargo invocation but that did not help.

You probably need 32-bit version of libnanomsg.so. You can download the file from here, although, I'm not sure that will be enough.

LD_LIBRARY_PATH is used for searching libraries at runtime. The env variable used by gcc to search for libraries at compile time is LIBRARY_PATH.

As a long term solution, I'd install 32-bit linux in chrooted environment and install all the necessary libraries there (and on your normal arch, set LIBRARY_PATH accordingly). You can even have a chrooted arm installation (although that needs telling the kernel it needs to run qemu to run arm binaries).