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. Managed C++/CLI
  4. Mixing C++ and C

Mixing C++ and C

Scheduled Pinned Locked Moved Managed C++/CLI
learningc++graphicsdockerhelp
7 Posts 2 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.
  • U Offline
    U Offline
    User 625201
    wrote on last edited by
    #1

    My ERROR: sim50avc error LNK2001: unresolved external symbol "void __cdecl showme(int)" (?showme@@$$J0YAXH@Z) I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. Here is the important code. /* BEGIN - don.c */ /* ############################################################ */ /* ### This just a cut-down example of my C code ### */ /* ############################################################ */ /* ### The fact that it is C code and not C++ cannot change ### */ /* ############################################################ */ #include extern void showme(int); /* call this routine in Form1.h */ int magiccalc() /* Magic calculation - written in C */ { showme(3); /* use the integer 3 instead of showing the calculations code */ return(0); } /* ############################################################ */ /* END - don.c */ /* BEGIN - Form1.h */ #pragma once extern "C" int magiccalc(void); namespace sim50avc { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { int num; InitializeComponent(); num = magiccalc(); /* Call magiccalc in don.c */ /* num is a meaningless return value */ } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::TextBox * inputBox; private: System::Windows::Forms::TextBox * outputBox; private: /// /// Required designer variable. /// System::ComponentModel::Container * components; /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->inputBox = new System::Windows::Forms::TextBox(); this->outputBox = new System::Windows::Forms::TextBox(); this->SuspendLayout(); // // inputBox // this->inputBox->Location = System::Dra

    I 1 Reply Last reply
    0
    • U User 625201

      My ERROR: sim50avc error LNK2001: unresolved external symbol "void __cdecl showme(int)" (?showme@@$$J0YAXH@Z) I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. Here is the important code. /* BEGIN - don.c */ /* ############################################################ */ /* ### This just a cut-down example of my C code ### */ /* ############################################################ */ /* ### The fact that it is C code and not C++ cannot change ### */ /* ############################################################ */ #include extern void showme(int); /* call this routine in Form1.h */ int magiccalc() /* Magic calculation - written in C */ { showme(3); /* use the integer 3 instead of showing the calculations code */ return(0); } /* ############################################################ */ /* END - don.c */ /* BEGIN - Form1.h */ #pragma once extern "C" int magiccalc(void); namespace sim50avc { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { int num; InitializeComponent(); num = magiccalc(); /* Call magiccalc in don.c */ /* num is a meaningless return value */ } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::TextBox * inputBox; private: System::Windows::Forms::TextBox * outputBox; private: /// /// Required designer variable. /// System::ComponentModel::Container * components; /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->inputBox = new System::Windows::Forms::TextBox(); this->outputBox = new System::Windows::Forms::TextBox(); this->SuspendLayout(); // // inputBox // this->inputBox->Location = System::Dra

      I Offline
      I Offline
      ian mariano
      wrote on last edited by
      #2

      Refer to Interoperating with Unmanaged Code in the MSDN for more information. For one, you're mixing Managed C++ with C. If you want to use .NET objects from C, you must expose the .NET objects as COM objects first. You can then call into COM objects using the standard COM API using C++. It is possible to use C, but it's more headache than it's worth. What is it you're trying to do here? If you're just trying to show the result from magiccalc, just return the value from the C code without displaying anything:

      /*  C automagical calculation routine in don.c   */
      int   magiccalc()
      {
         /*  some C calculation   */
      
         return someResult;
      }
      
      //  managed C++ in Form1.h
       
      //   forward declaration
      extern __cdecl int  magiccalc();
       
      //   in class Form1
      public:
         Form1()
         {
            InitializeComponent();
       
            int   num = magiccalc();
      
            //  display num here using MessageBox.Show and String.Format
         }
      

      Of course, if the magiccalc or other routine(s) are in an external DLL, use them via Consuming Unmanaged DLL Functions.

      Ian Mariano - http://www.ian-space.com/
      "We are all wave equations in the information matrix of the universe" - me

      U 2 Replies Last reply
      0
      • I ian mariano

        Refer to Interoperating with Unmanaged Code in the MSDN for more information. For one, you're mixing Managed C++ with C. If you want to use .NET objects from C, you must expose the .NET objects as COM objects first. You can then call into COM objects using the standard COM API using C++. It is possible to use C, but it's more headache than it's worth. What is it you're trying to do here? If you're just trying to show the result from magiccalc, just return the value from the C code without displaying anything:

        /*  C automagical calculation routine in don.c   */
        int   magiccalc()
        {
           /*  some C calculation   */
        
           return someResult;
        }
        
        //  managed C++ in Form1.h
         
        //   forward declaration
        extern __cdecl int  magiccalc();
         
        //   in class Form1
        public:
           Form1()
           {
              InitializeComponent();
         
              int   num = magiccalc();
        
              //  display num here using MessageBox.Show and String.Format
           }
        

        Of course, if the magiccalc or other routine(s) are in an external DLL, use them via Consuming Unmanaged DLL Functions.

        Ian Mariano - http://www.ian-space.com/
        "We are all wave equations in the information matrix of the universe" - me

        U Offline
        U Offline
        User 625201
        wrote on last edited by
        #3

        In a nutshell, I want to display data computed by my current code on a windowed form. My computations are produced by routines in maybe 50 or so "C compatible" source modules.

        I 1 Reply Last reply
        0
        • U User 625201

          In a nutshell, I want to display data computed by my current code on a windowed form. My computations are produced by routines in maybe 50 or so "C compatible" source modules.

          I Offline
          I Offline
          ian mariano
          wrote on last edited by
          #4

          Then just call into and retrieve the computation results from the C modules from C++.NET, and display the results in C++.NET as stated above. No need to "retrofit" the C. Just make sure the C prototypes are visible to the C++ portions and away you go.

          Ian Mariano - http://www.ian-space.com/
          "We are all wave equations in the information matrix of the universe" - me

          U 1 Reply Last reply
          0
          • I ian mariano

            Then just call into and retrieve the computation results from the C modules from C++.NET, and display the results in C++.NET as stated above. No need to "retrofit" the C. Just make sure the C prototypes are visible to the C++ portions and away you go.

            Ian Mariano - http://www.ian-space.com/
            "We are all wave equations in the information matrix of the universe" - me

            U Offline
            U Offline
            User 625201
            wrote on last edited by
            #5

            That should be the case but I am getting a link-time error from the C object module that SHOWME (called from C but coded as function in cpp) is an unresolved external. public: void __cdecl showme(int thenum) { MessageBox::Show(S"abc", S"Result..."); } Should I have declared the function differently so that C can see it? I appreciate any help you can give. Don

            I 1 Reply Last reply
            0
            • U User 625201

              That should be the case but I am getting a link-time error from the C object module that SHOWME (called from C but coded as function in cpp) is an unresolved external. public: void __cdecl showme(int thenum) { MessageBox::Show(S"abc", S"Result..."); } Should I have declared the function differently so that C can see it? I appreciate any help you can give. Don

              I Offline
              I Offline
              ian mariano
              wrote on last edited by
              #6

              First, that showme should be in your C++ and doesn't need the __cdecl. Second, you have to declare the prototype for the C function you'll be calling to get that int result from.

              Ian Mariano - http://www.ian-space.com/
              "We are all wave equations in the information matrix of the universe" - me

              1 Reply Last reply
              0
              • I ian mariano

                Refer to Interoperating with Unmanaged Code in the MSDN for more information. For one, you're mixing Managed C++ with C. If you want to use .NET objects from C, you must expose the .NET objects as COM objects first. You can then call into COM objects using the standard COM API using C++. It is possible to use C, but it's more headache than it's worth. What is it you're trying to do here? If you're just trying to show the result from magiccalc, just return the value from the C code without displaying anything:

                /*  C automagical calculation routine in don.c   */
                int   magiccalc()
                {
                   /*  some C calculation   */
                
                   return someResult;
                }
                
                //  managed C++ in Form1.h
                 
                //   forward declaration
                extern __cdecl int  magiccalc();
                 
                //   in class Form1
                public:
                   Form1()
                   {
                      InitializeComponent();
                 
                      int   num = magiccalc();
                
                      //  display num here using MessageBox.Show and String.Format
                   }
                

                Of course, if the magiccalc or other routine(s) are in an external DLL, use them via Consuming Unmanaged DLL Functions.

                Ian Mariano - http://www.ian-space.com/
                "We are all wave equations in the information matrix of the universe" - me

                U Offline
                U Offline
                User 625201
                wrote on last edited by
                #7

                I've gotten past this problem. Thanks to all Don

                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