www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error 42 When Trying to Interop with a C# Libary

reply Sarcross <csarcross gmail.com> writes:
I do apologize if this has been covered before, but I haven't 
come across anyone with this particular issue.

I'm using D for a class project in which we're making a 
rudimentary resume parser. We're using C# to handle pulling 
plaintext from .docx files and using D for the rest. The plan was 
to make a .dll and use the library with D. Unfortunately, after 
jumping through several hoops to get the linker to recognize the 
.lib, the compiler keeps giving me Error 42: Symbol Undefined 
_testLib

App.d: 
https://gist.github.com/Sarcross/035376964b3256e7d48697947c09fbe7

WordFileParser.d: 
https://gist.github.com/Sarcross/0e124d7e3f6e6cca4efe16c29e53b027

DocumentParser.cs: 
https://gist.github.com/Sarcross/b508ae839ee543c7a8048fd827baeee5

I've been trying to work with this tutorial I came across: 
https://github.com/taylorh140/Calling-NET-from-D

And the .cs code is based off of this tutorial: 
https://www.dotnetperls.com/word

I have to say, I'm really at a loss. I've tried working on it 
with the Eclipse DDT plugin, then switched over to using the 
VisualD plugin for Visual Studio to see if there was something I 
may have been missing, but no dice. I tried using extern (C++), 
extern(Windows), and even extern(System) to see if there was any 
change, but it appears that extern (C) is the proper one to use, 
however still no luck.

If you're wondering, I did use the COFF2OMF tool on the library I 
got from the C# code, which is why in WordFileParser.d you'll see 
"pragma(lib, "Parser2")".
Nov 05 2016
parent reply John C <johnch_atms hotmail.com> writes:
On Saturday, 5 November 2016 at 16:13:18 UTC, Sarcross wrote:
 If you're wondering, I did use the COFF2OMF tool on the library 
 I got from the C# code, which is why in WordFileParser.d you'll 
 see "pragma(lib, "Parser2")".
I have never had success trying to link with a lib file converted by COFF2OMF. Are you able to use -m32mscoff? I was able to call a .NET DLL using the technique in the tutorial when I tried it a few months ago, using that DMD option instead.
Nov 05 2016
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 5 November 2016 at 20:45:55 UTC, John C wrote:
 I have never had success trying to link with a lib file 
 converted by COFF2OMF.
coff2omf works with .obj files coffimplib works on .lib files
Nov 05 2016
parent John C <johnch_atms hotmail.com> writes:
On Saturday, 5 November 2016 at 21:02:14 UTC, Adam D. Ruppe wrote:
 coff2omf works with .obj files
Well, that's not what the docs say! http://www.digitalmars.com/ctg/coff2omf.html "The input files can be either object files (.obj) or library files (.lib)" Apparently you have to convert them to an older COFF format first using Microsoft's linker. Not sure if I ever tried that though.
Nov 05 2016
prev sibling parent reply Sarcross <csarcross gmail.com> writes:
On Saturday, 5 November 2016 at 20:45:55 UTC, John C wrote:
 On Saturday, 5 November 2016 at 16:13:18 UTC, Sarcross wrote:
 If you're wondering, I did use the COFF2OMF tool on the 
 library I got from the C# code, which is why in 
 WordFileParser.d you'll see "pragma(lib, "Parser2")".
I have never had success trying to link with a lib file converted by COFF2OMF. Are you able to use -m32mscoff? I was able to call a .NET DLL using the technique in the tutorial when I tried it a few months ago, using that DMD option instead.
I added -m32mscoff to dflags in dub.json and it results in this error: LINK : fatal error LNK1104: cannot open file '+C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2.lib' --- errorlevel 1104 dmd failed with exit code 1104. ^^^ Terminated, exit code: 2 ^^^ For reference, my dub.json: { "name" : "resume-parser", "description": "A D resume parser.", "libs" : ["Parser2"], "dflags" : ["-m32mscoff"], "lflags" : ["+C:\\Users\\antzy_000\\Documents\\Programming\\D\\Resume-Parser\\src\\Parser2.lib"], "dependencies" : { } } I attempted changing Parser2.lib to the unconverted Parser.lib but it gives a similar error.
Nov 05 2016
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 5 November 2016 at 22:06:21 UTC, Sarcross wrote:

 LINK : fatal error LNK1104: cannot open file 
 '+C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2.lib'
 --- errorlevel 1104
 dmd failed with exit code 1104.
   "lflags" :  
 ["+C:\\Users\\antzy_000\\Documents\\Programming\\D\\Resume-Parser\\src\\Parser2.lib"],
The error linker error tells you that the linker thinks the path you specified with '+' is a filename (which is why it tacked .lib on the end), meaning it doesn't recognize it as a command line option. That's because when you compile with -m32mscoff, you are using the Microsoft linker (link) rather than optlink, the default. The '+path' syntax is something optlink understands, but link does not. You can find the MS linker commands at [1], in this case you want [2]: "lflags" : "/LIBPATH:C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2" [1] https://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx [2] https://msdn.microsoft.com/en-us/library/1xhzskbe.aspx
Nov 05 2016
parent reply Sarcross <csarcross gmail.com> writes:
On Sunday, 6 November 2016 at 02:37:23 UTC, Mike Parker wrote:
 On Saturday, 5 November 2016 at 22:06:21 UTC, Sarcross wrote:

 LINK : fatal error LNK1104: cannot open file 
 '+C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2.lib'
 --- errorlevel 1104
 dmd failed with exit code 1104.
   "lflags" :  
 ["+C:\\Users\\antzy_000\\Documents\\Programming\\D\\Resume-Parser\\src\\Parser2.lib"],
The error linker error tells you that the linker thinks the path you specified with '+' is a filename (which is why it tacked .lib on the end), meaning it doesn't recognize it as a command line option. That's because when you compile with -m32mscoff, you are using the Microsoft linker (link) rather than optlink, the default. The '+path' syntax is something optlink understands, but link does not. You can find the MS linker commands at [1], in this case you want [2]: "lflags" : "/LIBPATH:C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2" [1] https://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx [2] https://msdn.microsoft.com/en-us/library/1xhzskbe.aspx
Well Mike, that seems to have gotten me a step closer, but now a new error: Building Debug\Resume_Parser.exe... OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html Debug\Resume_Parser.obj Offset 00000H Record Type 004C Error 138: Module or Dictionary corrupt Building Debug\Resume_Parser.exe failed! I'm not quite sure how it could be corrupt since there haven't been any changes to it since I used the tool on the original .lib. As a "let me see" scenario, I tried switching back to the original .lib but get the same error. A bit of additional information here; In Properties >> Command Line, Visual Studio is using this in the "Command Line" section: "$(VisualDInstallDir)pipedmd.exe" dmd -g -debug -X -Xf"$(IntDir)\$(TargetName).json" -deps="$(OutDir)\$(ProjectName).dep" -of"$(OutDir)\$(ProjectName).exe" -map "$(INTDIR)\$(SAFEPROJECTNAME).map" -L/MAP:FULL src\Parser2.lib And I've added this to the "Additional options" section: -m32mscoff \LIBPATH:C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2.lib
Nov 06 2016
parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 6 November 2016 at 17:57:23 UTC, Sarcross wrote:

 Building Debug\Resume_Parser.exe...
 OPTLINK (R) for Win32  Release 8.00.17
 "$(VisualDInstallDir)pipedmd.exe" dmd -g -debug -X 
 -Xf"$(IntDir)\$(TargetName).json" 
 -deps="$(OutDir)\$(ProjectName).dep" 
 -of"$(OutDir)\$(ProjectName).exe" -map 
 "$(INTDIR)\$(SAFEPROJECTNAME).map" -L/MAP:FULL src\Parser2.lib

 And I've added this to the "Additional options" section:

 -m32mscoff 
 \LIBPATH:C:\Users\antzy_000\Documents\Programming\D\Resume-Parser\src\Parser2.lib
Notice that the linker error is coming from OPTLINK, not the MS linker. That, and your -m32mscoff isn't showing up in Visual D's command line output (als, the \LIBPATH should be /LIBPATH). What I would do rather than adding -m32mscoff in additional options is to go into the project properties in 'Configuration Properties -> Compiler -> Output' and check the box 'Use MS-COFF object file format for Win32 (DMD 2.067+)'. I know that has worked for me when using Visual D. Also, run 'Clean' before building again just to make sure you aren't mixing OMF/COFF object files.
Nov 06 2016
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 7 November 2016 at 06:22:16 UTC, Mike Parker wrote:

 What I would do rather than adding -m32mscoff in additional 
 options
Oh, and I forgot. Don't set the lib path in additional options either. In 'Configuration Properties -> Linker', enter the path in the 'Library Search Path' field. That way, you don't have to worry about the specific linker flags.
Nov 06 2016