This is probably a silly question but i really don't know the answer to it. Can you write a program in C++ and have another part written in C and compile them into a single executable file?

I'm thinking a c/c++ compiler like visual c++ would do this.. Is that correct?

Thanks
danny2000

Yes, the linker can do that. I imagine Visual C++ would create the appropriate makefile to compile and link properly. Visual C++ may or may not set things up to help you in that linking. You're going to have to stick this statement...

#ifdef __cplusplus
extern "C"
{
#endif

// blah blah

#ifdef __cplusplus
}
#endif

in a lot of places in the header files to make things visible to the C++ files. Visual C++ might do that automatically when you create a header file and it might not. Lots of good links on how to do it. Google "Linking C and C++ files".

Can you write a program in C++ and have another part written in C and compile them into a single executable file?

Yes. The two would be compiled into object code separately, then linked together. However, that's not the hard part. The hard part is making sure that the two can talk to each other, because you're very likely calling a C function from C++ or a C++ function from C. The specific tricks are getting your linkage right and exporting usable wrappers.

As concerns linkage, you need to make sure that any C++ functions intended for use in a linked C program must have C linkage. Likewise, any C functions intended for use in a linked C++ program must also be declared with C linkage. Note that this is all on the C++ side. For example:

extern "C" void foo(); // Basically disables C++'s name mangling

Since C++ has a bunch of stuff that C won't recognize, you need to write wrappers and proxies that C will recognize. So if you want to use a class in C, the functionality would need to be wrapped up in a function with C linkage because C can't instantiate the object. If you want to use std::string as parameters or a return type then a wrapper is needed to convert that object into a C-style string that C can use.

Further reading: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

I just wanted to point this out.
Another solution is to simply attempt to compile everything with the C++ compiler. Technically, a C++ compiler cannot compile any C code, but it can compile most C code. Of course, if the C code is a large code-base and all, you shouldn't do this; in that case, use the solutions given by VernonDozier and deceptikon. But if it is only a small piece of C code that you grabbed from somewhere and wish to use in a small project, then you might consider just trying to compile it with the C++ compiler, and if there aren't too many errors and warnings you can try to fix them to make the C code compilable in C++.

it can compile most C code. Of course, if the C code is a large code-base and all, you shouldn't do this; in that case, use the solutio

Thanks for the reply on this. I'm going to have do do some work on this. The reason I'm asking is that I will be using Sqlite (built in C) and I want to build a gui to input values into a d'base (sqlite) . The guie will be built using C++. I'm assuming it will all come together given that sqlite is probably built for this kind of thing..

I'm travelling at the moment so will have to research this when I get back.

Thanks again for your answers, this will be a very big help.

Cheers
danny2000

Most library header files these days are C++ safe, using the aformentioned extern "C" {...} constructs necessary to make C++ use C linkages for these functions. If you do include a C header that is not C++ safe, then the linker will likely complain about missing symbols. That can be fixed easily enough in your C++ code with something like this:

extern "C" {
#include <unsafe_header.h>
}

In any case, a lot of C++ code uses SQLite so you shouldn't have too difficult a time with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.