如何将Objective C代码添加到FireBreath项目中? [英] How do I add Objective C code to a FireBreath Project?

查看:389
本文介绍了如何将Objective C代码添加到FireBreath项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为Mac OS编写一个浏览器插件,将在状态栏中放置一个状态栏图标,用户可以使用该图标与浏览器插件进行交互。我已经成功地在XCode 4.4.1中构建了一个FireBreath 1.6项目,并可以在浏览器中安装它。但是,FireBreath使用C ++,而大多数现有的Mac OS库是用Objective C编写的。

I am writing a browser plugin for Mac OS that will place a status bar icon in the status bar, which users can use to interface with the browser plugin. I've successfully built a FireBreath 1.6 project in XCode 4.4.1, and can install it in the browser. However, FireBreath uses C++, whereas a large majority of the existing libraries for Mac OS are written in Objective C.

在/Mac/projectDef.make文件中,我添加了可可框架和基础框架,建议这里和在互联网上找到的其他资源:

In the /Mac/projectDef.make file, I added the Cocoa Framework and Foundation Framework, as suggested here and in other resources I've found on the Internet:

target_link_libraries(${PROJECT_NAME}
    ${PLUGIN_INTERNAL_DEPS}
    ${Cocoa.framework} # added line
    ${Foundation.framework} # added line
)

我重新准备prepmac.sh,希望在XCode中用我的.mm文件和.m文件创建一个新项目;然而,似乎他们被忽视。我只看到.cpp和.h文件。我添加了在projectDef.make文件中的那些规则,但它似乎没有什么区别:

I reran prepmac.sh, expecting a new project to be created in XCode with my .mm files, and .m files; however, it seems that they're being ignored. I only see the .cpp and .h files. I added rules for those in the projectDef.make file, but it doesn't seem to make a difference:

file (GLOB PLATFORM RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    Mac/[^.]*.cpp
    Mac/[^.]*.h
    Mac/[^.]*.m    #added by me
    Mac/[^.]*.mm   #added by me
    Mac/[^.]*.cmake
)

即使我手动添加文件,我得到一系列编译错误。有大约20个,都与文件 NSObjRuntime.h 有关:

Even if I add the files in manually, I get a series of compilation errors. There are about 20 of them, all related to the file NSObjRuntime.h file:

Parse Issue - Expected unqualified-id
Parse Issue - Unknown type name 'NSString'
Semantic Issue - Use of undeclared identifier 'NSString'
Parse Issue - Unknown type name 'NSString'
...
...
Semantic Issue - Use of undeclared identifier 'aSelectorName'
...
...
Semantic Issue - Use of undeclared identifier 'aClassName'
...

具有类似的错误...

It continues like this for some time with similar errors...

根据我已经阅读的内容,这些错误出现是因为依赖于Foundation框架,我相信我已经包含在项目中。我也试过点击XCode中的项目

From what I've read, these errors appear because of dependencies on the Foundation Framework, which I believe I've included in the project. I also tried clicking the project in XCode

我现在的点,我不知道接下来要做什么。人们说,在C / C ++代码中使用Objective C并不困难,但对XCode和Objective C来说是新手可能会导致我的困惑。这只是我在这个新平台的第4天。

I'm to the point now where I'm not sure what to try next. People say it's not hard to use Objective C in C/C++ code, but being new to XCode and Objective C might contribute to my confusion. This is only day 4 for me in this new platform.

我需要做什么才能让XCode编译Objective C代码?请记住,我有一个新的,所以我会感谢你,如果你留下详细的答案,而不是在 firebreath tag。我只是在我的头上一点点,但如果你能让我超越这个障碍,我肯定会很好从那里去。

What do I need to do to get XCode to compile the Objective C code? Please remember that I'm a little new to this, so I'd appreciate it if you leave detailed answers as opposed to the vague one-liners that are common in the firebreath tag. I'm just a little in over my head, but if you can get me past this hurdle I'm certain I'll be good to go from there.

UPDATE:

我编辑了项目/ MyPlugin / CMakeLists.txt,在.m和.mm规则中也有。运行prepmac.sh后,文件包含在项目中,但我仍然得到相同的编译错误。

I edited projects/MyPlugin/CMakeLists.txt and added in the .m and .mm rules there too. after running prepmac.sh, the files are included in the project, but I still get the same compile errors.

我将所有.h文件和.mm文件从Obj C代码移动到MyPlugin根文件夹,并重新prepranac.sh文件。问题仍然存在。同样的编译错误。

I moved all the .h files and .mm files from the Obj C code to the MyPlugin root folder and reran the prepmac.sh file. Problem still exists. Same compile errors.

推荐答案

您的问题是,你有.h文件被包含在C ++文件,和/或包括在其中。为了做你想要的所有你使用的类c ++需要保持所有obj相关的东西在一个#ifdef或只是不公开任何obj c头文件。

Your problem is that you have .h files that are being included by C++ files that have objective c type and/or includes in them. In order to do what you want all of your classes that are used by c++ need to either keep all obj c related stuff in an #ifdef or simply not expose anything obj c in the header file.

例如,您可以在标题中使用此ifdef:

For example, you can use this ifdef in your header:

#ifdef __OBJC__
     // Objective C / C++ code here
#endif

重要的是记住.h文件是 以及头文件,因此它们实际上包括在内。如果在.cpp文件中包含目标c代码,编译器就会感到困惑和哭泣。

It's important to remember that .h files are includes as well as header files, so they are actually included. If you include objective c code in a .cpp file the compiler gets confused and cries.

编辑:最重要的事情要记住,你需要确保任何目标c头文件(或其他具有目标c代码的文件)不包括在cpp文件中。如果你能做到这一点,你会很好。

The most important thing to remember is that you need to ensure that any objective c header files (or other files with objective c code) are not included from the cpp files. If you can do that you'll be fine.

这篇关于如何将Objective C代码添加到FireBreath项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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