Skip to content

extra-libraries: link on Linux - #31

Merged
dmjio merged 1 commit into
mainfrom
extra-libraries-link-order
Oct 23, 2026
Merged

dmjio merged 1 commit into
mainfrom
extra-libraries-link-order

Conversation

@dmjio

@dmjio dmjio commented Oct 23, 2026

Copy link
Copy Markdown
Member

The bug: extra-libraries: [z] produced "undefined reference" on Linux. miso passes each library to mhs as -optl -lNAME, and mhs puts those flags before the runtime and the program's C file. GNU ld with --as-needed (the default on Ubuntu and Debian) drops a library that nothing before it needs.

The fix: on Linux, for native programs with extra-libraries, miso passes -Wl,--no-as-needed before them. macOS's linker and emcc (wasm-ld) resolve libraries in any order, so nothing changes there; macOS's linker doesn't accept the flag anyway.

Checked with mhs and zlib on Ubuntu 24.04:

  • with -optl -lz, the link failed;
  • with -optl -Wl,--no-as-needed first, it linked and printed zlib's version.

New smoke test case: a program with extra-libraries: [z] that prints zlib's version, wherever the C compiler has zlib.

Note: this and #30 both add a case at the end of scripts/smoke-test.sh. Whichever merges second will need a one-line conflict resolved.


Generated by Claude Code

miso passes each library to mhs as -optl -lNAME, and mhs puts those flags
before the runtime and the program's C file.  GNU ld with --as-needed (the
default on Ubuntu and Debian) drops a library that nothing before it
needs, so a program using one failed with "undefined reference".

On Linux, for native programs with extra-libraries, miso now passes
-Wl,--no-as-needed before them.  macOS's linker resolves libraries in any
order (and does not take the flag); emcc (wasm-ld) does too.

Checked with mhs and zlib on Ubuntu 24.04: with -optl -lz the link
failed; with -optl -Wl,--no-as-needed first it linked and printed zlib's
version.  The smoke test now builds and runs a program with
extra-libraries: [z] where the C compiler has zlib.
@dmjio
dmjio force-pushed the extra-libraries-link-order branch from f86d652 to 82befc9 Compare October 23, 2026 02:12
@dmjio
dmjio merged commit f9a8ce5 into main Oct 23, 2026
8 checks passed
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.

1 participant