#import command line equivalent

若如初见. 提交于 2019-12-06 06:16:32

问题


Using Visual Studio with Microsoft's C++ compiler, we have several source files which use the Microsoft-specific '#import' directive to import a type library. For instance:

#import my_type_lib.tlb

I'd like to remove the #import from the source code, and replace it with a command line step to be executed via GNU Make. The necessary interface definitions (.idl source code) are available during the build.

How can I remove my dependency on #import and replace it with specialized build tools to be executed via the command line?


回答1:


As far as I know, there are no separate tools to generate code from the type lib.

You could do #import once, and then stash away the generated files and include them as regular source files. But then changes to the type library would need a new supervised build to regenerate the files.

Which parts of the generated information are you using? If you only use this to get to IIDs, interfaces and CLSIDs, and you have the .IDL, you can use MIDL.EXE to generate a C++ representation.

If you're using the wrapper classes (IxxxPtr), I think you're short on luck -- these are produced by #import.




回答2:


It just occurred to me that you could use the compiler directly to generate the .tli/.tlh files from a type library.

With a simple source file, like this

// imports.cpp
#import "foo.dll"
#import "bar.dll"

int main(int argc, char* argv[])
{
}

you can use cl.exe to generate #import wrappers, and a useless .exe file, like so:

...\>cl.exe imports.cpp

From there on, #include the wrappers (foo.tlh/.tli) in your regular source code.

Not sure that this is an improvement on #import, but at least you've extracted #import out of your code base.



来源:https://stackoverflow.com/questions/1821822/import-command-line-equivalent

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