首页 > 解决方案 > Cairo 创建 png 文件时使用 clang LLVM 编译器的非法硬件指令

问题描述

我试图执行一个 C 程序。它已经正确编译,但是当我执行输出二进制文件时,它显示:

[16]    70041 illegal hardware instruction  ./create

所以,我添加-g选项看起来像这样:

cc create.c $(pkg-config --cflags --libs cairo) -g -o create

和 C 代码:

#include <cairo.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(void){
    cairo_surface_t *surface;
    cairo_t *cr;
    int j=0;
    char seq[5];

    for(int i=0; i<=360; i+=4){
        surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 800, 600);
        cr = cairo_create(surface);
        cairo_set_line_width (cr, 6.0);
        cairo_arc( cr, 800/2, 600/2, (600/2)-50, 0, 2*M_PI );
        cairo_stroke (cr);
        cairo_set_source_rgb( cr, 0.2, 0.2, i/360 );
        cairo_arc( cr, 800/2, 600/2, (600/2)-60, 0, i*(M_PI/180.0) );
        cairo_stroke(cr);
        j++;
        if( j<10 ){ sprintf(seq, "%s%d%d", "seq", 0, j); } else { sprintf(seq, "%s%d", "seq", j); }
        cairo_surface_write_to_png(surface, seq);
        cairo_surface_destroy(surface);
    }

    cairo_destroy(cr);

    return 0;
}

上面代码的目的是创建一个pngs图片序列。但是输出没有任何警告。

我包括lldb输出:

(lldb) target create "create"
Current executable set to '/Users/rikky/Documents/C/Cairo/FirstDraw/create' (x86_64).
(lldb) l
   7    int main(void){
   8        cairo_surface_t *surface;
   9        cairo_t *cr;
   10       int j=0;
   11       char seq[5];
   12   
   13       for(int i=0; i<=360; i+=4){
   14           surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 800, 600);
   15           cr = cairo_create(surface);
   16           cairo_set_line_width (cr, 6.0);
(lldb) run
Process 70245 launched: '/Users/rikky/Documents/C/Cairo/FirstDraw/create' (x86_64)
Process 70245 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    frame #0: 0x00007fff6e80ffca libsystem_c.dylib`__chk_fail_overflow.cold.1 + 16
libsystem_c.dylib`__chk_fail_overflow.cold.1:
->  0x7fff6e80ffca <+16>: ud2    

libsystem_c.dylib`__chk_fail_overlap.cold.1:
    0x7fff6e80ffcc <+0>:  pushq  %rbp
    0x7fff6e80ffcd <+1>:  movq   %rsp, %rbp
    0x7fff6e80ffd0 <+4>:  leaq   0x6e41(%rip), %rdi        ; "detected source and destination buffer overlap"
Target 0: (create) stopped.

我没看懂EXC_I386_INVOP代码。有什么办法可以解决吗?

Clang 版本:

Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

标签: clldbcairo

解决方案


推荐阅读