fungos / cr

cr.h: A Simple C Hot Reload Header-only Library
https://fungos.github.io/cr-simple-c-hot-reload/
MIT License
1.57k stars 103 forks source link

How to use this with Visual Studio? #74

Closed sbond75 closed 1 year ago

sbond75 commented 1 year ago

Hello,

I have seen this tip but I have a solution file in Visual Studio and can use the IDE itself or msbuild to compile my project from the command-line. In my solution file I have a project that produces a DLL that cr.h is managing as the "guest application", and the "host" is an executable that is being debugged by Visual Studio. It would be ideal to be able to recompile the DLL while the host program is running in the debugger. I have tried externally invoking msbuild while it runs but it says the PDB file is locked. Apparently this is a common issue with hot reloading. Is this not the intended setup, or is there something I'm missing?

fungos commented 1 year ago

That is supposed to work, as cr was created to solve the dll lock issue. Debugging the host should not lock the plug-in in any way, not sure what may be happening on your case. Try to validate that the plug-in is actually being copied and versioned in the output folder.

sbond75 commented 1 year ago

I saw that the plugin was copied in the output folder with a "1" appended. What is the copy of the DLL used for? I would imagine it would be better to unload the DLL, invoke the compiler, then reload the DLL?

fungos commented 1 year ago

cr host will monitor any timestamp change on the original dll, once detected it will unload the current loaded which is a copy that cr host does before loading one dll, and so it will copy this dll over and append a number which is used to track live rollback. Which this, the host will never lock the original dll compiled as it never uses that but only a copy. There are some details though though, just the copy will not actually guarantee the original dll isn't locked as cr needs to patch some debug info inside the copied dll. in your case, you may have a problem there, where cr is unable to patch this data for some reason (path invalid? too long?), best would be to enable cr tracing and debug the cr host loading the dll.

sbond75 commented 1 year ago

I found that I needed to link against the DLL from the host with /DELAY:UNLOAD and /DELAYLOAD: with the dll name appended, following this MSVC feature. However, I have not tested it with cr.h and am now using this for hot reload. If I need to switch back to it, it would probably work fine since this also seems to fix the pdb lock.