【发布时间】:2012-09-27 06:50:18
【问题描述】:
我对 C 和 C++ 很陌生。但是我有一些 C++ 函数需要从 C 中调用它们。我举了一个例子来说明我需要做什么
main.c:
#include "example.h"
#include <stdio.h>
int main(){
helloWorld();
return 0;
}
example.h:
#ifndef HEADER_FILE
#define HEADER_FILE
#ifdef __cplusplus
extern "C" {
#endif
void helloWorld();
#ifdef __cplusplus
}
#endif
#endif
example.cpp:
#include <iostream.h>
void helloWorld(){
printf("hello from CPP");
}
它只是不起作用。我仍然在我的main.c 中收到对_helloWorld 的未定义引用的错误。问题出在哪里?
【问题讨论】:
-
标准 C++ 标头
<iostream>没有.h。你的编译器可能有<iostream.h>作为扩展名,但它的内容是不可预测的。printf例如通常来自<cstdio> -
当你不控制C++库时:stackoverflow.com/questions/2744181/…