英文:
Need to call a function in C, that function has been defined in C++ and declared in customized header file, but facing <iostream> error
问题
我是你的中文翻译,以下是翻译好的部分:
我对这个主题还很新,所以我从打印10个数字的基本程序开始。
这个函数已经在C++中定义,并在我的自定义头文件中声明,现在我想在C文件中访问这个函数。我已经创建了文件,但我遇到了致命错误:iostream:没有这个文件或目录。
请告诉我我哪里出错了。我已经附上了以下程序:
C++ 代码:
#include <iostream>
#include "my_header.h"
// 函数定义
void printNumbers() {
for (int i = 1; i <= 10; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
}
int main() {
return 0;
}
头文件:
#include "my_cpp.cpp"
#ifndef NUMBERS_H
#define NUMBERS_H
// 函数声明
void printNumbers();
#endif
C 代码:
#include <stdio.h>
#include "my_header.h"
int main() {
// 调用在C++中定义的函数
printNumbers();
return 0;
}
提前感谢。
英文:
I'm new to this topic, so i started with basic program of printing 10 numbers.
The function has been defined in C++ and declared in my customized header file, now i want to access this function in C file. I have created the files, but i'm facing fatal error: iostream: No such file or directory
Please let me know where i'm going wrong. I have attached the programs below:
C++ code:
#include <iostream>
#include "my_header.h"
// Function definition
void printNumbers() {
for (int i = 1; i <= 10; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
int main()
{
return 0;
}
}
Header file:
#include "my_cpp.cpp"
#ifndef NUMBERS_H
#define NUMBERS_H
// Function declaration
void printNumbers();
#endif
C code:
#include <stdio.h>
#include "my_header.h"
int main() {
// Call the function defined in C++
printNumbers();
return 0;
}
Thanks in advance.
答案1
得分: 3
这里的错误是你在头文件中包含了一个源文件。
#include "my_cpp.cpp"
这导致你的C编译器看到了你的C++代码,因此你得到了错误信息。
C和C++采用独立编译的模型,这意味着每个C或C++源文件必须独立编译。一个源文件和它包含的头文件被称为翻译单元。
如何分别编译每个源文件,然后将它们链接在一起取决于你使用的编译器/IDE,但是使用#include
不是这样做的方式。
此外,你还有其他涉及混合C和C++的问题。不清楚你为什么这样做,对于一个新手程序员来说,这是一件奇怪的事情。但如果你真的想要这样做,你需要进行以下更改。这些更改是为了给你的C++函数提供C链接,这对于从C调用C++函数是必要的。
像这样更改你的头文件:
#ifndef NUMBERS_H
#define NUMBERS_H
#ifdef __cplusplus
extern "C" {
#endif
// 函数声明
void printNumbers();
#ifdef __cplusplus
}
#endif
#endif
extern "C"
部分为你的函数提供了C链接,但这段代码只在C++中合法。因此,必须将其包装在#ifdef __cplusplus
中,以确保只有C++编译器能够看到它。
此外,你还可以在你的C++源文件中使用相同的链接方式定义函数:
// 函数定义
extern "C" void printNumbers() {
尽管这不是必需的,只要你遵循最佳实践并在源文件中包含头文件,声明就足够了。
英文:
The mistake here is that you have included a source file in a header file
#include "my_cpp.cpp"
The results in your C compiler seeing your C++ code and hence you get the error you get.
C and C++ operate on a model of separate compilation, which that each C or C++ source file must be compiled independently. A source file and the header files it includes is called a translation unit.
How you compile each source file separately and then link them together depends on what compiler/IDE you are using, but using #include
is not the way to do this.
Also you have other problems to do with mixing C and C++. It's not clear why you are doing this, it's a strange thing for a newbie programmer to be doing. But if you really want to you need to make the following changes. These changes are to give your C++ function C linkage, which is necessary to call a C++ function from C.
Change your header file like this
#ifndef NUMBERS_H
#define NUMBERS_H
#ifdef __cplusplus
extern "C" {
#endif
// Function declaration
void printNumbers();
#ifdef __cplusplus
}
#endif
#endif
The extern "C"
part gives your function C linkage, but this code is only legal in C++. Therefore it has to be wrapped in #ifdef __cplusplus
to make sure that this is only seen by the C++ compiler.
Additionally you can define your function with the same linkage in your C++ source file
// Function definition
extern "C" void printNumbers() {
though this is not really necessary, the declaration is sufficient as long as you follow best practise and include the header file in the source file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论