Mixing C++ and C
-
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
-
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
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 -
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" - meIn 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.
-
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.
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 -
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" - meThat 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
-
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
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 thatint
result from.Ian Mariano - http://www.ian-space.com/
"We are all wave equations in the information matrix of the universe" - me -
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" - meI've gotten past this problem. Thanks to all Don