【问题标题】:How to use libjpeg complex macros with Swift?如何在 Swift 中使用 libjpeg 复杂宏?
【发布时间】:2016-11-30 22:39:14
【问题描述】:

当我尝试调用 libjpeg 的 jpeg_create_decompress() 函数时,我得到了

/usr/include/x86_64-linux-gnu/jconfig.h:8:34: error: invalid suffix '.0' on floating constant
#define LIBJPEG_TURBO_VERSION 1.5.0
                             ^
.../main.swift:49:5: error: use of unresolved identifier 'jpeg_create_decompress'
jpeg_create_decompress(&info)
^~~~~~~~~~~~~~~~~~~~~~
CJPEG.jpeg_CreateDecompress:1:13: note: did you mean 'jpeg_CreateDecompress'?
public func jpeg_CreateDecompress(_ cinfo: j_decompress_ptr!, _ version: Int32, _ structsize: Int)
        ^
<unknown>:0: error: build had 1 command failures

现在,看看里面jpeglib.h我明白了

/* Initialization of JPEG compression objects.
 * jpeg_create_compress() and jpeg_create_decompress() are the exported
 * names that applications should call.  These expand to calls on
 * jpeg_CreateCompress and jpeg_CreateDecompress with additional information
 * passed for version mismatch checking.
 * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
 */
#define jpeg_create_compress(cinfo) \
    jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
                        (size_t) sizeof(struct jpeg_compress_struct))
#define jpeg_create_decompress(cinfo) \
    jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
                          (size_t) sizeof(struct jpeg_decompress_struct))
EXTERN(void) jpeg_CreateCompress (j_compress_ptr cinfo, int version,
                                  size_t structsize);
EXTERN(void) jpeg_CreateDecompress (j_decompress_ptr cinfo, int version,
                                    size_t structsize);

jpeg_create_decompress 不可用,可能如documentation 所述:

C 和 Objective-C 中使用了复杂的宏,但在 Swift 中没有对应的宏。复杂宏是不定义常量的宏,包括带括号的、类似函数的宏。您在 C 和 Objective-C 中使用复杂的宏来避免类型检查约束或避免重新输入大量样板代码。但是,宏会使调试和重构变得困难。在 Swift 中,您可以使用函数和泛型来获得相同的结果,而不会做出任何妥协。 因此,C 和 Objective-C 源文件中的复杂宏不适用于您的 Swift 代码

但是如果没有这个宏,我如何在 Swift 中使用libjpeg

【问题讨论】:

    标签: swift c-preprocessor libjpeg


    【解决方案1】:

    我没有看到在 Swift 中重新定义它们的方法,例如:

    class JPEGLibWrapper {
    
        class func jpeg_create_decompress(cinfo: j_decompress_ptr) {
            jpeg_CreateDecompress(cinfo, JPEG_LIB_VERSION, MemoryLayout<jpeg_decompress_struct>.size)
        }
    }
    

    或者在 Objective-C 中:

    @interface JPEGLibWrapper : NSObject
    
    + (void) jpeg_create_decompress:(j_decompress_ptr)cinfo;
    
    @end
    
    @implementation JPEGLibWrapper
    
    + (void) jpeg_create_decompress:(j_decompress_ptr)cinfo {
        jpeg_create_decompress(cinfo);
    }
    
    @end
    

    (可以使用更好的命名...)

    编辑我刚刚注意到您在 Linux 上使用 Swift,对吗?我不知道 Swift–C 桥接是如何在 Linux 上完成的,但我想可以做一些类似于 Objective-C 代码但在 C 文件中的事情。

    在 Swift 中声明一个 Objective-C 或 C 方法/函数的优势在于,在前者中你实际上是在使用复杂的宏,因此避免(错误)翻译它。你只需在它周围声明一个包装器方法/函数并将其公开给 Swift。

    来源:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2020-04-26
      相关资源
      最近更新 更多