1

Is it possible to call routines from an external file like notepad (or also cpp file if needed)?

e.g. I have 3 files. MainCode.cpp

SubCode_A.cpp <- not included in the headers of the MainCode.cpp

SubCode_B.cpp <- not included in the headers of the MainCode.cpp

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 CODES

  {
  if (1) //i need to include if statement :)
        cout >> "Hello World!!";

  }

=================================

SubCode_B.cpp CODES

  {

  if (1) //i need to include if statement :)
        cout >> "World Hello!!";

  }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    If you plan to be a programmer, you will stop using "CODES" like this, or bunnies will come in the night and eat your face off. "codes" are for script kidz, programmers write "code". [NOTE: HUGE SMILEY] :) – kfsone Sep 17 '13 at 04:33

4 Answers4

0

Make the code in e.g. SubCode_A.cpp a function, then declare this function in your main source file and call it. You of course have to build with all source files to create the final executable.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

No

You have to compile both codes, Declare an external function (e.g. extern void function (int);, in a header. Compile those two files which will include this header. Then in a 3rd file, where you use it just include the header.

BUT as you include all the 3 files in the compilation it will work.

This other post maybe useful : Effects of the extern keyword on C functions

Community
  • 1
  • 1
dzada
  • 5,344
  • 5
  • 29
  • 37
  • This could just mean that my idea will just not work out. i would be using this concept for a program that needed to be compiled only once, but i could add codes that would come from any file with routines written correctly. anyway, it was very helpful. maybe i should try another way around.thanks a lot. :) – John dave Bumatay Sep 16 '13 at 07:17
  • @JohndaveBumatay that concept is called ["*reflection*"](http://en.wikipedia.org/wiki/Reflection_(computer_programming)) and is not supported by C++ – Manu343726 Sep 16 '13 at 08:02
  • @Manu34726 this has nothing to do with Reflection. Reflection is the process of interrogating an object to discover it's interface. There are no objects in this question, and therefore nothing to discover. – Strings Sep 18 '13 at 01:56
0

You can just use an #include statement. Include instructs the compiler to insert the specified file at the #include point. So your code would be

if (choice == 1)
{
    #include "SubCode_A.cpp"
}
...

And you wouldn't need the extra braces in the SubCode_?.cpp files because they exist in MainCode.cpp

Of course, the compiler will only compile what is in the SubCode files at the time of compilation. Any changes to source that aren't compiled won't end up in your executable.

But mid source #includes doesn't lend itself to very readable code.

Strings
  • 1,674
  • 10
  • 16
  • i see. without compiling the program together with the subcode, it will not be possible. thanks for the help guys. this was my first post. :) – John dave Bumatay Sep 16 '13 at 09:19
  • If you want to compile your sub modules, separately (ie. at a later date) from the main code, then you need to look into .dll (dynamic link libraries) on Windows and .so (shared objects) on *nix – Strings Sep 16 '13 at 09:54
0

It is not possible to call the code in another executable. It is possible for one application to expose an "api" (application programming interface) through a library or DLL which allows you to call some of the code that the application uses.

While compiling YOUR code, though, the compiler needs to know the "fingerprint" of the functions you are going to call: that is, what it returns and what arguments it takes.

This is done through a declaration or "prototype stub":

// subcode.h
void subCodeFunction1(); // prototype stub
void subCodeFunction3(int i, int j);

// subcode.cpp
#include <iostream>

void subCodeFunction1()
{
    std::cout << "subCodeFunction1" << std::endl;
}

void subCodeFunction2()
{
    std::cout << "subCodeFunction2" << std::endl;
}

void subCodeFunction3(int i, int j)
{
    std::cout << "subCodeFunction1(" << i << "," << j << ")" << std::endl;
}

// main.cpp
#include "subcode.h"

int main() {
    subCodeFunction1(); // ok
    subCodeFunction2(); // error: not in subcode.h, comment out or add to subcode.h
    subCodeFunction3(2, 5); // ok
    return 0;
}
kfsone
  • 23,617
  • 2
  • 42
  • 74