Thread: How to use a C lib in a C++ project

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    How to use a C lib in a C++ project

    Hello, I wrote a library to implement a communication protocol between a PalmOS palmtop ( programmed in C ) and a WinXP PC ( MSVC++ ). I need to implement the Hardware Abstraction Layer for the PC and the Palm. I used standard C programming for the lib, so the implementation of the HAL for the palm is ok.

    But now I need to implement it for the PC program, which is a c++ project: must I modify the library functions using different calling conventions? In which way?

    Also, I'd like to use MFC in the HAL implementation for PC: this involves to use a cpp compiler for the lib. Is this possible?

    Thanks for help,
    BrownB

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wait, what calling convention is used for the library functions? It sounds to me like you should be able to port this over directly as both c and c++ use the C calling convention.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    I spent some time looking around: now everything is clear enough, and the previous question is wrong: the problem wasn't the calling convention ( which is the same as you said ) but the file extension..must be cpp!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i believe you use

    Code:
    extern "C" void myFunction(int, char);
    this will compile the function as C functions.....C++ encode it's function names, C does not (or does not do it the same)...however, i've never done this myself so i could be way off....

    i'm sure there is something in the FAQ about this though
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Excellent. Your headers should look something like this (hint: name mangling):

    Example
    Code:
    #ifndef MY_HEADER_FILE
    #define MY_HEADER_FILE 1
    
    #ifdef __cplusplus
    extern "C" {   // c++ compile
    #endif
    
    /* 
     *  Insert your header info here.
     */
    
    #ifdef __cplusplus
    }              // c++ compile
    #endif
    
    #endif
    Last edited by master5001; 12-16-2004 at 05:47 AM.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I do recommend doing it more like my way as it allows the same header file to be compilable on both sides of your project. The C compiler will freak out when it sees extern "C".

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    listen to master, s/he seems to know more than i do
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well lets not get carried away here. You can do as MS does and do what misplaced suggested.

    Example
    Code:
    #ifdef __cplusplus
    # define EXTERN_C extern "C"
    #else
    # define EXTERN_C
    #endif
    
    EXTERN_C void myFunction(int, char);
    The above will compile using either C or C++ fine, and in both cases use C name mangling. The one major advantage to extern "C" { } is that you can basically copy/paste 6 lines of code into any existing header to make it C++ friendly.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    All good points, thanks!

    See you!
    BrownB

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah no problem. I'm also willing to bet this is on the FAQ, if not it couldn't hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM