Skip to content

remove -L/ and -l: from linker flags #4719

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

spitulax
Copy link

This is the actual fix to NixOS/nixpkgs#338972 and many other issues with linking on Nix build system. The issue may have been caused by using -l: to specify the path to libraries and -L/ to set the linker search path. The linker could not find the libraries only when building using Nix stdenv. Building inside a nix shell does not trigger this issue. Probably it has something to do with build sandboxing, but I'm not too knowledgeable on this matter.

I found the solution when I was trying to isolate the issue using a simple C program and a static library. Building this with nix stdenv, resulting in the linker not being able to found the library.

buildPhase = ''
    gcc -o main main.c -L/ -l:$(pwd)/lib/libh.a
'';
/nix/store/j7p46r8v9gcpbxx89pbqlh61zhd33gzv-binutils-2.43.1/bin/ld: cannot find -l:/build/source/lib/libh.a: No such file or directory

But removing -L/ and -l: does the trick.

buildPhase = ''
    gcc -o main main.c $(pwd)/lib/libh.a
'';

I also tested this patch on Debian and it seemed to work fine.

@gingerBill
Copy link
Member

Wouldn't it better to disable them just for Nix?

@flysand7
Copy link
Contributor

flysand7 commented Apr 8, 2025

@gingerBill There are some differences between specifying -l: and -L and not doing that, but yes, these should be disabled just for Nix, if we want to disable them.

I also believe that we need to update the way these flags are handled in the presence of

  • foreign import "lib"; and
  • foreign import "system:lib"

Since system: explicitly asks for it to be a system dependency. Currently system: directive is only used to special-case some common libraries -- libc, m and thread, which I think needs to be fixed in the future at some point.

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

Successfully merging this pull request may close these issues.

3 participants