How to extract compilation args for each compilation unit in a vcxproj?

冷暖自知 提交于 2019-12-22 10:52:46

问题


I'm trying to get the compilation args for each compilation unit so I can create the "compilation_commands.json" for my vcxproj that can be used with clang's libTooling.

The libTooling tutorial suggests using a CompilationDatabase to provide the compilation args for all the cpp files in a project. The tutorial shows that CMake can generate the compilation_commands.json for CMake based projects.

Since clang can be put into "MSVC mode" via clang.exe --driver-mode=cl or clang-cl.exe my thought was if I could get the compilation args for each cpp file in my VS2012 project I can create the compilation_commands.json for a vcxproj.

However I'm having trouble finding APIs in the VS2012 SDK that walks a vcxproj and retrieves the compilation args for each compilation unit. Can someone point me towards the right APIs?


回答1:


It is a DIY job. The VS IDE projects have properties for each .cpp file. By selecting a .cpp, then right click -> Properties -> Config properties -> C/C++ -> Command Line you have the options required to compile that specific file. The 64 dollar question is how to do it for every gile in the project / solution. The answer is not trivial, but it is doable. At least I did it on VS 2010 and I'm pretty sure it works on VS 2012/3

The secret lies with what is called VCEngine. It is a tool that evaluates all the properties or the files in the project. So the real problem is how to evaluate the property "Command Line" for each file. You need to iterate through all the .cpp files and call VCProject's Evaluate method for the "Command Line" property.

The simplest way I think you can do it is to write a plugin for the VS IDE and thus gain access to the VCEngine instance. There are plenty of examples of how to do that. Be aware that the VCEngine is version dependent.

Anywhay, for projects/solutions with thousands of .cpp files, you need to automate the compilation database creation.

When I'll have time I will put the solution on github




回答2:


You may be interested in reading this compile_commands.json for Windows/MSVC




回答3:


You can try to parse the CL.command.* files in the intermediate directories.

https://gist.github.com/Trass3r/f3fbe6807d28106e917368c33abf45d4




回答4:


I know this is really old, but the question is still relevant, so for those still looking for an answer, I managed to finally do this recently using this little VS extension.

Just install it and a new 'Sourcetrail' menu will appear, with a 'Create Compilation Database' entry (I found Intellisense needs to be enabled for it to be clickable). That will let you customize what to include and generate a compile_commands.json that you can use with other clang-based tools, etc.



来源:https://stackoverflow.com/questions/19672867/how-to-extract-compilation-args-for-each-compilation-unit-in-a-vcxproj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!