c++ - 调用外部代码

标签 c++

是否可以从记事本之类的外部文件(如果需要,也可以是 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/

相关文章:

用于用户输入的 C++ 字符串验证

c++ - 遍历作为 QList<int> 的 QVariant?

c++ - QPushButton 中的两种颜色文本

c++ - 共享库和静态库如何选择?

c++ - 加载共享库时出错 : libiomp5. 所以:无法打开共享对象文件:没有这样的文件或目录

c++ - 为什么 __int16 和 int(32) 用 C++ 生成不同的 asm?

c++ - 在 OpenCV C++ 中访问 "Mat"对象(不是 CvMat 对象)中的矩阵元素

c++ - 如何找到 boost 运行时版本

c++ - 在 main() 之前调用 void 函数

c++ - 关于volatile关键字的问题