2

Is this possible in C to import a library function under a different name?

like say ideally this fake C syntax:

 char *malloc(int) as mymalloc;

instead of malloc as it is known in the library file it becomes mymalloc. And and identifier malloc is not declared, and is available for future declarations.

I need this for some specific use case when the third party large header file can be optionally included later, but few functions must be always used. So I need another micro header that defines those same few functions as the bigger header declares(or not) later. This all is a part of a progamming language that compiles to C, so the weird requirement is that small header must define functions before the large header file. Also I cannot use some trivial ifdef solutions because large header does not know about me and I cannot modify it. The functions in question take a complex structure pointer as parameter, but I just declare them as talking void* thus compiler will bark at "incompatible redefinition".

CLUMSY_STRUCTURE *create_struct();
do_something(CLUMSY_STRUCTURE *);

and the definition of CLUMSY_STRUCTUREs is pages long, and I do not want them in my tiny header.

Also I am not sure even if I redefine CLUMSY_STRUCTURE will the compiler think two declarations are compatible, when it sees the second declaration.

exebook
  • 32,014
  • 33
  • 141
  • 226
  • 1
    Could you write your own function of desired name which simply calls the other function? – Yunnosch Jan 24 '19 at 08:32
  • 1
    If you take a subset of the declarations in "large header file" and save it as "small header file", then "small header file" can be safely included before "large header file". So I'm not sure I understand the problem. Can you create a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) that illustrates your problem ? – Sander De Dycker Jan 24 '19 at 08:37
  • No you can't... – 0___________ Jan 24 '19 at 08:43
  • What's wrong with something like `#include "LargeHeader"` ? – Ôrel Jan 24 '19 at 08:46

1 Answers1

2

Yes you can!

You only need to build a tiny wrapper that does include the appropriate header and calls the standard library functions, and call that wrapper from where you want. The only limit, is that the name malloc cannot receive external linkage, because if would conflict at link time with the standard library identifier.

Here is a code example:

mymalloc.h

void * mymalloc(int size);
void myfree(void * pt);

mymalloc.c

#include <stdlib.h>
#include "mymalloc.h"

void *mymalloc(int size) {
    return malloc(size);
}

void myfree(void *pt) {
    free(pt);
}

main.c

#include <stdio.h>
#include <string.h>
#include "mymalloc.h"

int main() {
        char *malloc = mymalloc(64);
        strcpy(malloc, "foo bar");
        printf("%s\n", malloc);
        myfree(malloc);
        return 0;
}

The malloc pointer in main has no linkage so it will work with no warning. If it was a global variable, it should be declared static to receive internal linkage

But beware: reusing a name from the standard library that way is allowed per standard and accepted by the compiler, but it will confuse future readers and should be avoided if possible. I have only did it here because of the explicit requirement. NEVER DO THAT IN NORMAL CODE, and at least never pretent that I advised it!

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • obligatory mention of the [`LD_PRELOAD` trick](https://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick) – Sander De Dycker Jan 24 '19 at 09:01
  • @SanderDeDycker: OP did not tag Linux and LD_PRELOAD is not portable. If we go that way, we should speak of gcc weak symbols which would explicitely allow overriding of a library symbol. – Serge Ballesta Jan 24 '19 at 09:31
  • @SanderDeDycker: No problem, and that is why I mentioned gcc weak symbols in my own comment. But I am not sure it really needs to be in the answer. Thanks anyway for the comments. – Serge Ballesta Jan 24 '19 at 09:45