Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking error related to libm when using Nim #339

Closed
ajusa opened this issue Dec 22, 2021 · 5 comments
Closed

Linking error related to libm when using Nim #339

ajusa opened this issue Dec 22, 2021 · 5 comments

Comments

@ajusa
Copy link
Contributor

ajusa commented Dec 22, 2021

Hello!
I've been trying to get some more complex projects to build, written in the Nim programming language. Nim outputs C code, so this should be feasible. I started working off of https://github.com/Yardanico/cosmonim which has some sane defaults and patches for the standard library. I've also gone ahead and made additional patches for the standard library, so I'm finally at a point where I'm getting linking errors rather than compiling errors.

/usr/bin/ld.bfd: errno: TLS reference in /usr/lib64/libm-2.34.a(w_pow.o) mismatches non-TLS reference in /home/ajusa/.cache/nim/main_r/stdlib_io.nim.c.o
/usr/bin/ld.bfd: /usr/lib64/libm-2.34.a: error adding symbols: bad value
collect2: error: ld returned 1 exit status

I've tried looking at some of the third party programs that folks have gotten working under cosmopolitan, but wasn't able to glean any information about this issue. I'm not super familiar or experienced with the linking side of things, so I'd appreciate any help!

@ahgamut
Copy link
Collaborator

ahgamut commented Dec 23, 2021

Hi @ajusa,

When porting Lua (#61) I saw a similar error, I skipped linking libm because it relied on some symbols from the system libc.

The math functions in cosmopolitan.a (from libc/tinymath) have been enough for porting Lua and Python.

Do you get the same linker error(s) if you skip linking with libm-2.34.a and just use cosmopolitan.a?

@ahgamut
Copy link
Collaborator

ahgamut commented Dec 23, 2021

You might find it useful to look at the Makefile I used to build Lua.

@ajusa
Copy link
Contributor Author

ajusa commented Dec 23, 2021

Awesome, thank you for the guidance. As it turns out, Nim is automatically appending -lm as a part of its process. I tried running the linker manually by hand (removing -lm) and that seems to have fixed that error!

Next, I got an error relating to stdlib_terminal.nim.c:(.text+0x1e9): undefined reference to ctermid'`. It seems like there's no implementation of that in cosmopolitan? I just went ahead and commented it out of Nim itself (defaulting to 80, as it's used to calculate the width of the terminal).

Do you know a good alternative to ctermid here, or am I mistaken in that it may not be possible to be cross platform? Again, thanks for the help. I've very happy to have this compiling 😄

@ahgamut
Copy link
Collaborator

ahgamut commented Dec 24, 2021

If ctermidis the only missing function, you can try adding a stub (I did this for sinh and friends when compiling Lua).

Create ctermid.c like below and add it to the build process:

// ctermid.c

char* ctermid(char *s)
{
static char buf[] = "/dev/tty";
return buf;
}

Reference: the man page of ctermid

@ajusa
Copy link
Contributor Author

ajusa commented Dec 25, 2021

Thanks! I've got it working and compiling nicely now, I'll probably upstream my efforts to https://github.com/Yardanico/cosmonim.

@ajusa ajusa closed this as completed Dec 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants