CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2007
    Posts
    5

    Mixing C and C++ Code

    I'm trying to mix C and C++ code. In a .cpp/C++ file, I'm trying to use some C code by including a C header file, doing basically: extern "C" {#include "header.h"} like this says.

    However, compiling the following code:

    cppcode.cpp

    #include <iostream>

    using namespace std;

    extern "C" {
    #include "ccode.h"
    }

    int main(){
    f();
    return 0;
    }

    ccode.h

    #include <stdlib.h>

    void f(){
    int* ptr=malloc(32);
    free(ptr);
    }

    With g++ and gcc doesn't work, as they both give the same error: ccode.h:4: error: invalid conversion from ‘void*’ to ‘int*’ . However, function f() contains valid C code - for example, the following program compiles fine as C:

    #include <stdlib.h>

    int main(){
    int* ptr=malloc(32);
    free(ptr);
    return 0;
    }

    So, preferably without modifying the C header files in any way, how can I use them in my C++ code? Thanks!

  2. #2
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Mixing C and C++ Code

    You can mix C and C++ in C++(but you can't have C++ in plain C compiling). Since C++ is just a superset of C, it just adds functionality. You shouldn't need 'extern "C"' to include headers. The reason for the error is that malloc wants a return type of void* which is basically saying a generic type(so it can allocate for different types). C allows it to not be casted, but C++ won't. So you need to cast it to basically tell it I want the returned type to be whatever type you cast to.

    Code:
    // C style
    int* ptr=(void*)malloc(32);
    
    // C++ style casting
    int* ptr=static_cast<int *>(malloc(32));
    // or another C++ cast
    int* ptr=reinterpret_cast<int *>(malloc(32));
    C++ has 4 specific castings:
    static_cast, dynamic_cast, const_cast, and reinterpret_cast all of them are for specific purposes.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  3. #3
    Join Date
    Sep 2007
    Posts
    5

    Re: Mixing C and C++ Code

    OK, thanks. Sorry for not being clear, but the code I gave is just something that I still expected and wanted to work by mixing C and C++. I'm mixing C and C++ code on a much larger scale, and I just wanted to see if I could get this code to work as an example.

    You can mix C and C++ in C++(but you can't have C++ in plain C compiling). Since C++ is just a superset of C, it just adds functionality. You shouldn't need 'extern "C"' to include headers.
    Well, I don't think this isn't completely true (like how the f() function I defined wouldn't compile as C++). In a larger project, I have a lot of C (and C-specific, that won't compile as C++) headers that I'm hoping to be able to use in my C++ code without modifying them. I may have missed something (correct me if I'm wrong).
    Last edited by bfr; July 8th, 2008 at 01:58 AM.

  4. #4
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Mixing C and C++ Code

    Well that kind of casting will cause problems. But for instance if you have a struct. It should still be valid with C++. The f() function still worked, it was what was contained within the f() function that didn't compile.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Mixing C and C++ Code

    The implementation you have provided for f() is in a header file which gets directly into a C++ file (which would be compiled as C++ code). What you have effectively written is C++ function that would be callable from plain C code and not the other way round (or probably not even that).

    What you need to do is, have a header file with the extern "C" wrapped around the function prototype and implementation in a file that is recognized as a C-file (the extension usually is .c for such files but you should abide by what your compiler understands as a C-implementation file). And then include the header into your C++ implementation files (or other header files). That would actually be called : calling C function from C++ code. Currently, it is not that way and hence the compiler complains about the type mismatch.

    Just for the sake of experimentation, you can use C++ constructs in your current f() function, for example, try using std::string or std::vector inside the f() body as implementation. You compiler will not complain, but that would not be C code.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Mixing C and C++ Code

    The rule of thumb: It's a lot easier to make this sort of thing work if you don't put function definitions in header files, only declarations.

  7. #7
    Join Date
    Jul 2008
    Location
    dalian, China
    Posts
    36

    Re: Mixing C and C++ Code

    You can try the code as this:

    ///////////////////==WinXP sp2 + Vc7.0 pass ==//////////////////////////////

    ///==main.cpp===//
    Code:
    /********************************************************************
    	created:	2008/07/10
    	created:	10:7:2008   16:15
    	filename: 	f:\coding\C++\CodeGuru_TestIncludeC\CodeGuru_TestIncludeC\main.cpp
    	file path:	f:\coding\C++\CodeGuru_TestIncludeC\CodeGuru_TestIncludeC
    	file base:	main
    	file ext:	cpp
    	author:		hecan
    	
    	purpose:	=============WinXP sp2 + Vc7.0 pass=====================
    *********************************************************************/
    
    #include "TestC.h"
    
    int main()
    {
    
    	f();
    	return 0;
    }

    ///==TestC.h===//
    Code:
    /********************************************************************
    	created:	2008/07/10
    	created:	10:7:2008   16:15
    	filename: 	f:\coding\C++\CodeGuru_TestIncludeC\CodeGuru_TestIncludeC\TestC.h
    	file path:	f:\coding\C++\CodeGuru_TestIncludeC\CodeGuru_TestIncludeC
    	file base:	TestC
    	file ext:	h
    	author:		hecan
    	
    	purpose:	
    *********************************************************************/
    
    #ifndef __TESTC_H__
    #define __TESTC_H__
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    	void f();
    
    #ifdef __cplusplus
    }
    #endif
     
    #endif

    ///==TestC.c===//
    Code:
    /********************************************************************
    	created:	2008/07/10
    	created:	10:7:2008   16:15
    	filename: 	f:\coding\C++\CodeGuru_TestIncludeC\CodeGuru_TestIncludeC\TestC.c
    	file path:	f:\coding\C++\CodeGuru_TestIncludeC\CodeGuru_TestIncludeC
    	file base:	TestC
    	file ext:	c
    	author:		hecan
    	
    	purpose:	
    *********************************************************************/
    #include "TestC.h"
    #include <stdlib.h>
    
    void f()
    {
    int* ptr=malloc(32);
    free(ptr);
    }
    Cigagou,Cogitou!

  8. #8
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Mixing C and C++ Code

    Quote Originally Posted by Lindley
    The rule of thumb: It's a lot easier to make thissort of thing work if you don't put function definitions in header files, only declarations.
    Especially in traditional ANSI C, which has no inline functions.

    Quote Originally Posted by exterminator
    What you need to do is, have a header file with the extern "C" wrapped around the function prototype and implementation in a file that is recognized as a C-file (the extension usually is .c for such files but you should abide by what your compiler understands as a C-implementation file). And then include the header into your C++ implementation files (or other header files).
    Something like this?


    Existing C header
    Code:
    // ccode.h
    
    #include <stdlib.h>
    
    void f(){
    int* ptr=malloc(32);
    free(ptr);
    }
    Header file to #include in C++ files.
    Code:
    // ccodewrapper.h
    
    #ifndef ccodewrapper_h
    #define ccodewrapper_h
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void fwrapper();
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    C file which calls f() successfully.
    Code:
    // ccodewrapper.c
    
    #include "ccode.h"
    #include "ccodewrapper.h"
    
    void fwrapper()
    {
        f();
    }
    Last edited by Zaccheus; July 10th, 2008 at 05:15 AM.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured