开发者

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

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

Thanks, Pavel.


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


There's absolutely no problems mixing C and C++ in the same project. All you'll need to do it to design your interface between C and C++ modules in tems of C functions and C data structures, an then make sure that those interface functions are declared on C++ side with C-linkage specifier extern "C".


They should add fine, considering the Microsoft C compiler will compile both. If you add them to the project, they'll get passed to cl -- and I believe cl makes a choice what mode to use based on the extension of the file. You're using .cpp, which is good.

In short: Yes.

GCC will do this too, so your makefile should be reasonably portable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜