RejectedSoftware Forums

Sign up

Linking to C libraries

I'm currently working on a smallish application with a web interface, and vibe.d seems like a good fit for my needs.

However, the application depends on a large C library. Is it possible to instruct vibe to link to a certain static library when using the 'vibe' command from the project root? Or should I call rdmd myself?

Rene

Re: Linking to C libraries

Am 05.11.2012 14:23, schrieb Rene Zwanenburg:

I'm currently working on a smallish application with a web interface,
and vibe.d seems like a good fit for my needs.
However, the application depends on a large C library. Is it possible to
instruct vibe to link to a certain static library when using the 'vibe'
command from the project root? Or should I call rdmd myself?

Rene

You can put the library as a dflags entry into a package.json file in
the root directory:

{
	"name": "my-project",
	"description": "Project description...",
	"copyright": "Copyright (c) 2012",
	"authors": [],
	"dflags": [
		"path/to/somelib.lib",            (windows variant)
		"-L-lsomelib"                     (linux/mac variant)
	]
}

Or, alternatively, set the DFLAGS environment variable with the lib
file. Different dflags for different platforms are not supported yet,
but will be added shortly.

Re: Linking to C libraries

On Mon, 05 Nov 2012 07:23:49 -0600, Rene Zwanenburg
renezwanenburg@gmail.com wrote:

I'm currently working on a smallish application with a web interface,
and vibe.d seems like a good fit for my needs. However, the application
depends on a large C library. Is it possible to instruct vibe to link to
a certain static library when using the 'vibe' command from the project
root? Or should I call rdmd myself?

Rene

If you are using dmd, then a simple <br>pragma(lib, "gtkd");<br> is all that's necessary. But note that gdc completely ignores that. I
have no clue what ldc does.

I usually include a pragma(lib, "whatever") even if I have a build script.
It makes it easier for (myself or others) to pick it apart and compile
individual sections.
I can vibe, or dmd -Isource -J... whatever and it still links fine.

As an added bonus, It's a nice way to keep the list of external libs in
one place. Self-documenting code and all that.
So... I would do both pragmas and the dflags method mentioned.

Using Opera's revolutionary email client: http://www.opera.com/mail/

Re: Linking to C libraries

On Mon, 05 Nov 2012 14:39:00 +0100, Sönke Ludwig wrote:

Am 05.11.2012 14:23, schrieb Rene Zwanenburg:

I'm currently working on a smallish application with a web interface,
and vibe.d seems like a good fit for my needs.
However, the application depends on a large C library. Is it possible to
instruct vibe to link to a certain static library when using the 'vibe'
command from the project root? Or should I call rdmd myself?

Rene

You can put the library as a dflags entry into a package.json file in
the root directory:

{
	"name": "my-project",
	"description": "Project description...",
	"copyright": "Copyright (c) 2012",
	"authors": [],
	"dflags": [
		"path/to/somelib.lib",            (windows variant)
		"-L-lsomelib"                     (linux/mac variant)
	]
}

Or, alternatively, set the DFLAGS environment variable with the lib
file. Different dflags for different platforms are not supported yet,
but will be added shortly.

That works wonderfully. Thanks!

Re: Linking to C libraries

On Mon, 05 Nov 2012 21:26:39 -0600, 1100110 wrote:

On Mon, 05 Nov 2012 07:23:49 -0600, Rene Zwanenburg
renezwanenburg@gmail.com wrote:

I'm currently working on a smallish application with a web interface,
and vibe.d seems like a good fit for my needs. However, the application
depends on a large C library. Is it possible to instruct vibe to link to
a certain static library when using the 'vibe' command from the project
root? Or should I call rdmd myself?

Rene

If you are using dmd, then a simple <br>pragma(lib, "gtkd");<br> is all that's necessary. But note that gdc completely ignores that. I
have no clue what ldc does.

I usually include a pragma(lib, "whatever") even if I have a build script.
It makes it easier for (myself or others) to pick it apart and compile
individual sections.
I can vibe, or dmd -Isource -J... whatever and it still links fine.

As an added bonus, It's a nice way to keep the list of external libs in
one place. Self-documenting code and all that.
So... I would do both pragmas and the dflags method mentioned.

Using Opera's revolutionary email client: http://www.opera.com/mail/

Thanks for the suggesion! I always assumed pragma(lib) only worked on windows, not sure why. Probably because the only other compiler I know supporting it is VC.

IIRC it doesn't work with ldc, but that's not a problem since we're using dmd exclusively.

Re: Linking to C libraries

On Thu, 08 Nov 2012 17:33:29 -0600, Rene Zwanenburg
renezwanenburg@gmail.com wrote:

On Mon, 05 Nov 2012 21:26:39 -0600, 1100110 wrote:

On Mon, 05 Nov 2012 07:23:49 -0600, Rene Zwanenburg
renezwanenburg@gmail.com wrote:

I'm currently working on a smallish application with a web interface,
and vibe.d seems like a good fit for my needs. However, the
application depends on a large C library. Is it possible to instruct
vibe to link to a certain static library when using the 'vibe'
command from the project root? Or should I call rdmd myself?

Rene

If you are using dmd, then a simple <br>pragma(lib, "gtkd");<br> is all that's necessary. But note that gdc completely ignores that.
I have no clue what ldc does.
I usually include a pragma(lib, "whatever") even if I have a build
script.
It makes it easier for (myself or others) to pick it apart and compile
individual sections.
I can vibe, or dmd -Isource -J... whatever and it still links fine.
As an added bonus, It's a nice way to keep the list of external libs
in one place. Self-documenting code and all that.
So... I would do both pragmas and the dflags method mentioned.
-- Using Opera's revolutionary email client: http://www.opera.com/mail/

Thanks for the suggesion! I always assumed pragma(lib) only worked on
windows, not sure why. Probably because the only other compiler I know
supporting it is VC.

IIRC it doesn't work with ldc, but that's not a problem since we're
using dmd exclusively.

I think that ldc wants to support it, and gdc cannot support it.
Either way as long as the compilers implement the specification correctly,
it will just be ignored if they can't use it.

ldc wants a single binary compiler and linker, while gdc can't do this if
it wants to be included into gcc.

I could be wrong though.

Re: Linking to C libraries

If you are using dmd, then a simple <br>pragma(lib, "gtkd");<br> is all that's necessary. But note that gdc completely ignores that. I
have no clue what ldc does.

I usually include a pragma(lib, "whatever") even if I have a build script.
It makes it easier for (myself or others) to pick it apart and compile
individual sections.
I can vibe, or dmd -Isource -J... whatever and it still links fine.

As an added bonus, It's a nice way to keep the list of external libs in
one place. Self-documenting code and all that.
So... I would do both pragmas and the dflags method mentioned.

Does anyone have any luck linking with libxml2 ??

I have tried
pragma(lib, "libxml-2.0");
and
pragma(lib, "libxml2");

But neither seems to work.

Re: Linking to C libraries

On 2012-11-15 13:01, Knud Sørensen wrote:

Does anyone have any luck linking with libxml2 ??

I have tried
pragma(lib, "libxml-2.0");
and
pragma(lib, "libxml2");

But neither seems to work.

pragma(lin,"xml2") do the trick.