Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mix C++ and C in a single project in Visual Studio?

I have a a Win32 DLL project in VS2008, it is written in a handful of C modules.
Because I also want to be able to build outside VS2008, with no dependency on VS2008, I have produced a custom makefile, that does all the build and link steps. All this is set up just fine.

Now I'd like to add a couple C++ modules to this DLL. I have modified the custom makefile to compile the .cpp modules as C++, and the .c modules as regular C (/Tc) . This all works. It links everything together, no problem .

Can I configure the VS2008 project to do the same?

Can I mix C++ and C in the same VS2008 project?

Or do I need a custom build step for this?

Thanks.


ANSWER

I had the VS2008 project set to compile as C. I needed to change it to Compile As "Default". Right click the project, select Properties, and then... :

alt text

Thanks, Pavel.

like image 230
Cheeso Avatar asked Oct 25 '09 01:10

Cheeso


People also ask

Can I use C and C++ in the same project?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

Can you mix C and C#?

It is not possible to mix C and C# code in the same file. However, you can replicate any C code (except for calling CreateRemoteThread ) in raw C# using unsafe code and/or P/Invoke. That's completely impossible, unless you write your own compiler.

Can you use C in C++?

If you are compiling the C code together, as part of your project, with your C++ code, you should just need to include the header files as per usual, and use the C++ compiler mode to compile the code - however, some C code won't compile "cleanly" with a C++ compiler (e.g. use of malloc will need casting).

Can you mix C++ and C#?

Yes using C# and C++ for your product is very common and a good idea. Sometimes you can use managed C++, in which case you can use your managed C++ module just like any other .


1 Answers

First of all, you shouldn't even need /Tc if you're building it yourself - cl.exe uses file extension to determine the type, so .c files will be compiled as C by default, and .cpp and .cxx files as C++.

For VS projects, it works in exact same way, except that you can't override this behavior (or at least I do not know how).

like image 158
Pavel Minaev Avatar answered Oct 04 '22 00:10

Pavel Minaev