Home > Back-end >  How to access another project's VS macro $(TargetFileName)?
How to access another project's VS macro $(TargetFileName)?

Time:11-14

In Visual Studio 2019 I have two C projects contained in the same Solution: ProjectA and ProjectB.

How to define a C/C Preprocessor Definition in ProjectB ( let's call it: THE_OTHER_FILE_NAME ), which is equal to the string returned by the VS macro $(TargetFileName) in the ProjectA ?

NOTE: Hardcoding paths in the C/C source files is outside the scope of this question.

CodePudding user response:

You can include file witch you need example my second project name is p2 path in same folder repo

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <Windows.h>
#include "../p2/Header.h"
int main(){
    print();
    system("PAUSE");
}

header file code

#pragma once
#include <stdio.h>
void print() {
    printf("Hello\n");
}

And you can cheek this enter image description here

Finally, enter the following code to get TargetFileName, hope it helps.

#include <iostream>
#include <string>

#if !defined TARGET_NAME
#define TARGET_NAME ""
#endif
int main() {
  std::string str(TARGET_NAME);
  std::cout << str;
}

CodePudding user response:

To access another project's VS macro $(TargetFileName) create the following XML file:

<Project>
 <PropertyGroup>
   <TargetFileName_Global>__UNDEFINED__</TargetFileName_Global>
   <TargetFileName_Global Condition="Exists('$(SolutionDir)TargetFileName.vsmacro')">"$([System.IO.File]::ReadAllText('$(SolutionDir)TargetFileName.vsmacro').trimend())"</TargetFileName_Global>
 </PropertyGroup>
</Project>

...and name it Directory.Build.props and place it into your Solution Directory (where your *.sln file resides).

Next, in Solution Explorer right-click on the ProjectA's Properties --> Configuration Properties --> Build Events --> Pre-Build Event --> Command Line and set it to:

echo $(TargetFileName) > $(SolutionDir)TargetFileName.vsmacro

Next, in Solution Explorer right-click on the ProjectB's Properties --> Configuration Properties --> C/C --> Preprocessor --> Preprocessor Definitions and add the following code there:

THE_OTHER_FILE_NAME=$(TargetFileName_Global);

Restart Visual Studio.

Now, you can use the THE_OTHER_FILE_NAME preprocessor macro in in ProjectB's *.h, *.c and *.cpp source files at will. This macro will be defined as a string of the ProjectA's target file name.

Note, that the THE_OTHER_FILE_NAME preprocessor macro in ProjectB will become defined only after you build the ProjectA. Before that happens, this macro will be defined as __UNDEFINED__. After building ProjectA you may have to refresh the Intellisense and the syntax highlighting of this macro in your source files by performing Rescan --> Rescan Solution from the context menu. Forgetting to rescan the solution this way, affects only the Intellisense display - it does not affect the building of any of the projects.

It is possible to do this with any other macro in Visual Studio 2019 and with multiple macros in multiple projects as well. This will not work with Visual Studio versions older than VS2019 because these versions ignore the Directory.Build.props file.

  • Related