Tutorial :Cross platform programming [closed]



Question:

I need to write a small program for the university. The problem is, it has to be in C/C++ under linux, and I've never used linux, I anticipate having a lot of problems with the IDE, compilation, and all that.

Is it possible to code it under windows and then "copy/paste" the code and compile it under linux? What are limitations I should know about if it's at all possible?

It will be a small program, typical client/server communication using sockets.


Solution:1

I think you should go ahead and do it under Linux (gcc?). This will teach you some stuff about 'old school' programming. Forget about using an IDE, use vim (if you already get it) or nedit (more like notepad).

Compile on the command line. Link it yourself. Write a make file to do this.

This is the basics. You need to understand it before using an IDE. Do this while you are still at university, because it's a pain and you will (and should) want to use an IDE for real work!

Also, a basic understanding of Unix is not hard to achieve (I have found my way around Solaris, Ubuntu and OS X, coming from a Windows background) - a few simple tutorials should get you up and running. For writing small school projects, there is not much you need to know: cd, ls, mkdir, make, gcc (be sure to use g++ for C++ projects - that has bitten me on my Mac before...). Stay close to your home directory (~).

Doing your project on the target system will help you get certain stuff right: When doing these simple sockets and pthreads examples, I found compiling and linking them to be non-platform portable. On certain systems, linking in the libraries needs to be done this way, on others that way.

BTW: If you really do want to do this under Windows, your best bet is to have a POSIX environment under Windows. POSIX sockets are different to the Windows networking model if I remember correctly.

Try either MinGW or Cygwin. Both should give you the *nix development environment under Windows. You can use your favorite text editor (a Windows port of vim?) and cmd.exe instead of bash for starting the compiler :)

EDIT: Sorry, if the tone is confrontational (according to comment). I will try to soften it a bit. It's just... I have seen quite a few people trying to learn C/C++ (or Java for that matter) with IDEs and have come to believe that they get in the way for starting off. Sure, you will need better tools for real life programs, but the overhead of project files etc. for school sample projects adds clutter. It also makes it harder to email your homework to your teacher - a zip with a bunch of .c and .h files and a makefile is really as simple as it gets...


Solution:2

If you need to do the coding in Windows, I would recommend using mingw/msys as a development environment. Msys implements a unix-like shell on windows, and mingw is a port of the gnu compiler collection (gcc) and other gnu build tools to the windows platform. It is an open-source project, and well-suited to the task.

The install procedure can be a little bit tricky, but I found that the best place to start is here.


Solution:3

Technically, you can write a lot of code that will work with very few changes across windows and linux platforms.

However, things get tricky in some cases. It's often easier to go from linux -> windows than from windows -> linux, just because windows users tend to use Windows API calls that do not have linux equivalents.

If you're going to use a GUI, you'll want to try to use a cross platform GUI. Qt or wxWidgets are good options. Qt, in particular, also has many cross platform utility classes that fall outside of the GUI space, and may help with the socket issues.

If your goal is really just to have it working on linux, and the software doesn't have to run on Windows, I'd recommend just jumping in and trying to make it work there from the start. It will be easier than trying to port.

As for a Linux IDE - at some point, plan to jump in and use Linux tools - you'll need a different build system (unless you use a cross platform solution like cmake or scons), there will be different IDEs if you use IDEs, etc.

KDevelop is a nice IDE that will be more familiar than trying to do everything from a command line, if you're used to working in an integrated IDE.


Solution:4

Have a look at the QT Library and tools


Solution:5

Rather than cut and paste, divide your project into platform agnostic files and platform specific files and/or use preprocessor directives to customize the code. Most of my work is for Windows, and it's nice to use it as a test bed for the Linux stuff using tools that are familiar.


Solution:6

First: install Cygwin, this provides UNIX like environment for windows with POSIX API that MingW doesn't provide, for example fork(). So you would be able to work in windows and feel like in Linux.

Second: I'd suggest to get used to Linux/install it, knowing Linux/UNIX environment would make your life in university (and not only there) much easier.


Solution:7

If you can write the program using only the standard C libraries, then theoretically it should already be cross-platform compatible, as the C library itself is designed to be totally portable. That said, you're going to run into issues with sockets, which unless I'm mistaken are usually implementation specific.

Linux (ubuntu especially) isn't really that hard to use, but the lack of a real IDE for development usually throws folks off. What I recommend is that you compile using Cygwin for windows, and try out a code editor called CodeBlocks while you do this. Cygwin will compile your code against linux libraries instead of windows ones (and then emulate those libraries using a windows .dll when you run it) so it's a good way to become familiar with your limitations. You should be able to set up a CodeBlocks project under windows, and then open that same project under the Linux version of CodeBlocks and test out your compile.

Cross-platform code isn't terribly easy to write, I admit, but it's more than possible with a bit of effort.


Solution:8

Both Windows and Unix-style platforms offer Berkeley Sockets APIs, but Windows implementation has some differences, e.g. the use of WSAStartup() and WSACleanup() functions. Other than that, a few #ifdefs and the application will compile on both platforms without any problems. Assuming it's supposed to be a simple command-line app of course. There's a section Porting Socket Applications to Winsock on MSDN. All you have to do is to work backwards. ;)

Also read a book on socket programming, for example Beej's Guide to Network Programming.

Another option is to use Boost::Asio. This hides any sockets API differences very smoothly.


Solution:9

I agree with others that if you install something like Ubuntu Linux, it's not that hard to learn. But to answer your actual question, the main issue is to avoid using Windows APIs unintentionally. My favorite environment for this is the AT&T U/Win toolset, which is designed for compiling POSIX apps on windows. It seems to generate fewer complaints from developers than mingw, which in turn generates fewer complaints than cygwin.


Solution:10

If you stick to the C/C++ standards, you should not have a problem porting well-written code from windows to unix, with some minor tweaking of included headers and the like.

I'm guessing that there is no GUI other than command line since its a client/server assignment under linux, which makes it possible. But be very careful that you stick to unix style sockets and C/C++ standards, and not use any windows libraries or shortcuts.


Solution:11

Juan,

If you're going crazy just learning your way around Linux, then you don't have the patience necessary to be a programmer. Coding isn't glamour, it requires meticulous effort, reading documentation, trying things out, staring at a debugger for hours.

You need to either rethink being involved in programming or buckle under and put in the effort to learn to navigate Linux, which is nowhere near as complex as any useful programming language.


Solution:12

If it don't use Windows API specific code or non-cross-platform libraries, and if it's standard C++ - if it don't use microsoft compiler specific features - then it should be cross platform and work on linux.

The compiler provided with linux is called GCC. There is a Windows version called MinGW . You could try it before trying on linux. If it compiles on MinGW, it will certainly compile on Linux. (The problem is MinGW is not up to date with the last versions of GCC...)

For usage of libraries that are not cross-platform (maybe GUI libraries), yous will have to change them, but most of the non OS-specific GUI libraries are cross-platform anyway. For other libraries, you'll find equivalent ones on other platforms I guess.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »