这是一个调用linux c函数的cpp应用程序。 我用g ++ 4.7编译并运行它。 有用。 我不擅长c ++。 但是我听说当你想从c ++源文件中调用c函数时,你需要声明'extern“C”'。 为什么这个程序有效?
#include <unistd.h> #include <iostream> using namespace std; int main(int argc, const char **argv) { rmdir("t"); cout << "Hello" << endl; return 0; }
unistd.h
头文件被编写为与C ++兼容。 如果你在里面看,你会发现这样的:
#ifdef __cplusplus extern "C" { #endif ... #ifdef __cplusplus } // extern "C" #endif
如果你在一个unistd.h
没有以这种方式保护的平台上,那么你需要在include中使用extern "C"
。