C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-26-2005, 08:09 AM   #1
Registered User
 
Join Date: Feb 2005
Posts: 61
using c++ in c code

I'm writing a program in c, and I try to use mmgr with it. Mmgr is a c++ file, and a h file. To use mmgr, I have to include the .h file in all my .c files. When I try to do this I get several errors :

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.
Thanks in advance.
hannibar is offline   Reply With Quote
Old 10-26-2005, 08:20 AM   #2
Registered User
 
Join Date: Sep 2005
Location: Sydney
Posts: 60
Yes.

Use a C++ compiler.
lemnisca is offline   Reply With Quote
Old 10-26-2005, 08:26 AM   #3
Registered User
 
Join Date: Feb 2005
Posts: 61
And how can I do it without using a c++ compiler ?
hannibar is offline   Reply With Quote
Old 10-26-2005, 08:29 AM   #4
Registered User
 
kryptkat's Avatar
 
Join Date: Dec 2002
Posts: 409
Quote:
using c++ in c code
<Hisssssss!>

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   Reply With Quote
Old 10-26-2005, 08:35 AM   #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
I tried it, but it doesn't work, but maybe it is a key to the solution ? I don't want to make the c files into cpp files. I just hope there is a 'cleaner' solution.
hannibar is offline   Reply With Quote
Old 10-26-2005, 09:03 AM   #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:
sadf 
durban is offline   Reply With Quote
Old 10-26-2005, 09:10 AM   #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   Reply With Quote
Old 10-26-2005, 09:19 AM   #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:
sadf 
durban is offline   Reply With Quote
Old 10-26-2005, 09:35 AM   #9
Registered User
 
Join Date: Feb 2005
Posts: 61
The documentation doesn't say anything about it.
hannibar is offline   Reply With Quote
Old 10-26-2005, 10:39 AM   #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
}
now, function foo() above can be called from either C or C++. And function foo() can access all normal c++ classes and other language features.

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   Reply With Quote
Old 10-26-2005, 12:01 PM   #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:
sadf 
durban is offline   Reply With Quote
Old 10-26-2005, 12:47 PM   #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   Reply With Quote
Old 10-27-2005, 04:17 AM   #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   Reply With Quote
Old 10-27-2005, 06:07 AM   #14
Registered User
 
Join Date: Aug 2005
Posts: 1,265
it equivalent to this in C
int* function();
Ancient Dragon is offline   Reply With Quote
Old 10-27-2005, 06:51 AM   #15
Registered User
 
Join Date: Jun 2005
Posts: 1,343
Quote:
Originally Posted by Ancient Dragon
it equivalent to this in C
int* function();
No, it is not. If you wanted an equivalent functionality in C, you would emulate it using a function that returns a pointer. But the way you use that function is syntactically different.

In
Code:
int &function()
means that a function returns a reference to an int. A sample usage is;
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
}
will print 24 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 */
}
While it achieves the same net effect, both the implementation of the function and the way it is invoked to achieve that effect are different.
grumpy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:26 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22