上午试图在C++ Builder加载DLL的,系统报错:
[Linker Error] DSOUND.LIB' contains invalid OMF record, type 0x21 (possibly COFF)
查help,google,bbs,用了很多种方法下午上课前才终于解决了问题:~(,
实验室的机器不能上国外的网,换了很多代理可大部分国外的论坛还是上不去,严重影响了debug效率。
记于此备忘!
Q: How to fix linker errors that say: "Invalid OMF record type 0x21 (possibly COFF)"
Answer
This error usually occurs when you try to link with a LIB file or an OBJ file that is not compatible with Borland's OMF file format. This typically happens when dealing with libraries from third party vendors. Often times, the third party vendor will supply you with a LIB file that is in Microsoft's COFF format. The Microsoft COFF format is incompatible with Borland's OMF format.
The best solution is to obtain Borland compatible files from the third party vendor. Unfortunately, many third party vendors will simply ignore you. If this happens to you, your choices are limited.
If the LIB file is an import library for a DLL, you can create a Borland compatible import library using Borland's IMPLIB tool. All you need to do is pass IMPLIB the name of the DLL. IMPLIB will generate a Borland compatible import library. Link with that library instead of the one from the vendor. Another option is to use Borland's COFF2OMF tool on the Microsoft import library. I prefer to use IMPLIB if the DLL is available. For more details on how to call an MSVC DLL from BCB, see the article at http://www.bcbdev.com/articles/vcdll.htm.
Note:
--------------------------------------------------------------------------------
IMPLIB and COFF2OMF will only work if the library and DLL export plain, C functions. If they export C++ classes or mangled function names, then you will get linker errors, or possibly errors while generating the Borland compatible import library. If you run into this problem, see the C++ DLL article at http://www.bcbdev.com/articles/vcdll2.htm.
--------------------------------------------------------------------------------
If the LIB file is a static library instead of an import library (static libraries contain real code, import libraries are just stubs for DLLs), then the situation becomes more convoluted. You cannot link MSVC static libraries with your BCB project. You can't easily convert the library from COFF format to OMF either. Borland's COFF2OMF tool only works on import libraries. The only clean solution is to use MSVC to create a DLL or a COM object that wraps the static library. Create a DLL with MSVC that exports a wrapper function for each routine in the static library. If the static library contains C++ classes, then you could be in for a rough ride. Consult the article at http://www.bcbdev.com/articles/vcdll2.htm. It describes how to wrap a C++ DLL with a Borland compatible DLL. Static C++ libraries are not quite the same, but they are very similiar. The techniques involved are practically the same.
If you have been given raw OBJs, then you follow the same advice from the preceding paragraph regarding static libraries. Static libraries are more or less collections of OBJ files. So it makes sense that you would have to follow the same, rather unpleasant, course of action. Create an MSVC wrapper DLL for the code in the OBJ, and call the DLL from your BCB application.
COFF2OMF
Convert Object and Lib Files to OMF
Digital Mars tools all work with the Intel 32 bit OMF object and library file format. Microsoft's 32 bit tools work with Microsoft's own variant of the COFF format. Many such COFF format files can be converted to OMF with the coff2omf program:
coff2omf inputfiles...
The input files can be either object files (.obj) or library files (.lib). They are converted in place to OMF.
For example, to convert user32.lib from COFF to OMF:
coff2omf user32.lib
The Microsoft COFF format apparently changed with Visual C++ 6.0. To use coff2omf on a .lib file with the newer format, use Microsoft's linker to convert the file to the earlier COFF format:
link /lib /convert file.lib
and then use coff2omf on it.
IMPLIB
Build an Import Library
An import library is necessary when calling functions in a DLL; it provides the stubs that hook up to the DLL at runtime. IMPLIB is used to create import libraries. It uses as input either the DLL itself or a module definition (.def) file.
implib commands have the following format:
implib [switches] libfile [ dllfile | deffile ]
libfile is the name of the import library file to create. .lib is the default extension.
dllfile is the name of the DLL to create an import library for. .dll is the default extension.
deffile is the name of the DLL's module definition file. The extension .def is required.
switches
/? Print this message.
/b[atch] Batch.
/h[elp] Print this message.
/i[gnorecase] Ignore case of symbols.
/noi[norecase] Be case sensitive. Mark library as case sensitive.
/nol[ogo] Ignored.
/now[ep] Ignore WEP (relevant only to 16 bit DLL's).
/ntd[ll] Ignored.
/p[agesize]:number Set page size to number (a power of 2).
/s[ystem] Prepend '_' to exported internal names. Used to create import library from Windows NT system DLLs (for example, kernel32.dll). Note that this switch is not available via the IDDE.
Either a dllfile or deffile must be given (but not both). IMPLIB reads the file, and uses the export records in it to create an import library libfile. libfile is then linked in with your application.
To create the import library kernel32.lib from the NT system dll kernel32.dll:
implib /s kernel32.lib kernel32.dll
Module definition files are in the same format as that used to create the DLL.
还要注意的是 coff2omf.exe 和 implib.exe 等程序都只能在windows控制台下运行,我找到这两个exe文件后,傻傻得点击了好久,就是不知道该怎么用,太土了,唉-,-b
解决了dll问题以后,后来的编程工作势如破竹,MR-H部分也快完成了,唯一懊恼的是各个环节的异常处理,总是没办法做得很严密,还把程序搞得支离破碎,可读性太低了。明天找高手问问有没有逐行检查状态的语法,try{...},catch{...}的自定义异常声明看不懂,不会用,555
晚上回来时,工作中断于和METERSOURCE通讯,置电流大小这一句,明天去查查具体什么错误,over
|