Skip to content

Commit 0145e4f

Browse files
committed
fix: statically link build.mcpp to avoid missing dynamic library issues
Add -static to host_base_flags() for both Clang and GCC paths so the compiled build.mcpp binary is fully static. Remove -Wl,-rpath and -Wl,--dynamic-linker flags that are unnecessary (and counterproductive) under static linking. This prevents runtime failures when users haven't configured LD_LIBRARY_PATH for the toolchain's shared libraries (libc++, libunwind, etc.).
1 parent 96f6e2e commit 0145e4f

1 file changed

Lines changed: 6 additions & 16 deletions

File tree

src/build/build_program.cppm

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ std::vector<std::string> host_base_flags(const mcpp::toolchain::Toolchain& tc) {
177177
// pipeline regenerates the cfg deterministically anyway.
178178
if (mcpp::toolchain::is_clang(tc)) {
179179
if constexpr (!mcpp::platform::is_linux) return f;
180+
f.push_back("-static");
180181
const auto dm = mcpp::toolchain::resolve_clang_driver(tc);
181182
if (dm.hasCfg) {
182183
f.push_back("--no-default-config");
@@ -186,33 +187,25 @@ std::vector<std::string> host_base_flags(const mcpp::toolchain::Toolchain& tc) {
186187
f.push_back("-fuse-ld=lld");
187188
f.push_back("--rtlib=compiler-rt");
188189
f.push_back("--unwindlib=libunwind");
189-
for (auto& d : dm.libDirs) {
190+
for (auto& d : dm.libDirs)
190191
f.push_back("-L" + d.string());
191-
f.push_back("-Wl,-rpath," + d.string());
192-
}
193192
}
194193
if (lm.mode == mcpp::toolchain::CLibMode::Sysroot) {
195194
f.push_back("--sysroot=" + lm.sysroot.string());
196195
} else if (lm.mode == mcpp::toolchain::CLibMode::PayloadFirst) {
197196
for (auto& inc : lm.systemIncludes) f.push_back("-isystem" + inc.string());
198197
f.push_back("-B" + lm.crtDir.string()); // Scrt1.o/crti.o discovery
199-
for (auto& d : lm.libDirs) {
198+
for (auto& d : lm.libDirs)
200199
f.push_back("-L" + d.string());
201-
f.push_back("-Wl,-rpath," + d.string());
202-
}
203-
if (!lm.loader.empty())
204-
f.push_back("-Wl,--dynamic-linker=" + lm.loader.string());
205200
}
206-
// Runtime lib dirs so the produced program can load private libs in-tree.
207-
for (auto& d : tc.linkRuntimeDirs) {
201+
for (auto& d : tc.linkRuntimeDirs)
208202
f.push_back("-L" + d.string());
209-
f.push_back("-Wl,-rpath," + d.string());
210-
}
211203
return f;
212204
}
213205

214206
// GCC: a fresh sandbox g++ needs --sysroot to find the C library + the
215207
// include-fixed headers; without a sysroot, wire the glibc payload directly.
208+
f.push_back("-static");
216209
if (lm.mode == mcpp::toolchain::CLibMode::Sysroot) {
217210
f.push_back("--sysroot=" + lm.sysroot.string());
218211
} else if (lm.mode == mcpp::toolchain::CLibMode::PayloadFirst) {
@@ -227,11 +220,8 @@ std::vector<std::string> host_base_flags(const mcpp::toolchain::Toolchain& tc) {
227220
auto ar = mcpp::toolchain::archive_tool(tc);
228221
if (!ar.empty()) f.push_back("-B" + ar.parent_path().string());
229222
}
230-
// Runtime lib dirs so the produced program can load private libs in-tree.
231-
for (auto& d : tc.linkRuntimeDirs) {
223+
for (auto& d : tc.linkRuntimeDirs)
232224
f.push_back("-L" + d.string());
233-
f.push_back("-Wl,-rpath," + d.string());
234-
}
235225
return f;
236226
}
237227

0 commit comments

Comments
 (0)