Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在共享库中使用fastcall安全吗?_C++_Gcc_Shared - Fatal编程技术网

C++ 在共享库中使用fastcall安全吗?

C++ 在共享库中使用fastcall安全吗?,c++,gcc,shared,C++,Gcc,Shared,例如,假设我有一个函数,可以为您交换32位值中的字节: uint32_t byte_swap(uint32_t in); 将32位值推到堆栈上并再次弹出它似乎很愚蠢,特别是如果我们要经常调用此函数,那么让我们通过ECX传递它: #if __FASTCALL_SUPPORTED_ /* Whatever this may be */ #define FASTCALL __attribute__((fastcall)) #else #define FASTCALL #endif uint32

例如,假设我有一个函数,可以为您交换32位值中的字节:

uint32_t byte_swap(uint32_t in);
将32位值推到堆栈上并再次弹出它似乎很愚蠢,特别是如果我们要经常调用此函数,那么让我们通过ECX传递它:

#if __FASTCALL_SUPPORTED_   /* Whatever this may be */
#define FASTCALL __attribute__((fastcall))
#else
#define FASTCALL
#endif

uint32_t FASTCALL byte_swap(uint32_t in);
现在我的问题是,将该函数编译成共享库以供分发是否安全?如果用户使用不同的编译器编译其程序和链接,该函数是否仍能正确调用?

\uuuu属性((fastcall))
是gcc扩展;因此,如果调用方没有使用gcc,那么它可能不可用。此外,在您给出的示例中,如果未定义uuu FASTCALL u SUPPORTED uu,您将以错误的调用约定结束调用-这是个坏主意

处理这种情况的一种方法可能是使用回退包装器。在.c文件中:

#include "foo.h"

uint32_t FASTCALL byte_swap(uint32_t in) {
    /* code ... */
}

uint32_t byte_swap__slowcall(uint32_t in) {
    return byte_swap(in);
}
在.h文件中:

#if __FASTCALL_SUPPORTED_   /* Whatever this may be */
#define FASTCALL __attribute__((fastcall))
#else
#define FASTCALL
#define byte_swap byte_swap__slowcall
#endif

uint32_t FASTCALL byte_swap(uint32_t in);
另外,请注意,在Linux上,fast byteswap实现在
中作为
bswap\u 32
提供。在x86机器上,它将编译成内联汇编程序,并且在足够高的
-march=
设置上有一条指令。

\uuuu属性((fastcall))
是一个gcc扩展;因此,如果调用方没有使用gcc,那么它可能不可用。此外,在您给出的示例中,如果未定义uuu FASTCALL u SUPPORTED uu,您将以错误的调用约定结束调用-这是个坏主意

处理这种情况的一种方法可能是使用回退包装器。在.c文件中:

#include "foo.h"

uint32_t FASTCALL byte_swap(uint32_t in) {
    /* code ... */
}

uint32_t byte_swap__slowcall(uint32_t in) {
    return byte_swap(in);
}
在.h文件中:

#if __FASTCALL_SUPPORTED_   /* Whatever this may be */
#define FASTCALL __attribute__((fastcall))
#else
#define FASTCALL
#define byte_swap byte_swap__slowcall
#endif

uint32_t FASTCALL byte_swap(uint32_t in);

另外,请注意,在Linux上,fast byteswap实现在
中作为
bswap\u 32
提供。在x86机器上,它将编译成内联汇编程序,并在足够高的
-march=
设置上执行一条指令。

此函数是库的公共接口的一部分,还是库的其他部分在内部调用?此函数是库的公共接口的一部分,或者它只是由库的其他部分在内部调用?