www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using glibc headers with ImportC

reply qua <quanrong1 1mailbox1.org1removethe1s> writes:
I'm trying to get D to use my system's libc. First of all, is 
such a thing possible with importc? Walter Bright's slides on 
importc say it should be "as easy as import stdio;", but it 
doesn't seem to be the case yet. In the specification page I see 
that importc looks for .c and .i files, not .h . Is it possible 
to do what I'm trying to do? And if so, how?

By the way, I know I can "import core.stdc.stdio;" and I'm sure 
it's a better option. I'm not trying to do anything important 
here, it's just to figure out how it works.
Nov 12 2022
parent reply qua <quanrong1 1mailbox1.org1removethe1s> writes:
This is supposed to work, right?

```
import stdio;

void main() {
   printf("Hello, World!\n");
}
```


In my case, DMD complains of not being able to read `stdio.d`. 
What am I doing wrong? I'm not using any compiler switch.
Nov 12 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 12 November 2022 at 13:46:27 UTC, qua wrote:
 This is supposed to work, right?
No, it isn't. And it probably never will. importC looks for a .c file in the current directory. It is that .c file's responsibility to #include whatever .h files you want.
Nov 12 2022
next sibling parent reply qua <quanrong1 1mailbox1.org1removethe1s> writes:
On Saturday, 12 November 2022 at 14:14:06 UTC, Adam D Ruppe wrote:
 No, it isn't. And it probably never will.

 importC looks for a .c file in the current directory. It is 
 that .c file's responsibility to #include whatever .h files you 
 want.
Okay, thanks, got it. However, I'm still having issues to get it to compile. I have tried the example in the importc page and it compiles (and works) well. However, I can't get any example with `#include` to compile, including the simple hello.c in the importc page: ``` #include <stdio.h> int main() { printf("hello world\n"); return 0; } ``` I get the following errors: ``` hello.c(1): Error: identifier or `(` expected hello.c(5): Error: identifier or `(` expected hello.c(6): Error: identifier or `(` expected ``` Do I have to do anything else? I've read that importc doesn't have a preprocessor and I assume it is related to that, however "ImportC can automatically run the C preprocessor associated with the Associated C Compiler". Is it necessary to enable or specify it in some way? Thanks for your help.
Nov 12 2022
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 12 November 2022 at 14:39:14 UTC, qua wrote:
 Do I have to do anything else? I've read that importc doesn't 
 have a preprocessor and I assume it is related to that, however 
 "ImportC can automatically run the C preprocessor associated 
 with the Associated C Compiler".
I still don't think that's been released yet, so if you aren't on the git master or at least the latest beta build it isn't going to work. Which dmd version are you running?
Nov 12 2022
parent reply qua <quanrong1 1mailbox1.org1removethe1s> writes:
On Saturday, 12 November 2022 at 14:57:23 UTC, Adam D Ruppe wrote:
 I still don't think that's been released yet, so if you aren't 
 on the git master or at least the latest beta build it isn't 
 going to work. Which dmd version are you running?
I was running 2.098, I have just installed 2.100.2, which does say that use of #include is not supported. I have tried too to preprocess hello.c with `gcc -E`, but dmd is also unable to compile it preprocessed (with errors such as /usr/include/stdio.h(246): Error: found `__filename` when expecting `,` ). Is there any reasonably simple way to compile files such as this hello.c, or is it not ready yet? I'm not an experienced programmer, so I mostly want to know if I'm doing something wrong or if there's an alternative that doesn't require manual translations from C to D.
Nov 12 2022
parent reply bachmeier <no spam.net> writes:
On Saturday, 12 November 2022 at 15:08:22 UTC, qua wrote:
 On Saturday, 12 November 2022 at 14:57:23 UTC, Adam D Ruppe 
 wrote:
 I still don't think that's been released yet, so if you aren't 
 on the git master or at least the latest beta build it isn't 
 going to work. Which dmd version are you running?
I was running 2.098, I have just installed 2.100.2, which does say that use of #include is not supported. I have tried too to preprocess hello.c with `gcc -E`, but dmd is also unable to compile it preprocessed (with errors such as /usr/include/stdio.h(246): Error: found `__filename` when expecting `,` ). Is there any reasonably simple way to compile files such as this hello.c, or is it not ready yet? I'm not an experienced programmer, so I mostly want to know if I'm doing something wrong or if there's an alternative that doesn't require manual translations from C to D.
You're going to need to use the nightly from here: https://github.com/dlang/dmd/releases/tag/nightly While a lot of progress has been made, it's definitely not finished, so you may still encounter errors here and there.
Nov 12 2022
parent qua <quanrong1 1mailbox1.org1removethe1s> writes:
On Saturday, 12 November 2022 at 15:13:36 UTC, bachmeier wrote:
 You're going to need to use the nightly from here: 
 https://github.com/dlang/dmd/releases/tag/nightly

 While a lot of progress has been made, it's definitely not 
 finished, so you may still encounter errors here and there.
Thanks! Just installed the nightly version and it works as expected.
Nov 12 2022
prev sibling parent reply qua <quanrong1 1mailbox1.org1removethe1s> writes:
On Saturday, 12 November 2022 at 14:14:06 UTC, Adam D Ruppe wrote:
 On Saturday, 12 November 2022 at 13:46:27 UTC, qua wrote:
 This is supposed to work, right?
No, it isn't. And it probably never will. importC looks for a .c file in the current directory. It is that .c file's responsibility to #include whatever .h files you want.
It seems that it will be now :) https://issues.dlang.org/show_bug.cgi?id=23479 https://github.com/dlang/dmd/pull/14636 I agree it was unexpected that it didn't, at least for newcomers.
Nov 13 2022
parent Mike Parker <aldacron gmail.com> writes:
On Sunday, 13 November 2022 at 09:15:39 UTC, qua wrote:

 I agree it was unexpected that it didn't, at least for 
 newcomers.
Almost everyone is a newcomer when it comes to ImportC.
Nov 13 2022