using c++ in c code

This is a discussion on using c++ in c code within the C Programming forums, part of the General Programming Boards category; I'm writing a program in c, and I try to use mmgr with it. Mmgr is a c++ file, and ...

  1. #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.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Yes.

    Use a C++ compiler.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    And how can I do it without using a c++ compiler ?

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    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.

  5. #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.

  6. #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 

  7. #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.

  8. #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 

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    The documentation doesn't say anything about it.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

  11. #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 

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

  13. #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.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it equivalent to this in C
    int* function();

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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