Thread overview
how to import .lib library
Aug 15, 2021
Timofeyka
Aug 15, 2021
Mike Parker
Aug 15, 2021
Timofeyka
Aug 15, 2021
Mike Parker
Aug 15, 2021
jfondren
Aug 15, 2021
Mike Parker
Aug 15, 2021
Marcone
Aug 15, 2021
Marcone
August 15, 2021

Hello!
I may have a very stupid question, but still.
How do I include a .lib library? How to use it in your code?

August 15, 2021

On Sunday, 15 August 2021 at 09:49:39 UTC, Timofeyka wrote:

>

Hello!
I may have a very stupid question, but still.
How do I include a .lib library? How to use it in your code?

You don't import a .lib file. They are for the linker, not the compiler. How you make use of it depends on what sort of library it is and how you're building your project.

If this is all new to you, it will be easier just to specify here which library it is that you're wanting to use, then I or someone else can give you directions. But the general idea is as follows.

If it's a D library, you'll need access to the source code (or alternatively, D interface files that have a .di extension, but that's another topic). That's what you use at compile time via the import statement. When you import, for example, std.stdio, you are importing the module from the Phobos source tree that ships with the compiler.

If the library is registered with the dub repository, then you can use dub to manage and build your project to make life easier. The library's source will be available to import, and dub will build the library and make sure it's linked.

If the library is not registered with dub, you'll need to download the source somewhere, make sure it's on the import path (use the -I switch on the compiler command line with the source path, e.g., -I/path/to/source), you'll need to make sure the library is compiled separately from your project, and then you'll need to give the lib file to the compiler on the command line along with your source (e.g., dmd app.d library.lib).

If it's a C library, you'll need to translate the C API to D (not the source code, just the type and function declarations) if it hasn't been done already. Then you import the translated D files and give the .lib file to the compiler as above.

August 15, 2021

On Sunday, 15 August 2021 at 10:05:15 UTC, Mike Parker wrote:

>

You don't import a .lib file. They are for the linker, not the compiler. How you make use of it depends on what sort of library it is and how you're building your project.

If this is all new to you, it will be easier just to specify here which library it is that you're wanting to use, then I or someone else can give you directions. But the general idea is as follows.

If it's a D library, you'll need access to the source code (or alternatively, D interface files that have a .di extension, but that's another topic). That's what you use at compile time via the import statement. When you import, for example, std.stdio, you are importing the module from the Phobos source tree that ships with the compiler.

If the library is registered with the dub repository, then you can use dub to manage and build your project to make life easier. The library's source will be available to import, and dub will build the library and make sure it's linked.

If the library is not registered with dub, you'll need to download the source somewhere, make sure it's on the import path (use the -I switch on the compiler command line with the source path, e.g., -I/path/to/source), you'll need to make sure the library is compiled separately from your project, and then you'll need to give the lib file to the compiler on the command line along with your source (e.g., dmd app.d library.lib).

If it's a C library, you'll need to translate the C API to D (not the source code, just the type and function declarations) if it hasn't been done already. Then you import the translated D files and give the .lib file to the compiler as above.

Thank you for your reply!
I wanted to link to my project another project without source code.

August 15, 2021

On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote:

>

Thank you for your reply!
I wanted to link to my project another project without source code.

Yeah, that's not possible. You either need the source or a set of D interface files that declares all the symbols you need. The compiler has to be able to see the symbols to know what's available for you to use.

August 15, 2021

On Sunday, 15 August 2021 at 10:19:33 UTC, Mike Parker wrote:

>

On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote:

>

Thank you for your reply!
I wanted to link to my project another project without source code.

Yeah, that's not possible. You either need the source or a set of D interface files that declares all the symbols you need.

Meaning, it is possible. On Windows where I assume these .lib files are:

PS C:\Users\jfond> cat math.d
extern(C) int twice(int n) { return n * 2; }

PS C:\Users\jfond> cat mathuser.d
extern (C) int twice(int n);

void main() {
    import std.stdio : writeln;

    writeln(twice(21));
}

PS C:\Users\jfond> ldc2 -lib math.d
PS C:\Users\jfond> ldc2 mathuser.d math.lib
PS C:\Users\jfond> ./mathuser
42

math.lib is written in D but it could've been written just as well in C or C++ or anything, as long as it's targeting the C ABI in whatever language.

When mathuser.d is compiled, D does not need the source for math.lib. That one extern(C) function without a body is sufficient to, again targeting the C ABI, say "I am expecting to be linked with a function like this", and math.lib supplies that function at link time.

D is identical to pretty much every other native-compiled language in this respect.

The question you probably want to be asking is, "given a specific library from this vendor, what's the most convenient way to link D against it", or "how should I tell dub to link this D application with a .lib file in a parent directory", etc.

August 15, 2021

On Sunday, 15 August 2021 at 10:40:36 UTC, jfondren wrote:

> >

Yeah, that's not possible. You either need the source or a set of D interface files that declares all the symbols you need.

Meaning, it is possible. On Windows where I assume these .lib files are:

I mentioned C libraries in an earlier post. But the OP did not say whether the library is a C library or a D one. If it's a D library, then you can't simply declare the functions locally because the module name is part of the fully-qualifed name. You absolutely need the source or the interface files.

Declaring C functions locally where you need them is fine if you only need a handful of sybmols. But when you need multiple functions and types from the API, that's going to get unwieldy.

August 15, 2021

On Sunday, 15 August 2021 at 09:49:39 UTC, Timofeyka wrote:

>

Hello!
I may have a very stupid question, but still.
How do I include a .lib library? How to use it in your code?

Inside the source code you can use pragma. Example:

pragma(lib, "gdi32.lib");

In DMD command line you can use -L flag that pass the lib to linker. Example:

dmd -Lgdi32.lib mycode.d

August 15, 2021

On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote:

>

Thank you for your reply!
I wanted to link to my project another project without source code.

This tutorial can help you create yours libs: https://wiki.dlang.org/Win32_DLLs_in_D