Thread overview
Link with C static library
Jan 16, 2011
alvinsay
Jan 16, 2011
Jonathan M Davis
Jan 16, 2011
novice2
Jan 16, 2011
Trass3r
Jan 16, 2011
alvinsay
Jan 16, 2011
Trass3r
January 16, 2011
Hi,

Is it possible for D language to link with C static library under win32 platform?

Because visual studio produce COFF fomrat, I try to use coff2omf to convert libraries build by vs2008. However, it seems that coff2omf doesn't support static library.
Should I change my C compiler? Or, there is another way to covert static library from COFF to OMF??

January 16, 2011
On Sunday 16 January 2011 00:14:56 alvinsay wrote:
> Hi,
> 
> Is it possible for D language to link with C static library under win32 platform?
> 
> Because visual studio produce COFF fomrat, I try to use coff2omf to convert libraries build by vs2008. However, it seems that coff2omf doesn't support static library. Should I change my C compiler? Or, there is another way to covert static library from COFF to OMF??

optlink is not compatible with Microsoft's linker. So, unless you can somehow convert from one format to another, you can't use C code with D code which has been compiled by a Microsoft compiler. However, if you use dmc to compile your C code, then it links just fine, since it uses optlink as its linker, just like dmd does.

Apparently, linking against dlls somehow avoids the COFF vs OMF problem, so that would be another way to get around the problem: use a dll for the C code.

Regardless, the fact that optlink unfortunately uses a different format than Microsoft's compiler means that you can normally link to C code on Windows unless you have all of the source code and can compile it yourself with dmc. Maybe someone else knows of a good library format converter though.

- Jonathan M Davis
January 16, 2011
> Is it possible for D language to link with C static library under win32 platform?

yes, imho, it is possible. i successfully tried such thing with dev-cpp + gcc + coff2omf.

btw, coff2omf.exe says:

COFF to OMF Object Module Conversion Utility, Version 1.00.195
Copyright (C) 1994 by Walter Oney
All rights reserved
January 16, 2011
> Should I change my C compiler? Or, there is another way to covert static library from COFF to OMF??

coff2omf should work, but unfortunately it isn't free.
coffimplib is free, but can only convert import libraries.

You may try to use objconv: http://www.agner.org/optimize/#objconv
Though it doesn't work in all cases.

Compiling with dmc solves the issue but is often tedious cause headers are different etc.
Easiest solution if you can't do it statically is to use dlls.
January 16, 2011
> coff2omf should work, but unfortunately it isn't free.
Actually, I downloaded coff2omf from ftp.digitalmars.com.
I used it to convert static library, and it says the input library is not import
library.
Is that the same version as what you mentioned non-free version?

> coffimplib is free, but can only convert import libraries.
> You may try to use objconv: http://www.agner.org/optimize/#objconv
> Though it doesn't work in all cases.
> Compiling with dmc solves the issue but is often tedious cause headers are
> different etc.
> Easiest solution if you can't do it statically is to use dlls.
Many existing C libraries are not written as dynamic libraries. Some C libraries can be compiled as DLL, but in performance critical application, such as games, they must use static link to increase performance. I hope there is a good solution for linking C static libraries.




January 16, 2011
> I used it to convert static library, and it says the input library is not import library.

That's coffimplib.
coff2omf is in the Extended Utilities Package which is sold for 15$.


> Many existing C libraries are not written as dynamic libraries. Some C libraries can be
> compiled as DLL, but in performance critical application, such as games, they must use
> static link to increase performance. I hope there is a good solution for linking C static libraries.

Well maybe Walter will give away coff2omf (and maybe obj2asm too) for free some day to solve this problematic situation.

As I said, either you buy coff2omf, try objconv or compile with dmc.