I have a project "First" with first.cpp
#include<iostream>
#include "second.h"
using namespace std;
int main()
{
#ifdef MY_MACRO1
cout << "MY_MACRO1 in first" << endl;
#endif
second();
return 0;
}
and a static library "Second" with second.cpp
#include <iostream>
#include "second.h"
using namespace std;
int second()
{
#ifdef MY_MACRO1
cout << "MY_MACRO1 in second" << endl;
#endif
return 0;
}
and second.h
#ifndef THIRD
#define THIRD
int second();
#endif
In First.vcxproj, I have defined
<PreprocessorDefinitions>WIN32;MY_MACRO1;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
In properties of Second.vcxproj, in the preprocessor definitions section, I have ticked the "Inherit from parent project or default". (Here in the read only section, I can see that the definitions are not being inherited. Same can be concluded from the "Command Line" section in properties of Second project.)
Now when I compile and execute First project I expect the output of this code to be as :
MY_MACRO1 in first
MY_MACRO1 in second
but what I'm getting is:
MY_MACRO1 in first
Why is the Second project/static library not inheriting the macro? What am I missing here?
PS: I have asked this question after googling for 2-3 days.
CodePudding user response:
Create a stand-alone property sheet and then have all relevant projects inherit that.
Under the View -> Other windows -> Property manager right click on one of the projects and select "Add new property sheet", edit the properties for the sheet then add it to the other projects that need to inherit the relevant properties.
You don't want to inherit a vcxproj file because those contain actual file references and not just properties to apply to build configurations.
CodePudding user response:
As suggested by @SoronelHaetir, I added the macro to a .props file and imported that .props file in both projects and that solved my initial problem.
As I mentioned in the comments, there is a third project which imports the 'Second' library and I did not want to compile Second with the macro when 'Third' is being compiled. To achieve this, I added a new configuration (Build->Configuration Manager), named NewConfig, to 'Second' and 'Third', where macro is not defined. While compiling 'Third' I used this NewConfig and while compiling 'First' I used the one where the macro is defined.