用-fomit帧指针编译C -mrtd自由函数指针创建段错误 [英] Compiling c with -fomit-frame-pointer -mrtd creates segfault on free function pointer

查看:151
本文介绍了用-fomit帧指针编译C -mrtd自由函数指针创建段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个C code崩溃分段错误,如果我用-fomit帧指针和-mrtd与GCC编译。

是我的code错在某种方式?其他函数指针正常工作,而不是当它被传递自由的功能?我得到编译时警告,但我不明白为什么还是我应该怎么做来解决。 (我不经常在C code,这个错误来自于我使用3D零件库)

我需要RTD / STDCALL,因为即时通讯在Windows和需要从蟒蛇与ctypes的调用此库,以及-fomit-frame-pointer的默认情况下,用gcc编译与-O1时包括在内。 (GCC版本是从TDM / MINGW32 4.6.1),感觉有点怪,一个默认的优化选项会造成麻烦?

C- code:

 的#include<&stdlib.h中GT;//无效免费(无效* PTR);
无效测试(无效* 0,无效(* freeFunc)(无效*)){
    freeFunc(O);
}诠释主(){
    为int * p =(INT *)释放calloc(1,sizeof的(INT));
    检验(P,免费);
}

编译:

  GCC -fomit-frame-pointer的-mrtd -c fx.c
GCC -fomit-frame-pointer的-mrtd fx.o -o fx.exe

编译警告:

  fx.c:在函数'主':
fx.c:11:5:警告:传递从兼容的指针类型测试的说法2 [默认启用]
fx.c:5:6:注:应为无效(*)(无效*),但参数的类型为无效(*)(无效*)'


解决方案

从GCC手册页 -mrtd

 警告:此调用约定是一个不兼容
通常使用在Unix上,所以如果你需要给你打电话不能使用它
与Unix的编译器编译库。

该警告是种奇怪的,但我相信它只是想告诉你,你传递一个指针,它使用一个不兼容的调用约定的函数。我想象中的免费在libc中会是这样的功能;我很惊讶,叫释放calloc 在所有工作。 (也许这是不是和你看到的是一个延迟的失败!)

This c code crash with a segmentation fault if I compile with GCC using -fomit-frame-pointer and -mrtd.

Is my code wrong in some way? Other function pointers works as expected, but not when its the free-function that is being passed? I get a warning when compiling, but I dont understand why or what I should do to fix. (I usually dont code in c, this error comes from a 3d part library that I use)

I need rtd/stdcall because Im on windows and need to call this library from python with ctypes, and -fomit-frame-pointer is included by default when compiling with GCC with -O1. (GCC version is 4.6.1 from TDM/Mingw32) It feels a bit strange that a default optimization option would cause trouble?

c-code:

#include <stdlib.h>

// void free ( void * ptr );
void test(void* o, void (*freeFunc)(void*)) {
    freeFunc(o);
}

int main() {
    int *p = (int *)calloc(1, sizeof(int));
    test(p, free);
}

compiled with:

gcc -fomit-frame-pointer -mrtd -c fx.c
gcc -fomit-frame-pointer -mrtd fx.o -o fx.exe

compile warning:

fx.c: In function 'main':
fx.c:11:5: warning: passing argument 2 of 'test' from incompatible pointer type[enabled by default]
fx.c:5:6: note: expected 'void (*)(void *)' but argument is of type 'void (*)(void *)'

解决方案

From the GCC manual page for -mrtd:

Warning: this calling convention is incompatible with the one
normally used on Unix, so you cannot use it if you need to call
libraries compiled with the Unix compiler.

The warning is kind of bizarre, but I believe it's simply trying to tell you that you're passing a pointer to a function which uses an incompatible calling convention. I imagine the free in libc would be such a function; I'm surprised that calling calloc works at all. (Or perhaps it isn't, and what you're seeing is a delayed failure!)

这篇关于用-fomit帧指针编译C -mrtd自由函数指针创建段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆