Jump to page: 1 2
Thread overview
Libc functions undefined when linking
Jan 04, 2022
Ben Jones
Jan 04, 2022
Ali Çehreli
Jan 04, 2022
Ben Jones
Jan 04, 2022
Adam D Ruppe
Jan 04, 2022
Ben Jones
Jan 04, 2022
Ben Jones
Jan 04, 2022
Adam D Ruppe
Jan 04, 2022
Ben Jones
Jan 04, 2022
Ben Jones
Jan 04, 2022
Adam D Ruppe
Jan 04, 2022
Ben Jones
Jan 04, 2022
bachmeier
Jan 05, 2022
Tejas
Jan 05, 2022
Ben Jones
Jan 06, 2022
Tejas
January 04, 2022

I have a somewhat unusual use case and I'm having trouble getting everything to link properly.

I'm writing an assignment for a course I'm teaching and I've written the skeleton code in D, and students are going to implement one function in C, which my skeleton code will call. The tricky part is that the lab machines that the students will be using don't have a D compiler installed (they're Fedora machines, and I didn't see a dmd package in their repos, or I would have asked the admins to install it).

I plan to distribute compiled .o files for all the d modules, along with a static libphobos. The students can compile their C file, and then link everything together. I haven't tested it on the linux machines yet, but developing it on my mac, I'm getting undefined symbols for libc and pthread functions from libphobos:

"_thread_suspend", referenced from:
      __D4core6thread8osthread7suspendFNbNiCQBjQBhQBd6ThreadZb in libphobos2.a(osthread_8db_302.o)
     (maybe you meant: _thread_suspendAll)
  "_tmpfile", referenced from:
      __D3std5stdio4File7tmpfileFNfZSQBcQBbQy in libphobos2.a(stdio_d42_180.o)
  "_toupper", referenced from:
      __D2rt6config16rt_envvarsOptionFNbNiAyaMDFNbNiQkZQnZQq in libphobos2.a(config_94b_6c3.o)
  "_waitpid", referenced from:
      __D4core8internal2gc2os8wait_pidFNbNiibZEQBmQBkQBeQBe11ChildStatus in libphobos2.a(os_610_351.o)

So I think I need to specify that I want to explicitly include libc when I link it. -lc didn't seem to work. Anyone how the right way to do so?

Here are the steps I'm doing. I'll compile all the D stuff myself, and distribute the .o files with my assignment, and they'll just be doing the C compilation and linking steps.

dmd -c -I=source -of=build/simpledisplay.o source/arsd/simpledisplay.d
dmd -c -I=source -of=build/color.o source/arsd/color.d
dmd -c -I=source -of=app.o source/app.d
clang -c -o source/assignment1.o source/assignment1.c
ld build/*.o -L. -lphobos2 -o build/executable
January 04, 2022
On 1/4/22 10:13 AM, Ben Jones wrote:

> So I think I need to specify that I want to explicitly include libc when
> I link it.  `-lc` didn't seem to work.

Did you add -lc and -lpthread on the linker line?

> ld build/*.o -L. -lphobos2 -o build/executable

Ali

January 04, 2022
On Tuesday, 4 January 2022 at 18:26:41 UTC, Ali Çehreli wrote:
> On 1/4/22 10:13 AM, Ben Jones wrote:
>
> > So I think I need to specify that I want to explicitly
> include libc when
> > I link it.  `-lc` didn't seem to work.
>
> Did you add -lc and -lpthread on the linker line?
>
> > ld build/*.o -L. -lphobos2 -o build/executable
>
> Ali

Maybe it's a mac thing and that will work when I get to linux, but I did try that and ld couldn't find the lib:

```
ld: library not found for -lc
```

Same result for -lpthread, and for -pthread (no l)
January 04, 2022
On Tuesday, 4 January 2022 at 18:13:56 UTC, Ben Jones wrote:
> clang -c -o source/assignment1.o source/assignment1.c

you might have better luck just telling clang to link it too

like

clang source/assignment1.o -lphobos2 build/*.o # etc

since there's a bunch of default search paths and libs etc the compiler pass to the linker.

otherwise if you do need to use the linker directly worth remembering the order of args matter too. you need to put the more derived dependencies first followed by more general ones at the end
January 04, 2022
On Tuesday, 4 January 2022 at 18:37:25 UTC, Adam D Ruppe wrote:
> On Tuesday, 4 January 2022 at 18:13:56 UTC, Ben Jones wrote:
>> clang -c -o source/assignment1.o source/assignment1.c
>
> you might have better luck just telling clang to link it too
>
> like
>
> clang source/assignment1.o -lphobos2 build/*.o # etc
>
> since there's a bunch of default search paths and libs etc the compiler pass to the linker.
>
> otherwise if you do need to use the linker directly worth remembering the order of args matter too. you need to put the more derived dependencies first followed by more general ones at the end

Using clang seems to work better.  It sounds like Apple basically hides system libraries since Big Sur, and I guess their clang knows the magic flags to pass to find them.

Thanks!
January 04, 2022
On Tuesday, 4 January 2022 at 18:45:37 UTC, Ben Jones wrote:
> On Tuesday, 4 January 2022 at 18:37:25 UTC, Adam D Ruppe wrote:
>> On Tuesday, 4 January 2022 at 18:13:56 UTC, Ben Jones wrote:
>>> clang -c -o source/assignment1.o source/assignment1.c
>>
>> you might have better luck just telling clang to link it too
>>
>> like
>>
>> clang source/assignment1.o -lphobos2 build/*.o # etc
>>
>> since there's a bunch of default search paths and libs etc the compiler pass to the linker.
>>
>> otherwise if you do need to use the linker directly worth remembering the order of args matter too. you need to put the more derived dependencies first followed by more general ones at the end
>
> Using clang seems to work better.  It sounds like Apple basically hides system libraries since Big Sur, and I guess their clang knows the magic flags to pass to find them.
>
> Thanks!

All good, except now simpledisplay is segfaulting on XDisplayConnection.get again (you helped me before by specifying a DISPLAY environment variable, but that's not working now, with either :0 or the longer value set by xquartz when opening an xterm).  I'm not sure how it can segfault since there are null checks in that method... maybe I should just head to the linux lab and stop fighting Apple.


January 04, 2022
On Tuesday, 4 January 2022 at 19:10:25 UTC, Ben Jones wrote:
> All good, except now simpledisplay is segfaulting on XDisplayConnection.get again

run it in the debugger; do a -g build and run it in gdb or lldb and do check the exact line it is on.

could be that the Xlib things didn't dynamically load. I thought I tested that but could have missed a spot and Apple has a bad habit of movign things so who knows.
January 04, 2022

On Tuesday, 4 January 2022 at 19:14:04 UTC, Adam D Ruppe wrote:

>

On Tuesday, 4 January 2022 at 19:10:25 UTC, Ben Jones wrote:

>

All good, except now simpledisplay is segfaulting on XDisplayConnection.get again

run it in the debugger; do a -g build and run it in gdb or lldb and do check the exact line it is on.

could be that the Xlib things didn't dynamically load. I thought I tested that but could have missed a spot and Apple has a bad habit of movign things so who knows.

Crashes on display = XOpenDisplay(displayName); :

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x0000000000000000
    frame #1: 0x000000010005180d gameboy`_D4arsd13simpledisplay18XDisplayConnection3getFZPSQBwQBu7Display at simpledisplay.d:12572
    frame #2: 0x00000001000470e0 gameboy`_D4arsd13simpledisplay5Image4impl11createImageMFiibbZv(this=0x0000000101200000, enableAlpha=false, forcexshm=false, height=144, width=160) at simpledisplay.d:12678
    frame #3: 0x0000000100046bcc gameboy`_D4arsd13simpledisplay5Image6__ctorMFiibbZCQBpQBnQBb(this=0x0000000101200000, enableAlpha=false, forcexshm=false, height=144, width=160) at simpledisplay.d:7065

Which makes lots of sense because: "On a POSIX-conformant system, if the display_name is NULL, it defaults to the value of the DISPLAY environment variable." (https://www.x.org/releases/X11R7.5/doc/man/man3/XOpenDisplay.3.html) (/sarcasm)

Explicitly calling setDisplayName before I try to use the display didn't make a difference, so maybe it's assignment to display that's causing the segfault? Not sure why that would cause a segfault either

January 04, 2022

On Tuesday, 4 January 2022 at 18:13:56 UTC, Ben Jones wrote:

>

be using don't have a D compiler installed (they're Fedora machines, and I didn't see a dmd package in their repos, or I would have asked the admins to install it).

Just for the record, while the official Fedora repos don't have dmd, they have both ldc and gdc.

January 04, 2022

On Tuesday, 4 January 2022 at 20:28:00 UTC, Ben Jones wrote:

>

On Tuesday, 4 January 2022 at 19:14:04 UTC, Adam D Ruppe wrote:

>

[...]

Crashes on display = XOpenDisplay(displayName); :

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x0000000000000000
    frame #1: 0x000000010005180d gameboy`_D4arsd13simpledisplay18XDisplayConnection3getFZPSQBwQBu7Display at simpledisplay.d:12572
    frame #2: 0x00000001000470e0 gameboy`_D4arsd13simpledisplay5Image4impl11createImageMFiibbZv(this=0x0000000101200000, enableAlpha=false, forcexshm=false, height=144, width=160) at simpledisplay.d:12678
    frame #3: 0x0000000100046bcc gameboy`_D4arsd13simpledisplay5Image6__ctorMFiibbZCQBpQBnQBb(this=0x0000000101200000, enableAlpha=false, forcexshm=false, height=144, width=160) at simpledisplay.d:7065

[...]

Hmm, I get the same segfault on linux, I'm linking with these flags and I the only warnings I get are about unwinding offsets:

clang build/*.o -L. -lphobos2 -lX11 -ldl -lpthread -o build/gameboy

« First   ‹ Prev
1 2