在Mac和Windows上使用C ++源文件 [英] Using a C++ source file across Mac and Windows

查看:192
本文介绍了在Mac和Windows上使用C ++源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个标题只有C ++源文件,它主要使用标准C ++ 11实现日志记录,因此它是可移植的,可以在Mac和Windows上运行。只有一个类在典型项目中为标准日志记录要求实现静态函数。



它在#ifdef / #endif块中有一些使用NSString的函数以及使用Cocoa的基于UI的项目的其他几个Cocoa类。标题中的其他所有内容都是纯粹的C ++。



任何使用日志记录的C ++源文件都包含此标头,并且为了能够在XCode中编译,文件设置指定编译为目标C ++,尽管文件扩展名为。 cpp。



让.cpp文件具有Objective C代码(通过包含此标头)并编译为.mm(Objective C ++)似乎尴尬,但是为了保持便携性,.cpp扩展名是必要/适当的。



假设使用Boost或其他库不是一种选择,如何纠正这种情况 - 不必将.cpp文件编译为Objective C ++,但是能够使用头文件来记录.mm文件中的NSString值。

Given a header only C++ source file that implements logging mostly using standard C++11 so that it is portable and works on both Mac and Windows. Just has a class that implements static functions for "standard" logging requirements in a typical project.

It has a few functions within #ifdef/#endif block that use NSString and few other Cocoa classes for UI based projects that use Cocoa. Everything else in the header is "pure" C++.

Any C++ source file that uses logging includes this header and to be able to compile in XCode the file settings specify "compile as Objective C++" although the file extension is .cpp.

It seems "awkward" to have a .cpp file have Objective C code (by including this header) and compile as .mm (Objective C++), but to keep it portable the .cpp extension is "necessary/ appropriate".

Assuming that using Boost or another library is not an option, how can this situation be rectified - not having to compile a .cpp file as Objective C++, but yet being able to use the header for logging say NSString values from a .mm file.

推荐答案

你可以在Xcode中使用C和C ++文件,但必须为Xcode编译器创建正确的文件。使用File-> New-> File并在向导中选择正确的类型。



将Cocoa类与纯C / C ++分开更容易。我们经常使用Coca-Wrapper类将数据转换为纯C数据而不是调用一些C ++。



You can use C and C++ files in Xcode, but you must must create it correct for the Xcode-compiler. With "File->New->File" and select the correct type in the wizard.

It is easier to separate the Cocoa classes from pure C/C++. We often use a Coca-Wrapper class which converting the data to pure C data and than calling some C++.

- (void) write: (NSString*) text
{
  char *text = [text cStringUsingEncoding:NSUTF8StringEncoding]; 
  LogMessage(text);
}


这篇关于在Mac和Windows上使用C ++源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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