c++ - 如何通过询问外部函数调用来打印 x 次“Hello world”,该函数调用写在另一个 c++ 文件中(共 2 个文件)
问题描述
如何用两个cpp文件解决它?如何通过用另一个 c++ 文件编写的外部函数调用询问来打印 x 次“Hello world”?
函数.cpp
#include <stdio.h>
int intf(void) {
int l;
printf("How many lines to print?");
scanf("%d", &l);
printf("%d lines to print.\n", l);
int i = 0;
while (i<l) {
printf("Hello world\n");
i++;
}
return l;
}
如何解决 main.cpp
解决方案
这称为单独编译,通常用于大中型程序。
这里 main.cpp 可以很简单:
int intf(void)
int main() {
intf();
return 0;
}
如果您使用的是 IDE,只需在同一个项目中声明两个源文件,如果您使用 CLI 编译器,只需将它们编译成一个可执行文件。这可能是类 Unix 系统上的命令:
c++ main.cpp function.cpp -o foo
但在那种情况下,你应该了解什么是makefile......
推荐阅读
- mongodb - MongoDb:使用 $lookup 查找深度嵌套的对象
- javascript - 在 TypeScript 中将具有键值的对象反序列化/解析为没有键的对象
- excel - 替换字符串时如果没有找到错误(选择替换,查找替换)
- python - Python Pandas 匹配具有重叠坐标的行
- python - 如何使用 Pandas 从 CSV 文件夹生成自定义主数据框?
- java - 不支持 geode 客户端服务器版本 - 不支持序数为 100 的对等或客户端版本
- python - ThreadPoolExecutor 导致 Pyspark 池中的挂起
- html - 链接的“背景颜色”和“边框颜色”属性不使用 CSS 修改
- string - Powershell:在目录中的最新文件中提取两个字符串之间的数据
- filter - 在 FIORI 中过滤模型会导致“f.forEach 不是函数”