Jump to page: 1 2
Thread overview
ImportC in a Dub project
Oct 28, 2022
Carsten Schlote
Oct 28, 2022
Imperatorn
Oct 28, 2022
Carsten Schlote
Oct 28, 2022
Carsten Schlote
Oct 28, 2022
Imperatorn
Oct 28, 2022
Carsten Schlote
Oct 28, 2022
Imperatorn
Oct 30, 2022
Carsten Schlote
Applied fix to Dub (Was: ImportC in a Dub project )
Oct 30, 2022
Carsten Schlote
Oct 30, 2022
Imperatorn
Oct 30, 2022
Carsten Schlote
Oct 30, 2022
Imperatorn
October 28, 2022

Hi,

I created a Dub project containing two files: app.d and zstd_binding.c

$ cat source/zstd_binding.c

#include <zstd.h>
#include <zstd_errors.h>

#include <stdio.h>

void relatedCode(void)
{
	printf("Hallo! This is some output from C code!\n");
}

and

$ cat source/app.d
import std.conv;
import std.stdio;

import zstd_binding;

void main()
{
	auto versionNr = ZSTD_versionNumber();
	auto versionStr = ZSTD_versionString();
	writefln("Version Info: Numeric %d String %s", versionNr, versionStr.to!string);

	/** more code stripped */

	relatedCode(); // The linker can't find it....
}

Accessing ZSTD works perfect. For linking you must give "-lzstd". So far, so good. The problem started, when I try to access the relatedCode() C function. There is simply no code generated for this C function. I need to compile the C separately into an object file and pass it as extra linker argument.

I expected that ImportC also allows to import C functions and creates the required code on the fly.

Is this a bug? What's wrong?

October 28, 2022

On Friday, 28 October 2022 at 17:45:59 UTC, Carsten Schlote wrote:

>

Hi,

I created a Dub project containing two files: app.d and zstd_binding.c

[...]

Are you using DMD?

October 28, 2022

On Friday, 28 October 2022 at 17:56:57 UTC, Imperatorn wrote:

>

On Friday, 28 October 2022 at 17:45:59 UTC, Carsten Schlote wrote:

>

Hi,

I created a Dub project containing two files: app.d and zstd_binding.c

[...]

Are you using DMD?

I'm using

$ dub --version
DUB version 1.29.2, built on Sep 29 2022
$ dmd-beta --version
DMD64 D Compiler v2.101.0-beta.1-36-ga2865d74fb
Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved written by Walter Bright
October 28, 2022

On 10/28/22 1:45 PM, Carsten Schlote wrote:

>

Hi,

I created a Dub project containing two files: app.d and zstd_binding.c

$ cat source/zstd_binding.c

#include <zstd.h>
#include <zstd_errors.h>

#include <stdio.h>

void relatedCode(void)
{
     printf("Hallo! This is some output from C code!\n");
}

and

$ cat source/app.d
import std.conv;
import std.stdio;

import zstd_binding;

void main()
{
     auto versionNr = ZSTD_versionNumber();
     auto versionStr = ZSTD_versionString();
     writefln("Version Info: Numeric %d String %s", versionNr, versionStr.to!string);

     /** more code stripped */

     relatedCode(); // The linker can't find it....
}

Accessing ZSTD works perfect. For linking you must give "-lzstd". So far, so good. The problem started, when I try to access the relatedCode() C function. There is simply no code generated for this C function. I need to compile the C separately into an object file and pass it as extra linker argument.

I expected that ImportC also allows to import C functions and creates the required code on the fly.

Is this a bug? What's wrong?

Are you passing the c file to the compiler? Also, you must be using dmd for ImportC currently.

What is your build line?

-Steve

October 28, 2022

On Friday, 28 October 2022 at 18:31:25 UTC, Steven Schveighoffer wrote:

>

Are you passing the c file to the compiler? Also, you must be using dmd for ImportC currently.

What is your build line?

$ cat dub.json
{
	"authors": [
		"Carsten Schlote"
	],
	"copyright": "Copyright © 2022, Carsten Schlote",
	"description": "A minimal D application.",
	"license": "proprietary",
	"name": "importc-app",
	"lflags": [ "-lzstd", "zstd_binding.o" ],
	"preBuildCommands": [ "gcc -g -O0 -c -o zstd_binding.o source/zstd_binding.c"]
$ dub build --compiler=dmd-beta -v
Using dub registry url 'https://code.dlang.org/'
Refreshing local packages (refresh existing: true)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
Looking for local package map at /home/cschlote/.dub/packages/local-packages.json
Looking for local package map at /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/packages/local-packages.json
Note: Failed to determine version of package importc-app at .. Assuming ~master.
Refreshing local packages (refresh existing: false)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
Looking for local package map at /home/cschlote/.dub/packages/local-packages.json
Looking for local package map at /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/packages/local-packages.json
Refreshing local packages (refresh existing: false)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
Looking for local package map at /home/cschlote/.dub/packages/local-packages.json
Looking for local package map at /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/packages/local-packages.json
Generating using build
Configuring dependent importc-app, deps:
Performing "debug" build using dmd-beta for x86_64.
Target '/home2/cschlote/build/dlang/importc-tests/dubbed/.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app' doesn't exist, need rebuild.
importc-app ~master: building configuration "application"...
Running pre-build commands...
Running gcc -g -O0 -c -o zstd_binding.o source/zstd_binding.c
dmd-beta -c -of.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app.o -debug -g -w -version=Have_importc_app -Isource/ source/app.d -vcolumns
Linking...
dmd-beta -of.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app .dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app.o -L--no-as-needed -L-lzstd -Lzstd_binding.o -g
Copying target from /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app to /home2/cschlote/build/dlang/importc-tests/dubbed

Without the preBuildCommands and without passing the resulting object file to the linker, the relatedCode() doesn't exist. And linking is failing.

October 28, 2022

On Friday, 28 October 2022 at 18:43:21 UTC, Carsten Schlote wrote:

>

On Friday, 28 October 2022 at 18:31:25 UTC, Steven Schveighoffer wrote:

>

[...]

$ cat dub.json
{
	"authors": [
		"Carsten Schlote"
	],
	"copyright": "Copyright © 2022, Carsten Schlote",
	"description": "A minimal D application.",
	"license": "proprietary",
	"name": "importc-app",
	"lflags": [ "-lzstd", "zstd_binding.o" ],
	"preBuildCommands": [ "gcc -g -O0 -c -o zstd_binding.o source/zstd_binding.c"]

[...]

Like schveiguy said, what's your build line? dmd should pick up the file if the import has the same name as the file.

October 28, 2022

On Friday, 28 October 2022 at 18:56:03 UTC, Imperatorn wrote:

>

Like schveiguy said, what's your build line? dmd should pick up the file if the import has the same name as the file.

What do you mean with 'buildline'? The project is build with dub. See previous posts with outputs from commandline.

DMD does find and import the C file. But it doesn't create code for relatedCode() in the C file. The only way to fix this, is to compile it with a C compiler and use the object as additional linker output.

IMHO this is a bug.

October 28, 2022

On 10/28/22 2:43 PM, Carsten Schlote wrote:

>

On Friday, 28 October 2022 at 18:31:25 UTC, Steven Schveighoffer wrote:

>

Are you passing the c file to the compiler? Also, you must be using dmd for ImportC currently.

What is your build line?

$ cat dub.json
{
     "authors": [
         "Carsten Schlote"
     ],
     "copyright": "Copyright © 2022, Carsten Schlote",
     "description": "A minimal D application.",
     "license": "proprietary",
     "name": "importc-app",
     "lflags": [ "-lzstd", "zstd_binding.o" ],
     "preBuildCommands": [ "gcc -g -O0 -c -o zstd_binding.o source/zstd_binding.c"]
$ dub build --compiler=dmd-beta -v
Using dub registry url 'https://code.dlang.org/'
Refreshing local packages (refresh existing: true)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
Looking for local package map at /home/cschlote/.dub/packages/local-packages.json
Looking for local package map at /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/packages/local-packages.json
Note: Failed to determine version of package importc-app at .. Assuming ~master.
Refreshing local packages (refresh existing: false)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
Looking for local package map at /home/cschlote/.dub/packages/local-packages.json
Looking for local package map at /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/packages/local-packages.json
Refreshing local packages (refresh existing: false)...
Looking for local package map at /var/lib/dub/packages/local-packages.json
Looking for local package map at /home/cschlote/.dub/packages/local-packages.json
Looking for local package map at /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/packages/local-packages.json
Generating using build
Configuring dependent importc-app, deps:
Performing "debug" build using dmd-beta for x86_64.
Target '/home2/cschlote/build/dlang/importc-tests/dubbed/.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app' doesn't exist, need rebuild.
importc-app ~master: building configuration "application"...
Running pre-build commands...
Running gcc -g -O0 -c -o zstd_binding.o source/zstd_binding.c
dmd-beta -c -of.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app.o -debug -g -w -version=Have_importc_app -Isource/ source/app.d -vcolumns
Linking...
dmd-beta -of.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app .dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app.o -L--no-as-needed -L-lzstd -Lzstd_binding.o -g
Copying target from /home2/cschlote/build/dlang/importc-tests/dubbed/.dub/build/application-debug-linux.posix-x86_64-dmd_v2.101.0-beta.1-36-ga2865d74fb-9DB8925B9A720899BDD003C68BC85F85/importc-app to /home2/cschlote/build/dlang/importc-tests/dubbed

Without the preBuildCommands and without passing the resulting object file to the linker, the relatedCode() doesn't exist. And linking is failing.

By default dub does not build C files (as evidenced by your command line). It may not even let you I don't know, but try:

  "sourceFiles" : ["source/zstdc_binding.c"]

-Steve

October 28, 2022

On Friday, 28 October 2022 at 19:04:21 UTC, Carsten Schlote wrote:

>

On Friday, 28 October 2022 at 18:56:03 UTC, Imperatorn wrote:

>

Like schveiguy said, what's your build line? dmd should pick up the file if the import has the same name as the file.

What do you mean with 'buildline'? The project is build with dub. See previous posts with outputs from commandline.

DMD does find and import the C file. But it doesn't create code for relatedCode() in the C file. The only way to fix this, is to compile it with a C compiler and use the object as additional linker output.

IMHO this is a bug.

Have you read this?

https://dlang.org/spec/importc.html

October 30, 2022

On Friday, 28 October 2022 at 19:08:47 UTC, Steven Schveighoffer wrote:

>

By default dub does not build C files (as evidenced by your command line). It may not even let you I don't know, but try:

  "sourceFiles" : ["source/zstdc_binding.c"]

Ok, this works. So Dub is not picking any C files by default. Thanks for the hint.

I will spent some time to prepare a fix for Dub, so that C files (only '.c' files, no headers) in detected/configured source directories are picked up as well.

« First   ‹ Prev
1 2