Thread overview
Undefined reference to "S_ISDIR" and "S_ISREG" when compiling with "-betterC"
Jan 10, 2022
rempas
Jan 10, 2022
Adam D Ruppe
Jan 10, 2022
rempas
Jan 10, 2022
Adam D Ruppe
Jan 10, 2022
rempas
Jan 10, 2022
rempas
Jan 10, 2022
Adam D Ruppe
Jan 13, 2022
rempas
January 10, 2022

I'm having the following code:

import core.sys.posix.sys.stat;

int is_dir(const char* path) {
  stat_t s;
  if (stat(path, &s) == 0) {
    if (S_ISDIR(s.st_mode)) return 1;
    else return 0;
  } else return 2;
}

int is_regf(const char* path) {
  stat_t s;
  if (stat(path, &s) == 0) {
    if (S_ISREG(s.st_mode)) return 1;
    else return 0;
  } else return 2;
}

extern (C) void main() {
  is_dir("/lol");
  is_regf("/tmp");
}

When I try to compile this code using "ldc2" and "dmd", I'm getting the following message (the "format" is different between the compilers tho):

estd.o:test.d:function _D4test6is_dirFxPaZi: error: undefined reference to '_D4core3sys5posixQk4stat7S_ISDIRFNbNikZb'
testd.o:test.d:function _D4test7is_regfFxPaZi: error: undefined reference to '_D4core3sys5posixQk4stat7S_ISREGFNbNikZb'

If I open the file from the "stat" module (in my linux system it's in "/usr/include/dlang/ldc/core/sys/posix/sys/stat.d"), I will see that these functions do exists and the weird thing is that it will work if I don't compile using the "-betterC" option but it will not if I do. If I'm not mistaken, "core*" should work with "-betterC". Any ideas?

January 10, 2022
On Monday, 10 January 2022 at 19:20:56 UTC, rempas wrote:
> If I'm not mistaken, "core*" should work with "-betterC". Any ideas?

You are mistaken, -betterC's main mission is to exclude core.

Declarations that just bind to something externally will still work, since it doesn't matter if they're included or not, but this one, being a macro in the original C, is a mini-function in D, and thus needs to be linked in.
January 10, 2022
On Monday, 10 January 2022 at 19:28:56 UTC, Adam D Ruppe wrote:
>
> You are mistaken, -betterC's main mission is to exclude core.
>
> Declarations that just bind to something externally will still work, since it doesn't matter if they're included or not, but this one, being a macro in the original C, is a mini-function in D, and thus needs to be linked in.

Thanks! It needs to be linked with which library? In C it doesn't need to be linked with nothing extra than the system library that the C compilers already link (unless you pass "-nostdlib" of course).
January 10, 2022
On Monday, 10 January 2022 at 19:33:36 UTC, rempas wrote:
> Thanks! It needs to be linked with which library?

That segment of druntime.

You can get dmd to do it for you by using the `-i -i=core.sys` flags (or maybe just the -i=core.sys thing im not sure i haven't actually tried).

Or just stop using -brokenD and find joy lol

> In C it doesn't need to be linked with nothing extra than the system library that the C compilers already link (unless you pass "-nostdlib" of course).

In C, that I_ISDIR is a macro in the header file so there's nothing to link, it all gets inlined by the preprocessor.

January 10, 2022
On Monday, 10 January 2022 at 20:11:06 UTC, Adam D Ruppe wrote:
> On Monday, 10 January 2022 at 19:33:36 UTC, rempas wrote:
>> Thanks! It needs to be linked with which library?
>
> That segment of druntime.
>
> You can get dmd to do it for you by using the `-i -i=core.sys` flags (or maybe just the -i=core.sys thing im not sure i haven't actually tried).
>
> Or just stop using -brokenD and find joy lol
>
>> In C it doesn't need to be linked with nothing extra than the system library that the C compilers already link (unless you pass "-nostdlib" of course).
>
> In C, that I_ISDIR is a macro in the header file so there's nothing to link, it all gets inlined by the preprocessor.

I see. Thanks! I will see what this function is supposed to do in the "stat.d" file or I'll write assembly and implement the system call be myself. Have a nice day my friend!
January 10, 2022
On Monday, 10 January 2022 at 20:28:43 UTC, rempas wrote:
> I see. Thanks! I will see what this function is supposed to do in the "stat.d" file or I'll write assembly and implement the system call be myself. Have a nice day my friend!

When never mind, I just copy pasted the macros from "stat.h" and everything seems to be working fine
January 10, 2022
On Monday, 10 January 2022 at 20:28:43 UTC, rempas wrote:
> I'll write assembly and implement the system call be myself.

It's not a system call, it just a bitflag tester.
January 13, 2022
On Monday, 10 January 2022 at 21:17:22 UTC, Adam D Ruppe wrote:
> It's not a system call, it just a bitflag tester.

I wasn't talking about the flag actually but for the "stat" system call