我即将能够在Windows上交叉编译Linux的二进制文件。我得到了一个命令,可以将我的代码编译成.o文件,但我无法让它链接到生成二进制文件。现在它说它不能链接到Linux库的几个副本,即使我在我的系统上有它们的副本,并且已经把目录给了链接器。
我使用的命令:clang -fuse-ld=lld -target x86_64-pc-linux-gnu -LD:/Toolchains/Linux/Libs -o main main.o -v
我得到的错误消息是:
InstalledDir: C:\Program Files\LLVM\bin
"C:\\Program Files\\LLVM\\bin\\ld.lld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main crt1.o crti.o crtbegin.o -LD:/Toolchains/Linux/Libs "-LC:\\Program Files\\LLVM\\bin/../lib" main.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o crtn.o
ld.lld: error: cannot open crt1.o: no such file or directory
ld.lld: error: cannot open crti.o: no such file or directory
ld.lld: error: cannot open crtbegin.o: no such file or directory
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: cannot open crtend.o: no such file or directory
ld.lld: error: cannot open crtn.o: no such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
所有的cannot open *.o: no such file or directory
错误都是我在我的Windows机上备份的文件,所以我不确定从这里到哪里去让链接器识别我的文件。相反,让链接器识别和使用Windows库相对简单
任何帮助我们都将不胜感激。
发布于 2021-05-10 10:11:59
我相信clang正在为您正在编译的环境搜索gcc C运行时。您应该能够使用--gcc-toolchain
标志为其设置搜索路径。
例如,在我的机器上运行以下命令(在Powershell中):
clang --gcc-toolchain=$TOOLCHAIN --sysroot=$TOOLCHAIN\x86_64-linux-gnu\sysroot -fuse-ld=lld -target x86_64-linux-gnu -o main main.c
$TOOLCHAIN
是我从here下载的Linux跨工具链的位置。
在WSL2下运行生成的./main
程序对我来说很有效。
要编译C++程序,请使用clang++
而不是clang
。其他一切都是一样的:
clang++ --gcc-toolchain=$TOOLCHAIN --sysroot=$TOOLCHAIN\x86_64-linux-gnu\sysroot -fuse-ld=lld -target x86_64-linux-gnu -o main main.cpp
https://stackoverflow.com/questions/67461415
复制相似问题