Mixing C++ and C code - HELP
-
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. There is no need for controls on the form to try this example. 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 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 = ne
-
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. There is no need for controls on the form to try this example. 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 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 = ne
I can send the project in a zip file if anyone is willing to look at it. Thanks Don
-
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. There is no need for controls on the form to try this example. 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 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 = ne
so, what's the question ? what's the problem ? maybe you'll get better help in the managed-C++ forum.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. There is no need for controls on the form to try this example. 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 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 = ne
try: extern "C" int magiccalc(void);
-
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. There is no need for controls on the form to try this example. 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 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 = ne
Here are the 2 errors. sim50avc fatal error LNK1284: metadata inconsistent with COFF symbol table: symbol '?magiccalc@@$$FYAHXZ' (0A00002D) mapped to '?magiccalc@@$$J0YAHXZ' (06000001) in don.obj c:\other\vc_projects\sim50avc\Form1.h(97): warning C4440: calling convention redefinition from '__clrcall ' to '__cdecl ' ignored
-
try: extern "C" int magiccalc(void);
It's the second line of code in Form1.h Thanks though
-
It's the second line of code in Form1.h Thanks though
Well, it's not the same extern "C" than just extern. In C++ some compilers add extra information to the function names (type of parameters, type of return,..). To avoid this problem, use the extern "C" declaration in all the 'C' functions.
-
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. There is no need for controls on the form to try this example. 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 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 = ne
Here is a possible short cut: If the C code is part of the project then you could just changed the extention to cpp. Then you will not need to modify it or the header, unless the C code is doing conversions or something else that the C++ compiler wants you to make sure you know what you are doing. Remember that C++ is a superset of C and therefore your C code can be compiled as C++. If you do not whish to change the file extesion then you will need to use the extern "C" any where there is an external C function declared. For future reference you may want to write your C code and test it then change the extesion to cpp and then recompile in order is see where the code can be improved. Make correction then change the extension back to C and retest. This will help improve your C coding and understanding of the C language. Well, good luck. INTP