John Smith wrote:
> On 24.8.2010. 12:21, Stuart Golodetz wrote:
>> John Smith wrote:
>>>
>>> I have a visual studio c++ project which is a dynamic dll.
>>> It's made for windows, and has windows.h and other windows specific
>>> headers.
>>> I want to compile the same project for linux and for mac os.
>>>
>>> Do you have any suggestions how to do that?
>>>
>>> I read about some library's like wxwidgets, boost, qt and similar.
>>> Do I have to use one of them?
>>>
>>> Thanks in advance.
>>
>> If it's of any significant size, I'd rewrite it to be
>> platform-independent from the ground up - porting a large,
>> platform-specific project is painful. While you're rewriting it, you can
>> also improve it - which is generally more motivating than the drudgery
>> of porting existing code that was never designed for it and fixing ~5000
>> compiler errors
You can, of course, reuse chunks of code from the
>> original where practicable.
>>
>> From personal experience, I can recommend CMake (www.cmake.org) as a
>> cross-platform built system - it takes a bit of getting used to, but
>> will generate platform-specific project files / makefiles for you based
>> on a script you write.
>>
>> As others have said, making your code build portably can be a lot of
>> work up-front. However, I've found that it's then generally pretty easy
>> to then keep your code portable as you develop. Invest the time (often a
>> few days, if your project is large) to get your build system set up
>> right at the beginning, and you'll experience far fewer problems down
>> the line.
>>
>> Re. libraries, you don't have to use any of the above. However, I
>> personally find Boost invaluable, even for platform-specific code. I can
>> recommend wxWidgets for cross-platform GUI development. Qt is also
>> well-spoken of by people who've used it - I haven't got personal
>> experience of it myself, though, so I can't say much about it.
>>
>> Cheers,
>> Stu
>
> I'm not very experienced in c++. If I decide not to use third party
> library's, what do I exactly have to do?
Depending on what you're trying to do (i.e. if it can't be done using
only the standard library, which happens a lot), you will probably need
to use *some* third-party libraries - but not necessarily the ones you
listed above. The important thing when writing portable code is to find
libraries that support as many platforms as possible (and, in
particular, at least the ones for which you're currently developing or
have plans to develop in the future). Generally speaking, I look for
ones that at least support Windows, Linux and Mac OS.
> Is it enough just to exclude windows.h and similar windows header files?
No - everything you use has to work on all the platforms on which you're
building. So you generally can't use any platform-specific libraries
(unless you have equivalents you can substitute in on platforms where
they're not supported, or the functionality they provide is
non-essential). Where you are forced to write platform-specific code,
you have to make sure that it's only compiled on that particular
platform - e.g. by using the preprocessor. If you do end up having to do
that, the general approach is to hide the platform-specific stuff behind
a platform-independent interface.
> Or I'd have to write some directives and include certain files for
> linux, windows and mac?
You may end up doing that in some cases. For example, I have a portable
header to include relevant OpenGL stuff that contains:
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX // prevent the min and max macros in windows.h being
defined (they interfere with the Standard C++ equivalents)
#endif
#include <windows.h>
#endif
#include "GLee.h"
#ifndef __APPLE__
#include <GL/gl.h>
#include <GL/glu.h>
#else
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#endif
The key differences there being that:
(a) On Windows, you have to include windows.h before including gl.h.
(b) On Mac, the GL headers are in a directory called OpenGL rather than GL.
There are often lots of annoying little things like this that you need
to play around with to get working.
> If the letter is the case, I think I'll stick to some third party library.
Doesn't always help you - see above. Basically, writing portable code is
a bit of a faff (if quite satisfying when you get it working)
Incidentally, if you're writing portable code then you'll probably end
up needing to be aware of byte-order and endianness issues, among other
things. Worth a Google...
Cheers,
Stu