Replacement for #import in Visual C++

2019-01-31 07:40发布

We have large C++ project that we used to compile with the /MP switch to take advantage of multiple cores.

However, we recently brought in some code that uses #import on a couple of tlb's, and #import is incompatibile with /MP, which means we are back to single threaded builds and a lot more time to get coffee.

Any suggestions on how to get #import and /MP to play nice? Is there a tool that will statically generate the C++ headers from a #import as a pre-build step?

Update:

Following Matt's advice worked great. For anyone else stumbling over this in google:

  1. create a separate static lib project
  2. set up enough includes so you can put the #import statement in the lib project
  3. make your main project dependent on the lib project (to ensure correct build order)
  4. add the lib project's temporary build folder to the include path for the main project
  5. #include the generated .tlh files where you were doing the #import
  6. enable the /MP switch and lose the coffee break time...

7条回答
劳资没心,怎么记你
2楼-- · 2019-01-31 07:53

You can use the /MP option for the project as a whole, and then make an exception for a single file using the /MP1 option.

查看更多
来,给爷笑一个
3楼-- · 2019-01-31 07:54

You could split the project into two parts, one that more or less does the import disabling /MP and one that does everything else enabling /MP.

查看更多
beautiful°
4楼-- · 2019-01-31 07:55

Why not just #include the generated headers created from #import?

查看更多
Bombasti
6楼-- · 2019-01-31 08:01

One option is to move the imports into a separate DLL and provide wrapper classes for them using an opaque pointer. Then disable /MP for that DLL only, and the rest of your build should be fine.

查看更多
贼婆χ
7楼-- · 2019-01-31 08:14

If you can limit the number of files you are #import'ing, you can put these in the precompiled header file (eg stdafx.h) which is already automatically excluded from /MP. This avoids the issue mentioned, since all other files will wait to compiler until stdafx.cpp is completed, and would all 'inherit' the same #import'ed definitions.

查看更多
登录 后发表回答