#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12

    Handling a C and a C++ file in a project?


    Hi,

    I have a project for Borland C++, and it has a C++ file and a C file. The C++ file cannot acess the C file functions, even though I have a header file that has the prototype. It works when they're both C, or C++, but the problem is one of them requires to be C++ and the other requires to be C (which is because of a loop, it's including a library that was for C, therefore the header file isn't compatible with C++!).

    So what do I have to put on my prototype to get it to be compatible with the C++? Is it even possible?

    Regards,
    Someonewhois
    Nathan Wong
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    c++ is supposedly a superset of c, so in theory if you change the file extension of the .c file to .cpp and don't bother to tell the compiler it's really c, it might work. I've never tried it, though.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I've not had to deal with this issue myself, but here's what the problem might be. Mind you, I'm having to remember back several years for this one.

    In order to support function overloading (using the same function name for different functions with different argument types), C++ performs "name mangling" on the function names within the object and library files, wherein the argument-list types are appended onto the function names in order to give each overloaded function a unique name. C does not do this.

    So if a C library or object file needs to be linked into a C++ program, the keyword cdecl (or __cdecl in VC++6) is used to indicate that you're using a C call instead of C++. You can also cdecl an entire block of function prototypes.

    Do a search on cdecl for more information and details.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Put all your C functions prototypes within an extern "C" declaration.
    Code:
    extern "C" {
       int func1(char *);
       int func2(void);
    }
    This tells the C++ compiler not to mangle the names before linking :) See http://www.glenmccl.com/bett_009.htm for more.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    Hi, scorpions4ever, thanks for your reply (along with all the other people's replies!)

    I get an error: MAIN.H(2,9):Declaration terminated incorrectly

    Line 2 is:
    extern "C" {


    Any ideas?
    Thanks!

    Regards,
    Someonewhois
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    Hm, strangely enough, my compiler (Borland) has this as documentation for "extern":

    Syntax

    extern <data definition> ;
    [extern] <function prototype> ;

    Description

    Use the extern modifier to indicate that the actual storage and initial value of a variable, or body of a function, is defined in a separate source code module. Functions declared with extern are visible throughout all source files in a program, unless you redefine the function as static.
    The keyword extern is optional for a function prototype.
    Use extern "c" to prevent function names from being mangled in C++ programs.
    And an example:

    extern "c" void cfunc(int);

    So it looks like it should work, but it doesn't. :(

    help is appreciated.


    Regards,
    Someonewhois
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    Oh, hold on.. My function is in a c file, adn needs to be accessible by a C++ file, so it should just be extern, right?

    Regards,
    Someonewhois
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    ARGH, no, it's supposed to be extern C which still errors. :(

    Any ideas?

    Regards,
    Someonewhois
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    Bah, with a bit of hacking away, it works. Seems that the extern "C" had to be done in the C++ file, not the header file...

    I got it working, thanks for your help. :)

    Regards,
    Someonewhois

IMN logo majestic logo threadwatch logo seochat tools logo