Platform independent code?

J

joe

John said:
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.

"John Smith' eh? Aren't you the one who married Jane Doe? And is your
middle name Troll by chance?
 
J

John Smith

"John Smith' eh? Aren't you the one who married Jane Doe? And is your
middle name Troll by chance?

Actually, that's my cousin.

By the way I used wxWidgets and compiled an so library on linux.
When I test it I'll let you know how it works.
 
B

BGB / cr88192

Paavo Helde said:
@news.metronet.hr:


all.

Sounds not too bad, depending on what functions are involved. Add a
header which defines Windows typedefs in a portable way and replaces
Windows-specific functions with portable or target-specific ones.

For example:

#include <stdint.h>

typedef uint32_t DWORD;
//...
inline int _ch_size(int fd, long size) {return ftruncate(fd, size);}
//...

A more proper way would be to use own names and typedefs in the code,
with different headers and cpp files for implementing them as needed on
each supported platform. However, this may require extensive rewrite of
the code.

or using find/replace (in a text editor), or sed (sort of the power-tool of
find/replace, for when doing find/replace on an entire directory of
files...).
 
T

Timothy Madden

John said:
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.

Porting, especially from Windows code (that is usually ignorant of
portability issues) is a highly non-trivial task. You simply can not
expect to find some easy drop-in replacement library that will make
Windows symbols magically work on Linux and Mac OS.

Happily for you porting a shared library is likely to be easier than
porting an application.

Despite what people here say or of your attempts already, trying to
supply all those Windows headers definitions yourself is the wrong thing
to do (and also it is not porting, it is creating an emulation layer,
again a non-trivial task).

The right thing to do is change your library project, so it uses
functions provided by standard or portable interfaces, like the standard
C library, the standard C++ library, boost library, POSIX/XSI functions,
etc, instead of the Windows ones.

In doing this you will find that you will need to include some more
portable libraries in your project. The 3 libraries you mentioned are
all very good ones indeed. As others said, many people (including me)
find boost an invaluable library to have, even if you are not porting.

Depending on how vendor-specific the previous code has been written,
this may be a difficult task. However, for a shared library, you are
still likely to be fine.

Beware that even if using standard or portable interfaces, you still
have to be careful for your resulting program to be portable. For
example getenv("PATH") will return the PATH environment variable, but if
you later parse it as a semi-colon ";" separated list of directories
than you are still off the Linux world, although getenv("PATH") is a
POSIX call. Also if you try to create a file with
ofstream("device.00:FA:9B:65:5A:F0") you are now off the Windows world
(since Windows file names may not include ":" character) even though you
would be using the standard C++ ofstream class. To generalize, file
names are not portable, even though the interfaces you use to access
files are portable.

Have fun,
Timothy Madden
 
J

John Smith

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.

After porting the library to linux I have some problems regarding
invalid handles.

Anywas I suspect the reason is in the GetSymbol function from wxWidgets
used instead of GetProcAddress.
GetSymbol returns void*, and I continued to use unsigned long as a
result of GetProcAddress.

The library itself returns some int values which should now be void*.
I thing converting this is a bit ugly.

Are there any suggestions of examples about this?
 
J

John Smith

After porting the library to linux I have some problems regarding
invalid handles.

Anywas I suspect the reason is in the GetSymbol function from wxWidgets
used instead of GetProcAddress.
GetSymbol returns void*, and I continued to use unsigned long as a
result of GetProcAddress.

The library itself returns some int values which should now be void*.
I thing converting this is a bit ugly.

Are there any suggestions of examples about this?


Dismiss this last post. Everything works fine.
I used wxWidgets for dynamic library loading, and portable code for
other things.
Thank you all for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top