Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Comments
Albert Holguin 3-Dec-15 22:39pm    
Not sure why libraries aren't an option for you... ??? ....there's an open source logger called log4cpp that's become pretty popular in Linux, but it's definitely cross-platform.

1 solution

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++.

Objective-C
- (void) write: (NSString*) text
{
  char *text = [text cStringUsingEncoding:NSUTF8StringEncoding]; 
  LogMessage(text);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900