mercredi 28 février 2018

Use variable from another C++ source file with a twist

I'm using one source file to create another and then from first file call a bash script to compile the second. Is it possible to access the variables of second file in the first?

// File 1: type.cpp
#include<..>
....
int main () {
  int var;
  // Open another file subtype.cpp via file input-output
  // directly write to another file subtype.cpp
  ...
    ofs << "int main () { \n";
    ofs << "printf(\" Hi \"); \n";
    ofs << "int subvar;"
    ofs << "return 0; \n }"
  ...
  // Call the bash script to compile the subtype.cpp file. 
  system ( bash.sh );  

  // Access the variable "subvar" here !
  var = subvar;
  return 0;
}  

I can see that using extern i can accomplish the task but for that the file header of second has to be included in first but file is only created after running first.

What i have tried:

I can think a direct way (a little ugly) of accessing the value of the variable by writing variables value (subtype.cpp) it into third file [subvar_value.txt] and then read the third file by first file (type.cpp). Is there a direct way i can access the variable subvar from first.

Aucun commentaire:

Enregistrer un commentaire