How can I detect if C malloc available for freestanding target platform?
Solution 1:
It sounds like what you want to do is produce a single library binary that can be used in both freestanding (embedded) and hosted (non-embedded) environments. This is tricky but possible.
If you are building a static library (which it probably needs to be anyways -- generally shared libraries will not be useful in an embedded environment), you can just use malloc in part of your library and, as long as that part is not used by the application, it will not be included and all will be fine. If an embedded application uses the thing that requires malloc, they'll then get a link-time error if the embedded environment does not provide it.
So what you end up needing to do is offer two entry points for your library -- one that requires malloc and one that does not -- and the user (application writer) needs to call the one that is appropriate for their environment. You need to make sure that these two entry points are included in the library such that only one needs to be linked (generally, just make sure they are in different compilation units).
But I do not want the library itself to load 'stdlib.h'
This statement indicates a fundamental misunderstanding about what is going on. stdlib.h is a header file, not a library, so you do not (and cannot) "load" it. You can include it in your source code, but that just gives you the declarations for what is in the library; it does not include any of the library in your library. If you don't use anything from the header, it will have no effect. If you do use things from the header, that will create dependencies in your library on the standard library that will need to be resolved at (application) link time.