【问题标题】:Why Does This Objective C/C++ Code Require main.m instead of main.mm?为什么这个 Objective C/C++ 代码需要 main.m 而不是 main.mm?
【发布时间】:2016-04-07 18:48:20
【问题描述】:

当我将以下命令行程序从 main.m 重命名为 main.mm 时,出现奇怪的代码错误。像 main.m 一样工作得很好。有人知道为什么吗?

https://stackoverflow.com/a/36469891/105539

来源

#import <Foundation/Foundation.h>

void detectNewFile (
    ConstFSEventStreamRef streamRef,
    void *clientCallBackInfo,
    size_t numEvents,
    void *eventPaths,
    const FSEventStreamEventFlags eventFlags[],
    const FSEventStreamEventId eventIds[])
{
    int i;
    char **paths = eventPaths;

    printf("GOT AN EVENT!!!!\n");
    for (i=0; i<numEvents; i++) {
        printf("Change %llu in %s, flags %u\n", eventIds[i], paths[i], (unsigned int)eventFlags[i]);
    }
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        short nPathCount = 2;
        CFStringRef mypath[nPathCount];
        mypath[0] = CFSTR("/Users/mike/Documents");
        mypath[1] = CFSTR("/Users/mike/Downloads");
        CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, nPathCount, NULL);
        void *callbackInfo = NULL;
        CFAbsoluteTime latency = 1.0; // seconds

        FSEventStreamRef hStream = FSEventStreamCreate(NULL,
            &detectNewFile,
            callbackInfo,
            pathsToWatch,
            kFSEventStreamEventIdSinceNow,
            latency,
            kFSEventStreamCreateFlagFileEvents
        );

        FSEventStreamScheduleWithRunLoop(hStream, CFRunLoopGetCurrent(),         kCFRunLoopDefaultMode);
        FSEventStreamStart(hStream);
        printf("Waiting on new file creations...\n");
        CFRunLoopRun(); // runs in an endless loop, only letting the callback function run

    } // end autorelease pool
    return 0;
}

错误

FOR: 
        char **paths = eventPaths;

Cannot initialize a variable of type 'char **' with an lvalue of type 'void *'

FOR: 
        FSEventStreamRef hStream = FSEventStreamCreate(NULL,
            &detectNewFile,
            callbackInfo,
            pathsToWatch,
            kFSEventStreamEventIdSinceNow,
            latency,
            kFSEventStreamCreateFlagFileEvents
        );

No matching function for call to 'FSEventStreamCreate'

【问题讨论】:

  • 使用相关代码和您遇到的错误更新您的问题。
  • @rmaddy 好的,我做到了。
  • 因为 char **paths = eventPaths; (其中 evenPaths 是无效的)在 C 中是合法的,但在 C++ 中是不合法的。
  • @johnelemans 如果我添加了(char **) 铸造,它是否有效?另外,对第二个错误有什么见解吗?
  • 是的,应该可以。检查您的参数类型以匹配调用。

标签: objective-c compilation


【解决方案1】:

感谢@johnelemans,我发现了问题。在 C 中,从 void *char ** 的自动转换是合法的,但在 C++ 中则不行,这是 .mm 文件将其切换到的内容。解决方法是使用强制转换:

char **paths = (char **)eventPaths;

然后,在 FSEventStreamCreate 上,它不喜欢 void * 而不是这个:

FSEventStreamContext *callbackInfo = NULL;

...并且不喜欢 CFAbsoluteTime 而不是:

CFTimeInterval latency = 1.0; // seconds

然后,您需要将 CoreServices.framework 库添加到构建步骤中。

我进行了这些更改,现在可以编译了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2017-08-28
    • 2017-05-12
    • 2012-01-30
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多