When does libcore get linked?

I understand that libcore is the fundamental part of rust and even in no_std environments you are allowed to use functions from here.

I am writing an implementation of libstd with just a minimal set of features (for an in-depth understanding about how the standard library works). You could say that its primary objective is to keep binary sizes as small as possible.

In regards to the above, even if I do not use any function from libcore at all in my libstd implementation, will libcore still be linked into the binary?

I have made some tests on rust-1.78 on my amd64 laptop where I created a very basic application which just endlessly loops with no_std and its about 670 bytes which suggests that libcore wasn't linked. Hence, another query of mine is what are the preconditions (if any) for libcore to get linked into a no_std application?

Thanks in advance.

I don't know the precise answer to your question, but note that there is a difference between

  • a library being linked, i.e. it was included in the arguments to the linker, and
  • some items from the library appearing in the final binary.

In most cases, only items which are used — referenced from other code — will appear in the binary produced by the linker. Some sorts of items will be included unconditionally (e.g. this is likely but not guaranteed to happen to items marked #[no_mangle]) but it is likely that core contains no such items, since core, unlike std, is not at all concerned with any kind of global (process-wide) state.

2 Likes

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.