I assume that you define malloc for free in the main.cpp file that you are trying to compile, and that mem_alloc and mem_free are located in libmemnmf-0.a
What is probably happening is that some links in main.cpp require objects from glibc. In particular, something dynamically loads the library (dlopen). This code is included in glibc (to answer question 2). When the linker includes objects from glibc and finds that the malloc / free symbol is required for these objects, it will try to directly enable malloc / free from the glibc library. Because of your -static linker flag, the entire libmemnmf-0.a library is statically included in your executable. This will obviously include another malloc and a free object in your executable.
What you need to do is put malloc and free routines in a separate .o file and add this file somewhere in your link command, preferably at the end (unless you specify a standard library in a special way on this line). The .o file will satisfy all character requests, and the glibc library will find these matches resolved whenever dlopen or other objects are required. The difference is that the libmnef-0.a file is a library, and linkers deal with libraries differently than simple objects (which is due to the number of passes through the library to resolve the characters requested by objects in this library). Alternatively, you can remove the -static flag, which I expect will fix the problem as well, but you probably have a good reason to turn this flag on for a start.
If you want to override the behavior of new and delete, you can also view the overload of the new operator and the delete operator for classes to provide a class allocation method or memory pool.
michiel
source share