-
July 7th, 2008, 10:53 PM
#1
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!
-
July 7th, 2008, 11:09 PM
#2
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.
-
July 8th, 2008, 12:21 AM
#3
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.
-
July 8th, 2008, 02:29 AM
#4
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.
-
July 9th, 2008, 10:31 AM
#5
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 9th, 2008, 11:53 AM
#6
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.
-
July 10th, 2008, 03:21 AM
#7
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!
-
July 10th, 2008, 05:05 AM
#8
Re: Mixing C and C++ Code
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.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|