且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与dylib ld的静态链接:找不到-lcrt0.o的库collect2:错误:ld返回1退出状态

更新时间:2023-10-18 19:43:52

GCC's -static option, which you are applying, is non-positional. It enforces static linkage of all libraries. Your linkage then fails because your system has no static version of libcrt0.o

您可能将GCC的static选项与 ld-static选项(同义词:-Bstatic-dn -non_shared), 位置.它仅影响命令行上的后续库.这是逆的 链接器的-Bdynamic选项(同义词:-dy-call_shared).

You may be confusing GCC's static option with ld's -static option (synonyms: -Bstatic, -dn -non_shared), which is positional. It affects only subsequent libraries on the commandline. It is the inverse of the linker's -Bdynamic option (synonyms: -dy, -call_shared).

因此,仅通过GCC静态链接库-lfoo-lbar ...,您可以将-Bstatic传递给 链接器(在您提及它们之前)和-Bdynamic在它们之后:

So to link only libraries -lfoo, -lbar... statically, via GCC, you can pass -Bstatic through to the linker just before you mention them and -Bdynamic just after them:

-Wl,-Bstatic -lfoo -lbar -Wl,-Bdynamic

即使-lbar是您的最后一个库,也不要省略最后一个-Wl,-Bdynamic,因为GCC会悄悄地附加标准​​库 到您的链接(如您所见).

Do not omit the final -Wl,-Bdynamic, even if -lbar is the last of your libraries, because GCC quietly appends standard libraries to your linkage (as you have found).