Thread: Porting code - Override Functions in STDLIB

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Question Porting code - Override Functions in STDLIB

    Hey,

    I'm porting some code to Unix, and the library/OS that the original code is running on has some function calls named the same as std unix/posix system calls. For example both systems have a getpid(); call.

    I'm writing a conversion library, and I need to figure out a way to override the uni/stdlib functions and then call them...

    I was thinking of using a wrapper - i have the getpid() function in the library which then calls another function in another file say getpidUnix(). The getpidUnix() function then calls the Unix version of getpid() and returns the result. The file with the getpidUnix() function has the correct header files (in this case unistd.h). The issue clearly with this method is that getpidUnix() calls getpid() which is then linked to the other getpid().c file rather than the std unix library. This basically gives an infinite loop until the program crashes...

    Is there any way I can easily do this without having to somehow use a make file to get the linker to look in the std lib before object files in the project directory?

    Or is there a directive that I can use to get the compiler to ignore the local object files and look in the lib directory?

    I have a few others that take different params for example calls to putc, printf etc... Func name is the same, but usage is different... I want to keep he usage consistent in the original source and make a conversion lib...

    Any help is greatly appreciated!

    Cheers,

    NW

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, I guess the first question is: are they writing their own getpid() because they want the Unix one and don't have it on the other OS? If so, then why not just not bother porting it and call the Unix one?

    What people were doing writing their own putc and printf I can't really guess. The official party line is that those names are reserved for the use of the implementation, and so you technically don't have a valid program in the first place (not that saying so helps you at all). I would suggest as you port that you rename the local versions something else (you can't rename the system calls, because, well, they're in the system that way already), if you can.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4
    Thanks for your reply.

    Yeah, this code doesnt use any standard libs, all functions were written as stand alone for a MCU.

    To run the apps locally we want to be able to rebuild the source using std unix libs/calls etc...

    I mean I can have a function in my own lib called getpid(), and all linking will be directed there... The issue is getting the linking inside the lib out to getpid()...

    With the 2 files - is there a way to stop the linker searching for external symbols in its local dir 1st..?

    I have getpid.c:
    Code:
    int getpidUnix(void); 
    getpid(){ return getpidUnix() }
    I have getpidUnix.c
    Code:
    #include <unistd.h>
    { .... return getpid() //want to call to the std unix lib, not to getpid.o}
    So is there any way I can undefine the getpid symbol or force the linker to look for getpid in the std libs before it looks at external symbol tables of the local object files in the project?

    I guess in the original source I could #define getpid getpidTheirs and then in my lib have getpidTheirs() which basically does the transformations and returns the result of unistd getpid()...?

    Cheers,

    NW
    Last edited by NightWolf8800; 10-18-2009 at 07:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to find the code that defines standard Functions?
    By hammer1234 in forum C Programming
    Replies: 7
    Last Post: 04-21-2006, 02:37 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Porting Code to C++ -- Need Help
    By uberdog in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2004, 12:51 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM