CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Posts
    11

    Post C++ lib used in c

    In linux, if i were to write a lib that used c++ in the lib, but the function that were used to interface with lib were standard c, would i be able to use the lib from a c program?
    theBUSH

    The next line is not true.
    The previous line is true.

    There are only 10 kinds of people in the world, those who know binary, and those who dont.

    There's no place like 127.0.0.1.

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    I don't think you can use any exported classes, but if you export functions with extern "C" I think they will be usable in C. If not, then a DLL will work for sure.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ lib used in c

    Originally posted by theBUSH
    In linux, if i were to write a lib that used c++ in the lib, but the function that were used to interface with lib were standard c, would i be able to use the lib from a c program?
    As long as the functions

    a) are "extern C functions" and

    b) The arguments to the functions are not references or C++ types

    then you won't have a problem calling it from 'C' code.

    For a), it doesn't matter what the function does internally or what language it was coded in. As long as the function name is a 'C' function name (not a C++ mangled name), then it can be called from a 'C' program. I have many libraries whose internals are C++, but the function interface is 'C'.

    You accomplish this by making the function extern "C". A lot of people believe that this means the function is a 'C' function -- this is not true. All "extern C" does is allow it to have 'C' linkage, which makes the function callable from a 'C' module. The function that is "extern "C" can use as much C++ as you feel like.

    As for the arguments, you cannot use reference types in a 'C' function interface, or C++ types (such as std::string, std::vector, etc.).

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured