你如何编译包含C源文件的C++源代码?

Ken*_*eth 3 c c++ compiler-construction

我试图在c ++程序中使用SQLite.我对C/C++的了解有限,因为我在这一点上主要使用Java.我在大学里上了一些课程,但已经有一段时间了,我们从未涉及过这样的课程.SQLite是用C语言编写的.在编译程序时你会怎么做?(我在我的Windows平台上安装了MinGW,所以gcc和g ++是我用来编译的.)

Dir*_*tel 11

您可以保护C++代码中的C标头

extern "C" { 

   // your includes here

}
Run Code Online (Sandbox Code Playgroud)

这应该是所有事情,g++应该高兴地从两个链接代码gccg++.这个extern "C" ...技巧也用在C++系统头文件和许多库中,只需查看g ++安装附带的头文件或一些合适的开源项目.这是一个Boost示例:

edd@max:~$ grep 'extern "C"' /usr/include/boost/date_time/*
/usr/include/boost/date_time/filetime_functions.hpp:    extern "C" {
/usr/include/boost/date_time/filetime_functions.hpp:    } // extern "C"
edd@max:~$ 
Run Code Online (Sandbox Code Playgroud)

编辑:感谢delnan的细心评论---这是来自sqlite3.h标题本身:

/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)

所以这当然已经得到了解决.