How to cross compile Rust and C++ with linked C++ libraries?

I'm working on a bigger project and have now decided to add cross compiling to it. I have created a minimum viable version of the project (Maybe 100 lines of code probably less) and uploaded it to github.

I am building a mostly rust program, with some bindings to C++ that call GLFW/Vulkan code. The demo only uses GLFW.

I added the targets via rustup:

rustup target add x86_64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu
rustup target add x86_64-pc-windows-gnu

I made sure to setup .cargo/config correctly

[build]
rustflags = [
 "-C", "link-args=-lglfw -lstdc++",
]

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-g++"

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-g++"

I installed the appropriate linkers and build stuff:

sudo apt install libssl-dev libglfw3 libglfw3-dev clang gcc g++ cmake build-essential
sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64

Building it with for my native platform (Ubuntu Linux, x86_64) works just fine, the binary runs. No Issues whatsoever.

Building it for ARM Linux x64 results in the linker not finding glfw:

  = note: /usr/lib/gcc-cross/aarch64-linux-gnu/11/../../../../aarch64-linux-gnu/bin/ld: cannot find -lglfw
          collect2: error: ld returned 1 exit status

Building for Windows x86_64 results in the header not being found:

cargo:warning=csrc/basic_glfw.cpp:1:10: fatal error: GLFW/glfw3.h: No such file or directory
cargo:warning=    1 | #include <GLFW/glfw3.h>
cargo:warning=      |          ^~~~~~~~~~~~~~

How do I fix my project so that the project compiles to these platforms correctly?
Also I'm aware of projects like glfw-rs and ash, I would rather not use them if at all possible.

It doesn't look like you are cross-compiling the C++ code anywhere, nor did you install the ARM/Linux or x64/Windows versions of the libraries.

If you are depending on precompiled libraries, it's not enough to merely switch to another compiler. Libraries, too, are in different binary formats depending on platform/hardware/OS. An ARM-targeting compiler and linker won't be able to handle your native x64 libs.

Hey thanks for the reply.

Sorry I don't understand what you mean by:

Where should I be doing stuff differently based on the target? The crate cc is at least aware of the target specified by cargo. The compiler/linker seem to switch to the correct platform specific ones unless I'm mistaken?

I imagined this was the case, though I can't find any apt listings for glfw or vulkan for other platforms. So I don't know where to find them, and also don't know how to tell the linker to link to them. My guess is with rust flags in .cargo/config but not 100% sure.

I haven't cross compiled before. But I'm learning :slight_smile:

That's normal — distributions typically only precompile packages for their own target. Likely, you'll need to cross-compile the libraries yourself, unless you can find precompiled binaries (eg. on the official project site for the two libraries).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.