0

I am building a project in C# to interface with a C++ library. Because I want this to be cross platform I am using .NET Core RC3 though I'm not sure if that is relevant.

Right now I have the cpp files in my project and I have a PreBuildEvent like

<PreBuildEvent>
   gcc -c -fPIC -std=c++11  "$(SolutionDir)ProjectName\cfile.cpp" -o"$(SolutionDir)ProjectName\cfile.o"
  gcc -shared -o "$(SolutionDir)ProjectName\cfile.dll" "$(SolutionDir)ProjectName\cfile.o" -lstdc++
</PreBuildEvent>

The DLL gets built, and then copied to the output directory. Then I use PInvoke to communicate with it.

This part works. Then I add a test project that references the first project. The part that doesn't work is when I try to do dotnet test.

Then it says

gcc : error : ProjectName\cfile.cpp: No such file or directory

I guess $(SolutionDir) must not be set in this case, or something.

If I change the project to a command line application, and do dotnet run that doesn't work either.

$(ProjectDir) doesn't seem to be available in this case either in the PreBuildEvent or the PostBuildEvent.

What's the best way to set this up so it works?

reveazure
  • 653
  • 2
  • 7
  • 14
  • Your prebuild event is in a project file, which likely has a fixed location relative to the solution (else you wouldn't be able to use SolutionDir in the first place). So specify the path to the cpp file relative to $(ProjectDir) instead. – stijn Feb 09 '17 at 09:04
  • For some reason $(ProjectDir) evaluates to nothing in both the prebuild and postbuild events. – reveazure Feb 09 '17 at 16:45
  • The problem also occurs if I change the project to a console application and try to do `dotnet run` on it. – reveazure Feb 09 '17 at 16:48

1 Answers1

0

Looks like $(MSBuildProjectDirectory) should be used instead ... When does MSBuild set the $(ProjectName) property?

This seems to work for all combinations of build, test, and run in Visual Studio and from the command line.

Community
  • 1
  • 1
reveazure
  • 653
  • 2
  • 7
  • 14