简体   繁体   中英

How can I use my custom types (C++) in an objective-c project with xcode?

I have a few classes I wrote to deal with numbers having units transparently as if they were just ints or floats. In a nutshell:

SI_Term speed(4, "mph");
SI_Term distance(10000, "feet");
SI_Term time = distance / speed;
std::cout<<time.string();

and all the major operators are overloaded to work this way. Took a quite a bit of work so I would like to use the C++ classes in my ObjC iPhone app. I tried adding them with .h and .mm extensions to my xcode project, but it doesn't seem to be treating them as C++ files. (Won't find any of the STL headers, syntax errors anywhere I declare a class or anything).

New Info: Tried .mm and .h extensions, errors: in a .h file: namespace DA and just about everything else involving c++ code (about 40 errors all this message) gives me expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

I also get *: No such file or directory for all instances of #include <*> throughout the c++ files.

I'm sure its some kind of simple fix but .mm and .h wasn't enough.

Update: Found that copying and pasting source code into new files seems to work. The mm file (or the cpp file) compiles. The h file doesn't work though, it throws the above mentioned error at any occurrence of c++ specific code.

Thanks!

You are probably including (or importing) your h file in one of your .m files. Those need to be renamed to .mm too.

In the end you'll probably name all .m fikes to .mm. And by the way, the cpp files, can simply remain cpp. No reason to rename them.

我不知道到底要得到什么错误,而不是将扩展名为.hpp和.cpp的文件添加到项目中,而是将头文件的扩展名更改为.h,并将扩展名更改为.cpp文件转换为.mm或.M,以便Xcode将其识别为Objective-C ++文件,并进行适当的编译。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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