Hello!
I may have a very stupid question, but still.
How do I include a .lib library? How to use it in your code?
Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 15, 2021 how to import .lib library | ||||
---|---|---|---|---|
| ||||
August 15, 2021 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timofeyka | On Sunday, 15 August 2021 at 09:49:39 UTC, Timofeyka wrote: >Hello! 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 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 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 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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 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 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! |
August 15, 2021 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timofeyka | On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote: >Thank you for your reply! 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 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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! 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:
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 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to jfondren | 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 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timofeyka | On Sunday, 15 August 2021 at 09:49:39 UTC, Timofeyka wrote: >Hello! 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 Re: how to import .lib library | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timofeyka | On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote: >Thank you for your reply! This tutorial can help you create yours libs: https://wiki.dlang.org/Win32_DLLs_in_D |