user1584421
user1584421

Reputation: 3843

Interaction between multiple files

I need to have two files in my setup. The second file will perform some variable definitions, but based on preprocessor directives read on the first file. The first file will then have to do stuff like set up an object, using the variables defined in the second file as parameters to the constructor.

Is this possible to have this back and forth information between two source files?

To sum up (something like this):

*File1 uses a #define status true.

*File2 sees the preprocessor directive of File1. If its true it sets up some variables.

*File1 then initializes an object with the variables from File2. In this example, steps 1 and 3 are redundant. Because if File1 sets up the preprocessor directive, it can know the variables that File2 which is going to set up - by hardcoding means.

But I just want to experiment with what information I can pass back and forth... Can File2 read preprocessor directives from File1? Can then File1 read back information from File2?

EDIT (pseudocode):

//file1.cpp
#define status true

//this class is defined previously
//var1 was defined in file2.cpp
MyObject object1(var1);



//file2.cpp
//status is the preprocessor directive from file1
if (status == true)
{
  int var1 = 1;
}

Upvotes: 0

Views: 221

Answers (1)

There is absolutely no interaction between preprocessor directives in multiple files.

If you recall, #include is just automated copy/paste. So you can put your directive in a header file, then #include that file into your other files. When you update the directive in the header file it will be like you updated the directive in both files, so they can't get out of sync, but there is still no cross-file interaction.

Officially, a translation unit is a source file after processing all the #includes. Preprocessor directives in one translation unit can't affect any other translation unit.

Upvotes: 2

Related Questions