一步步

一步步

无法在 x64 debian 上使用 gcc:

$ gcc a.cpp
Assembler messages:
Fatal error: No compiled in support for x86_64

我应该怎么办?有些人建议安装 binutils,但它们已经在那里了。

(添加 -m64 不会改变任何内容)。这个消息从哪里来?

a.cpp 只是一个空文件。

--help=目标给出

The following options are target specific:
  -m128bit-long-double        sizeof(long double) is 16
  -m32                        Generate 32bit i386 code
  -m3dnow                     Support 3DNow! built-in functions
  -m3dnowa                    Support Athlon 3Dnow! built-in functions
  -m64                        Generate 64bit x86-64 code
  -m80387                     Use hardware fp
  -m8bit-idiv                 Expand 32bit/64bit integer divide into 8bit
                              unsigned integer divide with run-time check
  -m96bit-long-double         sizeof(long double) is 12
  -mabi=                      Generate code that conforms to the given ABI
  -mabm                       Support code generation of Advanced Bit
                              Manipulation (ABM) instructions.
  -maccumulate-outgoing-args  Reserve space for outgoing arguments in the
                              function prologue
  -maes                       Support AES built-in functions and code generation
  -malign-double              Align some doubles on dword boundary
  -malign-functions=          Function starts are aligned to this power of 2
  -malign-jumps=              Jump targets are aligned to this power of 2
  -malign-loops=              Loop code aligned to this power of 2
  -malign-stringops           Align destination of the string operations
  -mandroid                   Generate code for the Android platform.
  -march=                     Generate code for given CPU
  -masm=                      Use given assembler dialect
  -mavx                       Support MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1,
                              SSE4.2 and AVX built-in functions and code
                              generation
  -mavx2                      Support MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1,
                              SSE4.2, AVX and AVX2 built-in functions and code
                              generation
  -mavx256-split-unaligned-load Split 32-byte AVX unaligned load
  -mavx256-split-unaligned-store Split 32-byte AVX unaligned store
  -mbionic                    Use Bionic C library
  -mbmi                       Support BMI built-in functions and code generation
  -mbmi2                      Support BMI2 built-in functions and code
                              generation
  -mbranch-cost=              Branches are this expensive (1-5, arbitrary units)
  -mcld                       Generate cld instruction in the function prologue.
  -mcmodel=                   Use given x86-64 code model
  -mcpu=                      This switch lacks documentation
  -mcrc32                     Support code generation of crc32 instruction.
  -mcx16                      Support code generation of cmpxchg16b instruction.
  -mdispatch-scheduler        Do dispatch scheduling if processor is bdver1 or
                              bdver2 and Haifa scheduling is selected.
  -mf16c                      Support F16C built-in functions and code
                              generation
  -mfancy-math-387            Generate sin, cos, sqrt for FPU
  -mfentry                    Emit profiling counter call at function entry
                              before prologue.
  -mfma                       Support MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1,
                              SSE4.2, AVX and FMA built-in functions and code
                              generation
  -mfma4                      Support FMA4 built-in functions and code
                              generation 
  -mforce-drap                Always use Dynamic Realigned Argument Pointer
                              (DRAP) to realign stack
  -mfp-ret-in-387             Return values of functions in FPU registers
  -mfpmath=                   Generate floating point mathematics using given
                              instruction set
  -mfsgsbase                  Support FSGSBASE built-in functions and code
                              generation
  -mfused-madd                This switch lacks documentation
  -mglibc                     Use GNU C library
  -mhard-float                Use hardware fp
  -mieee-fp                   Use IEEE math for fp comparisons
  -mincoming-stack-boundary=  Assume incoming stack aligned to this power of 2
  -minline-all-stringops      Inline all known string operations
  -minline-stringops-dynamically Inline memset/memcpy string operations, but
                              perform inline version only for small blocks
  -mintel-syntax              This switch lacks documentation
  -mlarge-data-threshold=     Data greater than given threshold will go into
                              .ldata section in x86-64 medium model
  -mlwp                       Support LWP built-in functions and code
                              generation 
  -mlzcnt                     Support LZCNT built-in function and code
                              generation
  -mmmx                       Support MMX built-in functions
  -mmovbe                     Support code generation of movbe instruction.
  -mms-bitfields              Use native (MS) bitfield layout
  -mno-align-stringops        This switch lacks documentation
  -mno-fancy-math-387         This switch lacks documentation
  -mno-push-args              This switch lacks documentation
  -mno-red-zone               This switch lacks documentation
  -mno-sse4                   Do not support SSE4.1 and SSE4.2 built-in
                              functions and code generation
  -momit-leaf-frame-pointer   Omit the frame pointer in leaf functions
  -mpc32                      Set 80387 floating-point precision to 32-bit
  -mpc64                      Set 80387 floating-point precision to 64-bit
  -mpc80                      Set 80387 floating-point precision to 80-bit
  -mpclmul                    Support PCLMUL built-in functions and code
                              generation
  -mpopcnt                    Support code generation of popcnt instruction.
  -mprefer-avx128             Use 128-bit AVX instructions instead of 256-bit
                              AVX instructions in the auto-vectorizer.
  -mpreferred-stack-boundary= Attempt to keep stack aligned to this power of 2
  -mpush-args                 Use push instructions to save outgoing arguments
  -mrdrnd                     Support RDRND built-in functions and code
                              generation
  -mrecip                     Generate reciprocals instead of divss and sqrtss.
  -mrecip=                    Control generation of reciprocal estimates.
  -mred-zone                  Use red-zone in the x86-64 code
  -mregparm=                  Number of registers used to pass integer arguments
  -mrtd                       Alternate calling convention
  -msahf                      Support code generation of sahf instruction in
                              64bit x86-64 code.
  -msoft-float                Do not use hardware fp
  -msse                       Support MMX and SSE built-in functions and code
                              generation
  -msse2                      Support MMX, SSE and SSE2 built-in functions and
                              code generation
  -msse2avx                   Encode SSE instructions with VEX prefix
  -msse3                      Support MMX, SSE, SSE2 and SSE3 built-in
                              functions and code generation
  -msse4                      Support MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1 and
                              SSE4.2 built-in functions and code generation
  -msse4.1                    Support MMX, SSE, SSE2, SSE3, SSSE3 and SSE4.1
                              built-in functions and code generation
  -msse4.2                    Support MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1 and
                              SSE4.2 built-in functions and code generation
  -msse4a                     Support MMX, SSE, SSE2, SSE3 and SSE4A built-in
                              functions and code generation
  -msse5                      This switch lacks documentation
  -msseregparm                Use SSE register passing conventions for SF and
                              DF mode
  -mssse3                     Support MMX, SSE, SSE2, SSE3 and SSSE3 built-in
                              functions and code generation
  -mstack-arg-probe           Enable stack probing
  -mstackrealign              Realign stack in prologue
  -mstringop-strategy=        Chose strategy to generate stringop using
  -mtbm                       Support TBM built-in functions and code generation
  -mtls-dialect=              Use given thread-local storage dialect
  -mtls-direct-seg-refs       Use direct references against %gs when accessing
                              tls data
  -mtune=                     Schedule code for given CPU
  -muclibc                    Use uClibc C library
  -mveclibabi=                Vector library ABI to use
  -mvect8-ret-in-mem          Return 8-byte vectors in memory
  -mvzeroupper                Generate vzeroupper instruction before a transfer
                              of control flow out of the function.
  -mx32                       Generate 32bit x86-64 code
  -mxop                       Support XOP built-in functions and code
                              generation 

  Known assembler dialects (for use with the -masm-dialect= option):
    att intel

  Known ABIs (for use with the -mabi= option):
    ms sysv

  Known code models (for use with the -mcmodel= option):
    32 kernel large medium small

  Valid arguments to -mfpmath=:
    387 387+sse 387,sse both sse sse+387 sse,387

  Known vectorization library ABIs (for use with the -mveclibabi= option):
    acml svml

  Valid arguments to -mstringop-strategy=:
    byte_loop libcall loop rep_4byte rep_8byte rep_byte unrolled_loop

  Known TLS dialects (for use with the -mtls-dialect= option):
    gnu gnu2

答案1

除非涉及某些神圣实体,否则您将无法编译空文件。这就像要求面包师凭空制作面包一样。

ac“hello world”可以编译吗? IE:

#include <stdio.h>

int main (void)
{
        printf ("Hello ya!\n");
        return 0;
}

还尝试使用-v选项进行编译。


编辑:来自 64 位的新数据:

即:我在32 位64 位机器上的上述代码的输出结果是:

$ gcc -Wall -Wextra -pedantic -v -o hello hello.c
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8) 
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-pedantic' '-v' '-o' 'hello' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.4.5/cc1 -quiet -v hello.c -quiet -dumpbase hello.c -mtune=generic -auxbase hello -Wall -Wextra -pedantic -version -o /tmp/cclZGw1d.s
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/usr/include/x86_64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.4.5/include
 /usr/lib/gcc/x86_64-linux-gnu/4.4.5/include-fixed
 /usr/include
End of search list.
GNU C (Debian 4.4.5-8) version 4.4.5 (x86_64-linux-gnu)
    compiled by GNU C version 4.4.5, GMP version 4.3.2, MPFR version 3.0.0-p3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: dac4d891d068d1bed01868869b00bd17
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-pedantic' '-v' '-o' 'hello' '-mtune=generic'
 as -V -Qy -o /tmp/ccx0akTi.o /tmp/cclZGw1d.s
GNU assembler version 2.20.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Debian) 2.20.1-system.20100303
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-pedantic' '-v' '-o' 'hello' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.4.5/collect2 --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../.. /tmp/ccx0akTi.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crtn.o

一步步

对于一步一步(和更清洁的方法),请执行 - (这就是 gcc 所做的):需要时在括号中添加“Clean”命令。

第 1 步 - 展开宏:

$ cpp hello.c > hello.i
# This gives source code with all macros expanded.

用于-save-temps自动生成。应该给.iic++。

步骤 2 - 在 hello.s 中创建汇编代码 (gcc -S hello.i):

$ gcc -Wall -Wextra -pedantic -S -v hello.i
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8) 
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-pedantic' '-S' '-v' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.4.5/cc1 -fpreprocessed hello.i -quiet -dumpbase hello.i -mtune=generic -auxbase hello -Wall -Wextra -pedantic -version -o hello.s
GNU C (Debian 4.4.5-8) version 4.4.5 (x86_64-linux-gnu)
    compiled by GNU C version 4.4.5, GMP version 4.3.2, MPFR version 3.0.0-p3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: dac4d891d068d1bed01868869b00bd17
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-Wextra' '-pedantic' '-S' '-v' '-mtune=generic'

步骤 3 - 将汇编代码转换为机器代码。 (如 hello.s -o hello.o)

看看“man as”有很多选项。

$ as hello.s -ag -al -o hello.o
GAS LISTING hello.s             page 1

 GNU assembler version 2.20.1 (x86_64-linux-gnu)
     using BFD version (GNU Binutils for Debian) 2.20.1-system.20100303.
 options passed : -ag -al 
 input file     : hello.s
 output file    : hello.o
 target         : x86_64-pc-linux-gnu
 time stamp     : 2012-12-09T00:25:03.000+0100


GAS LISTING hello.s             page 2


   1                    .file   "hello.c"
   2                    .section    .rodata
   3                .LC0:
   4 0000 48656C6C      .string "Hello ya!"
   4      6F207961 
   4      2100
   5                    .text
   6                .globl main
   7                    .type   main, @function
   8                main:
   9                .LFB0:
  10                    .cfi_startproc
  11 0000 55            pushq   %rbp
  12                    .cfi_def_cfa_offset 16
  13 0001 4889E5        movq    %rsp, %rbp
  14                    .cfi_offset 6, -16
  15                    .cfi_def_cfa_register 6
  16 0004 B8000000      movl    $.LC0, %eax
  16      00
  17 0009 4889C7        movq    %rax, %rdi
  18 000c B8000000      movl    $0, %eax
  18      00
  19 0011 E8000000      call    printf
  19      00
  20 0016 B8000000      movl    $0, %eax
  20      00
  21 001b C9            leave
  22 001c C3            ret
  23                    .cfi_endproc
  24                .LFE0:
  25                    .size   main, .-main
  26                    .ident  "GCC: (Debian 4.4.5-8) 4.4.5"
  27                    .section    .note.GNU-stack,"",@progbits

第 4 步 - 链接:

# Highly dependable on arch and libs and etc.
# For me it is:
$ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.5 hello.o -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc/x86_64-linux-gnu/4.4.5/crtend.o /usr/lib/crtn.o

# Shorter option:
$ gcc hello.o
# Using -v option you should get some more information.

第 5 步 - 运行:

$ ./a.out 
Hello ya!

# Optionally run $ strace ./a.out

到哪一步失败了?


作为注释,我的输出来自 as -V --target-help

$ as -V --target-help
GNU assembler version 2.20.1 (x86_64-linux-gnu) using BFD version 
(GNU Binutils for Debian) 2.20.1-system.20100303
  -Q                      ignored
  -V                      print assembler version number
  -k                      ignored
  -n                      Do not optimize code alignment
  -q                      quieten some warnings
  -s                      ignored
  --32/--64               generate 32bit/64bit code
  --divide                ignored
  -march=CPU[,+EXTENSION...]
                          generate code for CPU and EXTENSION, CPU is one of:
                           i8086, i186, i286, i386, i486, pentium, pentiumpro,
                           pentiumii, pentiumiii, pentium4, prescott, nocona,
                           core, core2, corei7, l1om, k6, k6_2, athlon, k8,
                           amdfam10, generic32, generic64
                          EXTENSION is combination of:
                           8087, 287, 387, no87, mmx, nommx, sse, sse2, sse3,
                           ssse3, sse4.1, sse4.2, sse4, nosse, avx, noavx,
                           vmx, smx, xsave, movbe, ept, aes, pclmul, fma,
                           clflush, syscall, rdtscp, 3dnow, 3dnowa, sse4a,
                           svme, abm, padlock, fma4
  -mtune=CPU              optimize for CPU, CPU is one of:
                           i8086, i186, i286, i386, i486, pentium, pentiumpro,
                           pentiumii, pentiumiii, pentium4, prescott, nocona,
                           core, core2, corei7, l1om, k6, k6_2, athlon, k8,
                           amdfam10, generic32, generic64
  -msse2avx               encode SSE instructions with VEX prefix
  -msse-check=[none|error|warning]
                          check SSE instructions
  -mmnemonic=[att|intel]  use AT&T/Intel mnemonic
  -msyntax=[att|intel]    use AT&T/Intel syntax
  -mindex-reg             support pseudo index registers
  -mnaked-reg             don't require `%' prefix for registers
  -mold-gcc               support old (<= 2.8.1) versions of gcc

相关内容