Joh*_*ica 11
不std::vector,不.任何模板化的东西都是正确的.
一般来说,使用C++代码并不好玩,但可以做到.您必须将类包装在C代码可以调用的普通非类函数中,因为C不会执行类.要使这些函数可以从C中使用,然后使用extern "C"声明来包装它们,以告诉C++编译器不要进行名称修改.
然后,您可以使用C++编译器编译包装器函数,并创建一个C程序可以链接的库.这是一个非常简单的例子:
// cout.cpp - Compile this with a C++ compiler
#include <iostream>
extern "C" {
void print_cout(const char *str) {
std::cout << str << std::endl;
}
}
/* print.c - Compile this with a C compiler */
void print_cout(const char *);
int main(void) {
print_cout("hello world!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)