我尝试在 cygwin 上使用 clang 编译 C++ 文件,但它生成的可执行文件无法运行。我从 cygwin 收到运行时失败:
Cygwin runtime failure: a.exe: Invalid relocation. Offset 0x2fd4bce73 at address 0x1004010e9 doesn't fit into 32 bits
示例代码-proba.cpp:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello, World!";
return 0;
}
编译为:
clang++ proba.cpp
编译完成,没有任何错误或警告。
clang 是从源代码编译的。我尝试了最新版本 (8.0.0) 以及 git 版本 (2019.03.26)。运行其输出时,两者均产生相同的错误。
clang 安装过程如下:
git clone https://github.com/llvm/llvm-project.git
mkdir build
cd build
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;compiler-rt;lld;" -DLLVM_TARGETS_TO_BUILD=X86 ../llvm/
make -j4
make install
cygwin 还有一个 clang 包(版本 5.0.1),可将 proba.cpp 编译为可执行文件。不使用它的原因是它不编译 SystemC 库(https://www.accellera.org/downloads/standards/systemc) 正确。在“make check”期间,只有少数测试失败,而使用 gcc (7.4.0) 进行编译时,所有测试均通过。
我还注意到,从源代码编译的 clang 驱动程序使用 g++ 作为链接器,而 cygwin 包附带的驱动程序使用 ld。我也没有办法强制 clang 9 使用 lld 而不是 g++。(-fuse-ld=lld 无法被 g++ 识别)
clang 附带 cygwin:
$ clang++ -v proba.cpp -o a_clang5.exe
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/bin
"/usr/bin/clang-5.0" -cc1 -triple x86_64-unknown-windows-cygnus -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name proba.cpp -mre
location-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -v -dwarf-co
lumn-info -debugger-tuning=gdb -resource-dir /usr/lib/clang/5.0.1 -internal-isystem /usr/x86_64-pc-cygwin/include/c++ -internal-isystem /usr/x86_64-pc-cygwin/include/c++/x86_6
4-pc-cygwin -internal-isystem /usr/x86_64-pc-cygwin/include/c++/backward -internal-isystem /usr/x86_64-pc-cygwin/include/c++/7.4.0 -internal-isystem /usr/x86_64-pc-cygwin/incl
ude/c++/7.4.0/x86_64-pc-cygwin -internal-isystem /usr/x86_64-pc-cygwin/include/c++/7.4.0/backward -internal-isystem /usr/include/c++/7.4.0 -internal-isystem /usr/include/c++/7
.4.0/x86_64-pc-cygwin -internal-isystem /usr/include/c++/7.4.0/backward -internal-isystem /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++ -internal-isystem /usr/lib/gcc/x86_64
-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin -internal-isystem /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward -internal-isystem /usr/local/include -internal-isystem
/usr/lib/clang/5.0.1/include -internal-isystem /usr/x86_64-pc-cygwin/include -internal-isystem /usr/include -internal-isystem /usr/include/w32api -fdeprecated-macro -fdebug-c
ompilation-dir /cygdrive/c/home/peter.gerst/tmp/clang_proba -ferror-limit 19 -fmessage-length 175 -femulated-tls -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics
-show-option -fcolor-diagnostics -o /tmp/proba-e855cb.o -x c++ proba.cpp
clang -cc1 version 5.0.1 based upon LLVM 5.0.1 default target x86_64-unknown-windows-cygnus
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include/c++"
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include/c++/x86_64-pc-cygwin"
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include/c++/backward"
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include/c++/7.4.0"
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include/c++/7.4.0/x86_64-pc-cygwin"
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include/c++/7.4.0/backward"
ignoring nonexistent directory "/usr/include/c++/7.4.0"
ignoring nonexistent directory "/usr/include/c++/7.4.0/x86_64-pc-cygwin"
ignoring nonexistent directory "/usr/include/c++/7.4.0/backward"
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/x86_64-pc-cygwin/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward
/usr/lib/clang/5.0.1/include
/usr/include
/usr/include/w32api
End of search list.
"/usr/bin/ld" -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPv --wrap _ZdaPv --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPv
KSt9nothrow_t -Bdynamic --tsaware -o a_clang5.exe /usr/lib/crt0.o /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/crtbegin.o -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0 -L/usr/x86_64-pc-cygwin/
lib -L/usr/lib /tmp/proba-e855cb.o -lstdc++ -liconv -lgcc_s -lgcc -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc -lcygwin /usr/lib/default-manifest.o /usr/lib
/gcc/x86_64-pc-cygwin/7.4.0/crtend.o
clang 9 从以下来源编译而来:
$ clang++ -v proba.cpp -o a.exe
clang version 9.0.0 (https://github.com/llvm/llvm-project.git e24441aab03e393d591d9252612e8c0ac557b1c3)
Target: x86_64-unknown-windows-cygnus
Thread model: posix
InstalledDir: /usr/local/bin
"/usr/local/bin/clang-9" -cc1 -triple x86_64-unknown-windows-cygnus -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name proba.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir /usr/local/lib/clang/9.0.0 -fdeprecated-macro -fdebug-compilation-dir /cygdrive/c/home/peter.gerst/tmp/clang_proba -ferror-limit 19 -fmessage-length 135 -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/proba-b6bf15.o -x c++ proba.cpp -faddrsig
clang -cc1 version 9.0.0 based upon LLVM 9.0.0svn default target x86_64-unknown-cygwin
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/x86_64-pc-cygwin
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/backward
/usr/local/include
/usr/local/lib/clang/9.0.0/include
/usr/include/w32api
/usr/include
End of search list.
"/usr/bin/g++" -v -m64 -o a.exe /tmp/proba-b6bf15.o
Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-7.4.0-1.x86_64/src/gcc-7.4.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libcilkrts --enable-libgomp --enable-libitm --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 7.4.0 (GCC)
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/lib/../lib/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/lib/:/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-m64' '-o' 'a.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/collect2.exe -plugin /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/cyglto_plugin.dll -plugin-opt=/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/lto-wrapper.exe -plugin-opt=-fresolution=/tmp/ccM89KcB.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lcygwin -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id -m i386pep --wrap _Znwm --wrap _Znam --wrap _ZdlPv --wrap _ZdaPv --wrap _ZnwmRKSt9nothrow_t --wrap _ZnamRKSt9nothrow_t --wrap _ZdlPvRKSt9nothrow_t --wrap _ZdaPvRKSt9nothrow_t -Bdynamic --dll-search-prefix=cyg --tsaware -o a.exe /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../lib/crt0.o /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/crtbegin.o -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0 -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/lib/../lib -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/lib -L/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../.. /tmp/proba-b6bf15.o -lstdc++ -lgcc_s -lgcc -lcygwin -ladvapi32 -lshell32 -luser32 -lkernel32 -lgcc_s -lgcc /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../lib/default-manifest.o /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/crtend.o
COLLECT_GCC_OPTIONS='-v' '-m64' '-o' 'a.exe' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
使用 clang 9 编译和执行 proba.cpp 的 C 等效代码可以正常工作。我尝试了文档建议的测试(https://clang.llvm.org/get_started.html):
clang --help
clang file.c -fsyntax-only
clang file.c -S -emit-llvm -o -
clang file.c -S -emit-llvm -o - -O3
clang file.c -S -O3 -o -
在我看来它们工作正常。
我也尝试了以下但没有成功
clang++ -c proba.cpp -o proba.o
ld.lld proba.o -o proba.exe
ld.lld: error: proba.o: unknown file type
有人知道在 cygwin 上使用 clang 9 进行 C++ 编译可能出现什么问题吗?
有人设法在 cygwin 下将 lld 链接器与 clang 一起使用吗?
任何关于如何在 cygwin 下正确设置 clang + lld 工具链的提示都将受到赞赏。