![]() |
| | #1 |
| Registered User Join Date: Feb 2005
Posts: 61
| using c++ in c code Does anyone knwo how I can solve this ? Code: F:\Cvs\Gui\mmgr\mmgr.h(57) : error C2061: syntax error : identifier 'bool'
F:\Cvs\Gui\mmgr\mmgr.h(58) : error C2061: syntax error : identifier 'breakOnRealloc'
F:\Cvs\Gui\mmgr\mmgr.h(58) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(62) : error C2059: syntax error : '}'
F:\Cvs\Gui\mmgr\mmgr.h(101) : error C2143: syntax error : missing '{' before '&'
F:\Cvs\Gui\mmgr\mmgr.h(101) : error C2059: syntax error : '&'
F:\Cvs\Gui\mmgr\mmgr.h(102) : error C2143: syntax error : missing '{' before '&'
F:\Cvs\Gui\mmgr\mmgr.h(102) : error C2059: syntax error : '&'
F:\Cvs\Gui\mmgr\mmgr.h(119) : error C2061: syntax error : identifier 'm_validateAddress'
F:\Cvs\Gui\mmgr\mmgr.h(119) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(119) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(120) : error C2061: syntax error : identifier 'm_validateAllocUnit'
F:\Cvs\Gui\mmgr\mmgr.h(120) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(120) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(121) : error C2061: syntax error : identifier 'm_validateAllAllocUnits'
F:\Cvs\Gui\mmgr\mmgr.h(121) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(121) : error C2059: syntax error : ')'
F:\Cvs\Gui\mmgr\mmgr.h(127) : error C2143: syntax error : missing ')' before '*'
F:\Cvs\Gui\mmgr\mmgr.h(127) : error C2143: syntax error : missing '{' before '*'
F:\Cvs\Gui\mmgr\mmgr.h(127) : error C2059: syntax error : ')'
F:\Cvs\Gui\mmgr\mmgr.h(134) : error C2143: syntax error : missing ')' before '*'
F:\Cvs\Gui\mmgr\mmgr.h(134) : error C2143: syntax error : missing '{' before '*'
F:\Cvs\Gui\mmgr\mmgr.h(134) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(134) : error C2059: syntax error : ')'
F:\Cvs\Gui\mmgr\mmgr.h(135) : error C2143: syntax error : missing ')' before '='
F:\Cvs\Gui\mmgr\mmgr.h(135) : error C2072: 'm_dumpMemoryReport' : initialization of a function
F:\Cvs\Gui\mmgr\mmgr.h(135) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(135) : error C2059: syntax error : ')'
F:\Cvs\Gui\mmgr\mmgr.h(142) : error C2061: syntax error : identifier 'new'
F:\Cvs\Gui\mmgr\mmgr.h(142) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(142) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(143) : error C2061: syntax error : identifier 'new'
F:\Cvs\Gui\mmgr\mmgr.h(143) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(143) : error C2059: syntax error : '['
F:\Cvs\Gui\mmgr\mmgr.h(144) : error C2061: syntax error : identifier 'new'
F:\Cvs\Gui\mmgr\mmgr.h(144) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(144) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(145) : error C2061: syntax error : identifier 'new'
F:\Cvs\Gui\mmgr\mmgr.h(145) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(145) : error C2059: syntax error : '['
F:\Cvs\Gui\mmgr\mmgr.h(146) : error C2061: syntax error : identifier 'delete'
F:\Cvs\Gui\mmgr\mmgr.h(146) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(146) : error C2059: syntax error : 'type'
F:\Cvs\Gui\mmgr\mmgr.h(147) : error C2061: syntax error : identifier 'delete'
F:\Cvs\Gui\mmgr\mmgr.h(147) : error C2059: syntax error : ';'
F:\Cvs\Gui\mmgr\mmgr.h(147) : error C2059: syntax error : '['
Error executing cl.exe.
|
| hannibar is offline | |
| | #2 |
| Registered User Join Date: Sep 2005 Location: Sydney
Posts: 60
| Yes. Use a C++ compiler. |
| lemnisca is offline | |
| | #3 |
| Registered User Join Date: Feb 2005
Posts: 61
| And how can I do it without using a c++ compiler ? |
| hannibar is offline | |
| | #4 | |
| Registered User Join Date: Dec 2002
Posts: 409
| Quote:
You can use c in c++ but not c++ in c code. You can .cpp your c code and compile it fine. If you use c++ you have to .cpp your .exe compile. You ask how you can solve your errors. Just .cpp your file. That should take care of most of your errors. Some are syntax errors with out seeing the code i can only guess what you did. | |
| kryptkat is offline | |
| | #5 |
| Registered User Join Date: Feb 2005
Posts: 61
| I've seen something like : Code: #ifdef __cplusplus
extern "C" {
#endif
#include "cppfile.h"
//c code
#ifdef __cplusplus
}
#endif
|
| hannibar is offline | |
| | #6 |
| Bioport Productions Join Date: Oct 2005
Posts: 215
| all that code is doing is checking whether you are using a c++ compiler, if you are, then it launches that code. That line proves that kryptkat is right - you CANNOT use C++ code in a C compiler, otherwise you wouldn't check for it right? To make that code work with C++ you would have to #define __cplusplus.
__________________ -"What we wish, we readily believe, and what we ourselves think, we imagine others think also." PHP Code: |
| durban is offline | |
| | #7 |
| Registered User Join Date: Feb 2005
Posts: 61
| Ok this is hard to explain... I'll try again. I am writing a program in c using MSVC++6. Because I want to test this program on memory leaks, I decided to use a library called mmgr. (can be found here : http://www.fluidstudios.com/publications.html). This small library is written in c++. I have read that it can be used for c++ AND for c. Now the only thing I want to know is how I can do it. I've searched quite a bit, but I haven't found any info on it. |
| hannibar is offline | |
| | #8 |
| Bioport Productions Join Date: Oct 2005
Posts: 215
| Check the documentation. If they promote that it can be used for C and C++ then they will have a reference on how to do it.
__________________ -"What we wish, we readily believe, and what we ourselves think, we imagine others think also." PHP Code: |
| durban is offline | |
| | #9 |
| Registered User Join Date: Feb 2005
Posts: 61
| The documentation doesn't say anything about it. |
| hannibar is offline | |
| | #10 |
| Registered User Join Date: Aug 2005
Posts: 1,265
| yes you can use c++ code in C programs, but there are some restructions. 1. C programs can't access any c++ classes, or overloaded functions. If you want a function in a c++ file to be used from C, then you have to declare it as "extern C" Code: // this is in the implementation file something.cpp
extern "C" void foo()
{
// do something
}
Do the same in the header file Code: // myheader.h
#ifdef __cplusplus
extern "C" {
#endif
void foo();
// other functions here
#ifdef _cplusplus
}
#endif
I don't think the extern "C" will work the way you posted earlier -- I couldn't get VC++ 6.0 compiler to like that either. I had to put the extern "C" stuff in the .h file itself. Note: __cplusplus is not ansi standard macro (or it wasn't a few years ago), so different compiles may define something else. Last edited by Ancient Dragon; 10-26-2005 at 10:45 AM. |
| Ancient Dragon is offline | |
| | #11 |
| Bioport Productions Join Date: Oct 2005
Posts: 215
| wouldn't extern "C" be linking to the "C" libraries? Saying a C++ program can compile on a C compiler is ridiculous. You are actually using C in C++, not the other way around.
__________________ -"What we wish, we readily believe, and what we ourselves think, we imagine others think also." PHP Code: |
| durban is offline | |
| | #12 |
| Registered User Join Date: Aug 2005
Posts: 1,265
| I didn't say the C++ modules are compiled with a C compiler. You have to use a c++ compiler to do that, such as g++, Dev-C++, VC++ 6.0/7.0, and many other c++ compilers. c++ compilers can compile both c and c++ modules, and it knows how to compile by the file extension (*.c for C code and either *.cpp or *.cc for c++ code). Many c++ compilers have additional options to recognize other file extensions. If you want to link both C and C++ into the same executable, then yes, the C runtime libraries will also be linked into the program. [edit]The above assumes that the source code for all *.c and *.cpp files is available and can be modified as needed. In OP case, I don't think he does -- it appears he may be trying to call c++ library functions from c, which as you said before, cannot be done. Last edited by Ancient Dragon; 10-26-2005 at 01:09 PM. |
| Ancient Dragon is offline | |
| | #13 |
| Registered User Join Date: Feb 2005
Posts: 61
| Thanks for the help everyone. It seems that my problem is not trivial to solve, so I'll try to code my own memory manager library. btw : as a side-question : in c++, when you declare a function like this : int &function(...); hen what does the & mean? Last edited by hannibar; 10-27-2005 at 04:21 AM. |
| hannibar is offline | |
| | #14 |
| Registered User Join Date: Aug 2005
Posts: 1,265
| it equivalent to this in C int* function(); |
| Ancient Dragon is offline | |
| | #15 | |
| Registered User Join Date: Jun 2005
Posts: 1,343
| Quote:
In Code: int &function() Code: int a = 0, b = 0;
int &function(int t)
{
if (t)
return a;
else
return b;
}
int main()
{
function(1) = 24; // sets the global a to 24
function(0) = 42; // sets the global b to 42
}
The (approximate) equivalent in C is this; Code: int a = 0, b = 0;
int *function(int t)
{
if (t)
return &a;
else
return &b;
}
int main()
{
*(function(1)) = 24; /* sets the global a to 24 */
*(function(0)) = 42; /* sets the global b to 42 */
}
| |
| grumpy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extended ASCII Characters in an RTF Control | JustMax | C Programming | 18 | 04-03-2009 08:20 PM |
| Enforcing Machine Code Restrictions? | SMurf | Tech Board | 21 | 03-30-2009 07:34 AM |
| Obfuscated Code Contest | Stack Overflow | Contests Board | 51 | 01-21-2005 04:17 PM |
| Interface Question | smog890 | C Programming | 11 | 06-03-2002 05:06 PM |
| Who will map the scan code (inserted by VKD_Force_keys) to virtual key code? | Unregistered | Windows Programming | 0 | 02-21-2002 06:05 PM |