首页 > 解决方案 > 如何通过询问外部函数调用来打印 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

标签: c++functionprintingexternal

解决方案


这称为单独编译,通常用于大中型程序。

这里 main.cpp 可以很简单:

int intf(void)

int main() {
    intf();
    return 0;
}

如果您使用的是 IDE,只需在同一个项目中声明两个源文件,如果您使用 CLI 编译器,只需将它们编译成一个可执行文件。这可能是类 Unix 系统上的命令:

c++ main.cpp function.cpp -o foo

但在那种情况下,你应该了解什么是makefile......


推荐阅读