ImportC in a Dub project

Steven Schveighoffer schveiguy at gmail.com
Fri Oct 28 18:31:25 UTC 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


More information about the Digitalmars-d-learn mailing list