Quote:
Original post by littlekid
hi, I am pretty confused over how Mac OS X and Linux handle their string. I have searched on google and some have said that in mac and linux, char are handled natively as unicode. Does that mean I don't have to use wchar_t/std::wstring in mac/linux??
std::string and "char*-strings" are always C/C++ strings. Support for unicode requires extra care.
Quote:
I have been programming in windows and have always you std::wstring. What about in mac/linux?
Personally, I've always only had a use for std::string, which works fine among OSes.
Quote:
Is there anyway to easily achieve platform independence?
Of course, stick with the Standard-C++ given stuff. Full independence, though, is only seldomly sane (or do you want to support fridges and remote controls?), and in case you really need Unicode support or whatever, you might want to use an external library that has support for what you want to support.
The next revision of the C++ standard will also feature a few more string variants, btw.
Quote:
I was thinking of using something like
#ifdef WIN32typedef std::wstring tstring;#define TSTR(text) L##text#elsetypedef std::string tstring;#define TSTR(text) text#endifint main(){ tstring libraryPath(TSTR("path\myfilename"));#ifdef WIN32 LoadLibrary(libraryPath.c_str());#else dlopen(libraryPath.c_str(), RTD_LAZY);#endof return 0;}
Is this a recommended method??
If this method is portable enough for you, then I'd say use it. But it is more probably like
int main(){#ifdef WIN32 tstring libraryPath(TSTR("foo\\myfilename")); LoadLibrary(libraryPath.c_str());#else tstring libraryPath(TSTR("frob/myfilename")); dlopen(libraryPath.c_str(), RTD_LAZY);#endof return 0;}
Also, be aware that you have a severe mistake in your code:
tstring libraryPath(TSTR("path\myfilename"));
Use slashes, not backslashes. Filenames in C/C++ are always with slashes as the path-seperator. Backslash might work, but are OSspecific (Windows in this case), and error-prone:
path
\myfilename
evaluates to
[path] [
\m] [yfilename]
. In this case you are lucky, but you be less lucky if it happens to be a valid escape character like \n or \0. Then it becomes a runtime error, and possibly be hard to detect. The better version would be
path
\\myfilename
and the canonical C++ version would be
path
/myfilename
Quote:
p.s Sometimes I wonder why can't all OS have a standard way of doing things, makes developer life easier in doing cross platform =D
Oh, there is:
POSIX. You'll see that a plethora of operating systems follow that standard to a large degree.
I can recommend Qt+MinGW for apps-development, which will let you reuse all or a lot of your code on windows, if planned right (my pet picogen [see sig.] compiles and runs on windows+linux thatnks to it).