我正在尝试让我的工作 C/C++ 代码在 Ubuntu 上使用 GCC/G++ 进行编译 - 除了一个奇怪的错误以及与 MinGW GCC 端口、Intel 和 MSC 编译器的一些非常小的差异之外,它编译和链接正常。
尽管它链接时没有警告并生成默认的 a.exe,但生成的可执行文件拒绝从 bash 命令行运行。我猜这是我的一些配置错误。 GCC 和 G++ 都是以其开箱即用的默认值安装的。下面是最终的链接和执行步骤以及 gcc -v 的结果。
echo "Start link"
Start link
gcc KeplerSolver.cpp -o KeplerSolver -O3 -Ofast -lm benchmark.o BM_shims.o BM_starter.o
classic.o compilertests.o cuberoot.o deltas.o intel.o MyTrig.o opt_starter.o quartic.o
PadeApprox.o solver.o starter.o stdafx.o tests.o verify.o
martin@martin-virtual-machine:~/Documents/ks$ ./a.exe
bash: ./a.exe: cannot execute binary file: Exec format error
martin@martin-virtual-machine:~/Documents/ks$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --
with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-
languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-
version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --
enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-
threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --
enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --
enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --
with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --
enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic -
-enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-
nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-
cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu -
-target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-
serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
我似乎在所有合适的地方都有图书馆。在 Windows 下使用 MinGW 使用 GCC 可以构建相同的代码(只是一些关键浮点代码是硬连线的 x87)。有人能发现我在这里做错了什么吗?
-o
选项表示输出。您正在编译并将可执行文件保存到 KeplerSolver
,而不是 a.exe
gcc KeplerSolver.cpp -o KeplerSolver ...
你需要运行
./KeplerSolver