是否可以从记事本之类的外部文件(如果需要,也可以是 cpp 文件)调用例程?
例如 我有 3 个文件。 主代码.cpp
SubCode_A.cpp <- 不包含在 MainCode.cpp 的 header 中
SubCode_B.cpp <- 不包含在 MainCode.cpp 的 header 中
MainCode_A.cpp
#include <iostream>
using namespace std;
int main ()
{
int choice = 0;
cin >> choice;
if (choice == 1)
{
"call routines from SubCode_A.cpp;" <- is there a possible code for this?
}
else if (choice == 2)
{
"call routines from SubCode_B.cpp;" <- is there a possible code for this?
}
return 0;
}
=================================
SubCode_A.cpp 代码
{
if (1) //i need to include if statement :)
cout >> "Hello World!!";
}
=================================
SubCode_B.cpp 代码
{
if (1) //i need to include if statement :)
cout >> "World Hello!!";
}
最佳答案
将代码写成例如SubCode_A.cpp
一个函数,然后在你的主源文件中声明这个函数并调用它。当然,您必须使用所有源文件进行构建才能创建最终的可执行文件。
关于c++ - 调用外部代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18821804/