如何在iphone项目中添加C ++文件 [英] How to add C++ files in iphone project
问题描述
我有一个iPhone项目,在这里我想使用c ++文件。我创建了如下所示的c ++文件:
File-> New File - > C / C ++ files - > C ++ File
它为 ClassA.cpp
在ClassA.cpp 中
#include< iostream>
class ClassA
{
public:
int a,b;
void add();
};
void ClassA :: add()
{
// printf(sdf); $ file
> p>
#importViewController.h
#importClassA.cpp
- (void)viewDidLoad
{
ClassA a;
a.add();
[super viewDidLoad];
//在加载视图之后执行任何其他设置,通常来自nib。
}
我读了一些帖子,我说我把.m重命名为.mm,所以我将所有.m文件重命名为.mm
并添加了两个链接器标志
-cclib -lstdc ++
但它给出以下错误:
解决方案您需要将C ++部分拆分为头文件和 cpp
文件,否则 ClassA :: add
被定义两次。
ClassA.h:
#include< ; iostream>
class ClassA
{
public:
int a,b;
void add();
};
ClassA.cpp:
#includeClassA.h
void ClassA :: add()
{
// printf(sdf);
}
您的.mm文件:
#importClassA.h
...文件的其余部分...
I have an iPhone project, in this I wanted to use c++ files. I created c++ file like below:
File->New File -> C/C++ files -> C++ File
and named it as ClassA.cpp
In ClassA.cpp
#include <iostream>
class ClassA
{
public:
int a, b;
void add();
};
void ClassA::add()
{
// printf("sdf");
}
in my viewController.mm file:
#import "ViewController.h"
#import "ClassA.cpp"
- (void)viewDidLoad
{
ClassA a;
a.add();
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
I read some posts it says me to rename .m to .mm so I renamed all .m files to .mm
And added two linker flags
-cclib -lstdc++
But It gives the following error:
解决方案 You need to split the C++ portion into a header file and a cpp
file, otherwise the ClassA::add
will be defined twice.
ClassA.h:
#include <iostream>
class ClassA
{
public:
int a, b;
void add();
};
ClassA.cpp:
#include "ClassA.h"
void ClassA::add()
{
// printf("sdf");
}
Your .mm file:
#import "ClassA.h"
... the rest of the file ...
这篇关于如何在iphone项目中添加C ++文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!