简体   繁体   English

如何将目标C代码添加到FireBreath项目中?

[英]How do I add Objective C code to a FireBreath Project?

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. 我正在编写适用于Mac OS的浏览器插件,该插件将在状态栏中放置一个状态栏图标,用户可以使用该图标与浏览器插件进行交互。 I've successfully built a FireBreath 1.6 project in XCode 4.4.1, and can install it in the browser. 我已经在XCode 4.4.1中成功构建了FireBreath 1.6项目,并且可以将其安装在浏览器中。 However, FireBreath uses C++, whereas a large majority of the existing libraries for Mac OS are written in Objective C. 但是,FireBreath使用C ++,而Mac OS的大部分现有库都是用Objective C编写的。

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: 在/Mac/projectDef.make文件中,我按照此处的建议以及在Internet上找到的其他资源中添加了Cocoa Framework和Foundation Framework:

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

I reran prepmac.sh, expecting a new project to be created in XCode with my .mm files, and .m files; 我重新运行prepmac.sh,希望在XCode中使用.mm文件和.m文件创建一个新项目。 however, it seems that they're being ignored. 但是,似乎它们已被忽略。 I only see the .cpp and .h files. 我只看到.cpp和.h文件。 I added rules for those in the projectDef.make file, but it doesn't seem to make a difference: 我在projectDef.make文件中添加了规则,但似乎没有什么不同:

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

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: 其中大约有20个,都与NSObjRuntime.h文件有关:

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... 它会像这样继续一段时间并出现类似的错误...

From what I've read, these errors appear because of dependencies on the Foundation Framework, which I believe I've included in the project. 从我阅读的内容来看,这些错误的出现是由于对Foundation Framework的依赖,我相信我已经将其包含在项目中。 I also tried clicking the project in XCode 我也尝试单击XCode中的项目

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. 人们说在C / C ++代码中使用Objective C并不难,但是XCode和Objective C的新手可能会加剧我的困惑。 This is only day 4 for me in this new platform. 在这个新平台上,这只是我的第四天。

What do I need to do to get XCode to compile the Objective C code? 我需要怎么做才能使XCode编译目标C代码? 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 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: 更新:

I edited projects/MyPlugin/CMakeLists.txt and added in the .m and .mm rules there too. 我编辑了project / MyPlugin / CMakeLists.txt文件,并在其中添加了.m和.mm规则。 after running prepmac.sh, the files are included in the project, but I still get the same compile errors. 运行prepmac.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. 我将所有.h文件和.mm文件从Obj C代码移至MyPlugin根文件夹,然后重新运行prepmac.sh文件。 Problem still exists. 问题仍然存在。 Same compile errors. 相同的编译错误。

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. 您的问题是您拥有.h文件,这些文件被具有目标c类型和/或包含在其中的c ++文件包含。 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. 为了做您想做的事情,c ++使用的所有类都需要将所有与obj c相关的东西保留在#ifdef中,或者干脆不在头文件中公开任何obj c。

For example, you can use this ifdef in your header: 例如,可以在标头中使用以下ifdef:

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

It's important to remember that .h files are includes as well as header files, so they are actually included. 重要的是要记住,.h文件是包括和头文件,所以其实包括很重要的。 If you include objective c code in a .cpp file the compiler gets confused and cries. 如果在.cpp文件中包含目标c代码,则编译器会感到困惑和哭泣。


EDIT: 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. 编辑:要记住的最重要的事情是,您需要确保cpp文件中不包含任何目标c头文件(或其他具有目标c代码的文件)。 If you can do that you'll be fine. 如果可以的话,您会没事的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM