conditional compilation based on existance of a symbol?

This is a discussion on conditional compilation based on existance of a symbol? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am writing a program that uses a function that doesn't currently exist in mingw (gettimeofday() in this case). ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167

    conditional compilation based on existance of a symbol?

    Hi,
    I am writing a program that uses a function that doesn't currently exist in mingw (gettimeofday() in this case). I am doing my development on Linux/GCC, but I want to be able to compile my program everywhere.

    I can define that function in terms of Win32 API.

    What I have in mind is to conditionally define that function myself if it doesn't already exist.

    I could do the condition based on __WIN32 or something of that sort, but I assume my code will break if/when mingw finally implements that function?

    How can I do it?

    Thank you

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    One way would be to write your own function and have separate implementations of it for Windows and *nix systems. For your windows implementation, you would have it call Windows API functions, possibly just as a 1:1 wrapper. Same concept for *nix.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    hmm I see... why didn't i think of this =)

    thank you for your help

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    how about using boost::date_time?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    that would be a good idea, too, but since I am not using anything else from boost in this particular project, I am reluctant to add boost as a dependency just for this function.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Put you implementation of gettimeofday() in its own library, for instance libwindows_compat.a.

    Then, EXPLICITLY list the C library on the link line, after everything else except the compat library, and place -lwindows_compat AFTER it:

    Code:
    $(LINK) -o myprogram $(MY_OBJS) $(MY_LIBS) -lc libwindows_compat.a
    This causes the C library to be linked against BEFORE checking windows_compat. If some future version of the MinGW library includes a gettimeofday() function, it will be used, not the one in libwindows_compat.a

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    I see what you mean, but I have never written a library before =) looks quite complicated. I will google it up. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21