Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calling C++ code from C

Calling C++ code from C

Scheduled Pinned Locked Moved C / C++ / MFC
c++designbusinessquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peterchen
    wrote on last edited by
    #1

    Hi, I have the following scenario:

    // .cpp implementation
    class CFoo
    {
    public:
    int Foo();

    static CFoo \* CreateFoo() { return new CFoo; }
    

    };

    // .c
    void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
    int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }

    I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)


    Flirt harder, I'm a coder.
    mlog || Agile Programming | doxygen

    P A D 4 Replies Last reply
    0
    • P peterchen

      Hi, I have the following scenario:

      // .cpp implementation
      class CFoo
      {
      public:
      int Foo();

      static CFoo \* CreateFoo() { return new CFoo; }
      

      };

      // .c
      void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
      int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }

      I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)


      Flirt harder, I'm a coder.
      mlog || Agile Programming | doxygen

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      what kinda of error are you getting. My God is more powerfull Than Your God. (the line that divides the world)

      P 1 Reply Last reply
      0
      • P peterchen

        Hi, I have the following scenario:

        // .cpp implementation
        class CFoo
        {
        public:
        int Foo();

        static CFoo \* CreateFoo() { return new CFoo; }
        

        };

        // .c
        void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
        int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }

        I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)


        Flirt harder, I'm a coder.
        mlog || Agile Programming | doxygen

        A Offline
        A Offline
        Anthony_Yio
        wrote on last edited by
        #3

        Does C has classes? Sonork 100.41263:Anthony_Yio

        J 1 Reply Last reply
        0
        • A Anthony_Yio

          Does C has classes? Sonork 100.41263:Anthony_Yio

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          No, so this will not compile. You have to put the code of the function that uses the class inside the .cpp file. John

          1 Reply Last reply
          0
          • P Prakash Nadar

            what kinda of error are you getting. My God is more powerfull Than Your God. (the line that divides the world)

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #5

            Sorry for the late reply - I got it working. Error was function not found at linkage. Found out that in a .cpp file, you can call CPP stuff even in an funciton that is declared extern "C" So things look like this:

            // .h -------
            // C++ declarations
            #ifdef __cplusplus
            class CFoo { ... };

            #endif

            // C Declarations
            #ifdef __cplusplus
            extern "C" {
            #endif
            void * FooCreate();
            int FoFoo(void *);
            #ifdef __cplusplus
            } // extern "C"
            #endif

            // ---- .cpp ----------
            CFoo * CFoo::CreateInstance() { ... };
            int CFoo::Foo() { ... }

            extern "C" void * FooCreate() { return (void *) CFoo::CreateInstance(); }
            extern "C" int FooFoo(void * f) { return ((CFoo *)f)->Foo(); }

            Cool! I'm not sure if this is a VC6 only thing - but that shouldn't hurt me much


            Flirt harder, I'm a coder.
            mlog || Agile Programming | doxygen

            1 Reply Last reply
            0
            • P peterchen

              Hi, I have the following scenario:

              // .cpp implementation
              class CFoo
              {
              public:
              int Foo();

              static CFoo \* CreateFoo() { return new CFoo; }
              

              };

              // .c
              void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
              int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }

              I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)


              Flirt harder, I'm a coder.
              mlog || Agile Programming | doxygen

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              declaring extern C or not will only tells the compiler how to mangle the function the c++ way or the c way... nothing changes in its functionality.(yeah linking should be carefully done) My God is more powerfull Than Your God. (the line that divides the world)

              1 Reply Last reply
              0
              • P peterchen

                Hi, I have the following scenario:

                // .cpp implementation
                class CFoo
                {
                public:
                int Foo();

                static CFoo \* CreateFoo() { return new CFoo; }
                

                };

                // .c
                void * GetNewFooHandle() { return (void *) CFoo::CreateFoo(); }
                int FooFoo(void * fooHandle) { return ((CFoo *)fooHandle)->Foo(); }

                I don't it done with the declarations (I played around with __cdecl / extern "C" etc.) Any pointers? (Or a sample would be nice)


                Flirt harder, I'm a coder.
                mlog || Agile Programming | doxygen

                D Offline
                D Offline
                Dinesh Ahuja
                wrote on last edited by
                #7

                This will help you in accessing the static member function of C++ in C. //Test1.cpp #include typedef int (* pFunc) (int,int); class D { public : static int Calculate(int iNum1,int iNum2); };` extern "C" { int D::Calculate(int iNum1, int iNum2) { return iNum1 + iNum2; } pFunc p = D::Calculate; } // Test.c #include typedef int (* pFunc) (int,int); extern pFunc p; int main() { int iResult = (*p)(10,20); printf("Value is %d",iResult); return 0; } Hope this will help you! Regards Dinesh

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups