locked
using c++ function in c RRS feed

  • Question

  • hi! im working on a project in C. but i have to use my function that i have written in C++. how can i implemented this function to my project.

    i see some extern declarations but i couldnt implemented it. i tried to use like that.

    extern "C" int save(int size, char *fileName, short int *recordBuffer1)

    {

    }

    Thanks


    Ayhan KÜÇÜKMANİSA
    Wednesday, December 29, 2010 3:35 PM

Answers

  • mr_jeff wrote:

    In the main.c file you just need the prototype (forward declaration)  like this:

    int save(int size, char *fileName, short int *recordBuffer1);

    void main()
    {
    int x;
    short int * rb;
    x = save(10, "myfile.txt", rb);
    }

    Here's a techniqe for writing headers that can be included both in C and  C++ sources:

    // save.h

    #ifdef __cplusplus
    extern "C" {
    #endif

    int save(int size, char *fileName, short int *recordBuffer1)

    #ifdef __cplusplus
    }
    #endif

    Now you can include save.h both in main.c and in save.cpp (where you no  longer need to write extern "C").


    Igor Tandetnik

    • Proposed as answer by Carl Daniel Wednesday, December 29, 2010 10:20 PM
    • Marked as answer by ayhankm Thursday, December 30, 2010 11:58 AM
    Wednesday, December 29, 2010 4:12 PM

All replies

  • Could you provide a little more information?  What compiler are you using?    Is the C++ function written in it's own .cpp file?  If so, why not just add it to the project in Visual Studio - unless of course you are using some other non-IDE compiler?
    Wednesday, December 29, 2010 3:40 PM
  • im using visual studio 2008. im adding this function with add menu to project. but how can i call this function from "main.c"?
    Ayhan KÜÇÜKMANİSA
    Wednesday, December 29, 2010 3:44 PM
  • In the main.c file you just need the prototype (forward declaration) like this:

     

    int save(int size, char *fileName, short int *recordBuffer1);

     

    void main()

    {

      int x;

      short int * rb;

      x = save(10, "myfile.txt", rb);

    }

    Wednesday, December 29, 2010 3:51 PM
  • mr_jeff wrote:

    In the main.c file you just need the prototype (forward declaration)  like this:

    int save(int size, char *fileName, short int *recordBuffer1);

    void main()
    {
    int x;
    short int * rb;
    x = save(10, "myfile.txt", rb);
    }

    Here's a techniqe for writing headers that can be included both in C and  C++ sources:

    // save.h

    #ifdef __cplusplus
    extern "C" {
    #endif

    int save(int size, char *fileName, short int *recordBuffer1)

    #ifdef __cplusplus
    }
    #endif

    Now you can include save.h both in main.c and in save.cpp (where you no  longer need to write extern "C").


    Igor Tandetnik

    • Proposed as answer by Carl Daniel Wednesday, December 29, 2010 10:20 PM
    • Marked as answer by ayhankm Thursday, December 30, 2010 11:58 AM
    Wednesday, December 29, 2010 4:12 PM
  • mr_jeff wrote:

    In the main.c file you just need the prototype (forward declaration)  like this:

    int save(int size, char *fileName, short int *recordBuffer1);

    void main()
    {
    int x;
    short int * rb;
    x = save(10, "myfile.txt", rb);
    }

    Here's a techniqe for writing headers that can be included both in C and  C++ sources:

    // save.h

    #ifdef __cplusplus
    extern "C" {
    #endif

    int save(int size, char *fileName, short int *recordBuffer1)

    #ifdef __cplusplus
    }
    #endif

    Now you can include save.h both in main.c and in save.cpp (where you no  longer need to write extern "C").


    Igor Tandetnik


    In this case though I don't think he wants the extern "C" part though because the function "save" is written in C++, right?
    Wednesday, December 29, 2010 4:22 PM
  • mr_jeff wrote:

    In this case though I don't think he wants the extern "C" part though  because the function "save" is written in C++, right?

    He does, if he wants it to be callable from C. All extern "C" does is  suppress C++ name mangling. Without it, C compiler will say "main.obj  needs a function named _save", and C++ compiler will say "save.obj has a  function available, named save@XYZ123" (or whatever its mangled name  turns out to be). Then the linker will complain about unresolved  externals.


    Igor Tandetnik

    Wednesday, December 29, 2010 4:35 PM
  • mr_jeff wrote:

    In this case though I don't think he wants the extern "C" part though  because the function "save" is written in C++, right?

    He does, if he wants it to be callable from C. All extern "C" does is  suppress C++ name mangling. Without it, C compiler will say "main.obj  needs a function named _save", and C++ compiler will say "save.obj has a  function available, named save@XYZ123" (or whatever its mangled name  turns out to be). Then the linker will complain about unresolved  externals.


    Igor Tandetnik


    That's why I only have one medal next to my name.  :)  So for his solution he will have to put the extern "C" business in his C++ file, right?  Then in main.c he doesn't use the extern "C"?  Is that right?
    Wednesday, December 29, 2010 4:38 PM
  • mr_jeff wrote:

    mr_jeff wrote:

    In this case though I don't think he wants the extern "C" part  though because the function "save" is written in C++, right?

    He does, if he wants it to be callable from C. All extern "C" does is  suppress C++ name mangling. Without it, C compiler will
    say "main.obj needs a function named _save", and C++ compiler will  say "save.obj has a function available, named save@XYZ123"
    (or whatever its mangled name turns out to be). Then the linker will  complain about unresolved externals. 

    That's why I only have one medal next to my name. :) So for his  solution he will have to put the extern "C" business in his C++
    file, right? Then in main.c he doesn't use the extern "C"? Is that  right?

    Yes, extern "C" goes into C++ code, never into C code. C compiler would  consider it a syntax error. However, as I've shown, you can use  preprocessor tricks to expose extern "C" to C++ compiler while hiding it  from C compiler, all in the same file.


    Igor Tandetnik

    Wednesday, December 29, 2010 4:54 PM
  • Thanks. i implement c++ function to my code. now i can call function from main.c
    Ayhan KÜÇÜKMANİSA
    Thursday, December 30, 2010 11:57 AM