DEV Community

Gerson Loaiza Vasquez
Gerson Loaiza Vasquez

Posted on

How I solved the "error: linking with cc failed" in Large Rust Projects with Mold.

When I started contributing to open source projects, I started with highly technical projects like Cairo Native, Madara, kakarot-rpc, among others.

It turns out that these projects have a VERY large codebase, which makes them take a long time to install, compile and consume too much memory resources. This causes the installation and compilation process to fail with an error that says something like this:

error: linking with cc failed: exit status: 1
This linking error with cc at the end has a note that says:
collect2: fatal error: ld terminated with signal 9 [Killed]
Enter fullscreen mode Exit fullscreen mode

It is an error that has to do with the linking of the cc compiler. When I was trying to solve it while searching on the internet, I realized that it is a very common error and that it is caused by multiple factors. And this is what makes it difficult to solve. But in my case it has always been a problem of lack of memory.

What I am going to explain is based on my experience dealing with that error. When we are installing and building a project, this compilation process is being done with a default compiler configuration. And this default configuration does not mean that it is the most efficient. Causing this process to fail in projects with huge codebases. It turns out that there is a tool called mold. Mold is a modern linker that makes this linking process much more efficient than the default configuration that the compiler already has. Saving resources and allowing the project in question to be built. Setting it up is quite simple. It is configured as follows:

When I was dealing with this error I was using WSL2 and Rust, so that's why I'm using Linux command line:

sudo apt install mold

Follow these steps for Rust.

All the projects I mentioned are based on Rust and I was always using that programming language. Curiously, I had this error recently in a project called Trust-Bridge when installing it. Luckily, I already knew the recipe I had to apply to solve the error. That would be it folks. If you ever have that problem it's probably due to memory issues and you just have to change to a mold linker.

Here you can see a graph that shows how efficient is the executables that the mold linker produce. The differences in sizes are insane.

Image description

Note: I had this error for the first time when contributing to Cairo Native, it took me more than 3 days to fix that error. Some time later I also had the same error with the Madara and Kakarot projects. But the difference was that I didn't spend days fixing the error haha.

Top comments (0)