0

I am using visual studio 2019 for a c++ project. In this project, I generate a cpp file on build (pre-build event) and, if needed, modify the .vcxproj in order to add the file to the project. When the file is already present and thus only overwritten, it works perfectly.

However, if I want to generate the file and add it at the start of compilation, it won't detect the file and I will need to hit compile again in order for it to work (as it has been generated and added on the first compile, even though it failed). There is also a pop-up window at the end of the first compile telling me some files were changed and asking me what to do (reload/reload all/ignore/ignore all).

Is there a way to make it work on first try ? Maybe with a custom build event set before anything else ? Or is there a way to modify the execution order of build events ? (to set pre-build events before anything else)

HalfLion
  • 59
  • 3
  • Have you tried CMake? – prehistoricpenguin Jun 30 '21 at 06:16
  • @prehistoricpenguin I would like to use the default compiler if possible. – HalfLion Jun 30 '21 at 06:23
  • CMake is a build tool, which is very helpful for your case, see this: https://stackoverflow.com/questions/20824194/cmake-with-google-protocol-buffers – prehistoricpenguin Jun 30 '21 at 06:27
  • @prehistoricpenguin I know what CMake is, but I would like to use as few tools as possible. Thanks for the suggestion anyway, I'll most probably use it if I don't find another way of doing this without :) – HalfLion Jun 30 '21 at 06:32
  • I'm with prehistoricpenguin with this one regarding CMake, however as a workaround, I'd suggest for your use case to always generate the source file, but leave it empty if you are not going to use it. – Jan Gabriel Jun 30 '21 at 08:17

1 Answers1

0

I actually found a solution ! Instead of creating a console project, you need to create a MakeFile project which will call a .bat you make. In this .bat you have to call a .exe which will create the visual studio files (.vcxproj, .sln etc...), generate the cpp files (and do all pre-build events I want, basically) then call the compiler and linker (with the right arguments, depending on the platform, 32/64 bits, release/debug etc...). That way you can control what you compile and what you do around it (like, I need post-build events that always execute, even if the build fails, which you can't do in a basic console project)

HalfLion
  • 59
  • 3