Import .CPP files from a working iOS project and rename to .mm issue

拟墨画扇 提交于 2019-12-31 05:59:07

问题


I have copied a .CPP and its .h file from a working project to a new one.

I renamed the ending from .CPP to .mm but it still gives me errors.

In the .h file, near the class definition class MeterTable, it says it expect the ;

In the .mm file, there are all kinds of errors.

I thought by changing the ending of the implementation file .mm it would clean all those errors. And yes, the original .CPP file compiled under the old project.


回答1:


Looks like you're including that .h file into some other .m file. You cannot just include C++ header into a C or Objective-C source file. You need to make sure that your C++ interface is C compatible (no classes, only free functions without overloading). Or you would have to change all your .m files in the project into .mm.




回答2:


The only solution is to have an inline header file.

Change your .h file to .hpp and combine your .cpp file with your .h file.

As example; your .h file:

// class.h

class {
public:
    void someclass(int a);
};

And your .cpp file:

// class.cpp

#include "class.h"

void someclass(int a) {
    return;
}

Merge into .hpp like this:

// class.hpp

class {
public:
    void someclass(int a) {
        return;
    }
};



回答3:


Objective-C and C++ can play nicely together, but there are a couple of caveats. First, any .m file that references C++ code (i.e., via a header) must be defined to be an Objective-C++ source, not Objective-C. This can be done on a case-by-case basis by going to the File Inspector for the .m file and changing the file type from Objective-C Source to Objective-C++ source. Second, use the same method to define the .h and .cpp (or .mm, the actual extension does not really matter, it's the defined types) as C++ Header and Objective-C++ source, respectively.

As long as you use this compartmentalization of file types, you should be able to mix Objective-C and C++ freely -- I commonly use this paradigm to consume C++ in my iOS code.



来源:https://stackoverflow.com/questions/12920598/import-cpp-files-from-a-working-ios-project-and-rename-to-mm-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!