views:

669

answers:

12

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.

+4  A: 

Have a look at the QT Library and tools

Conrad
I think QT might be a bit of an overkill for a small university project.
nstehr
Nope, Qt is quite easy, and he won't need a commercial license.
MSalters
+20  A: 

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 allready 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-plattform 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...

Daren Thomas
Your tone is a bit confrontational, but I agree with your statements. The best way to learn how to program in a given environment is to jump right in. +1
e.James
This is good advice for leaning The Unix Way(tm) (modulo s/vim/emacs/g of course), but for getting the job done, he's probably better using eclipse or something.
dmckee
Thanks for the advice, but I don't think I'll ever use linux again... I already have an ubuntu image on a virtual machine on my notebook, but the overhead of "not finding basic stuff" is driving me crazy
Juan Manuel
@Juan: sounds like you're just hitting the normal learning curve ;-) There are plenty of tutorials and references out there on the internet that can help you (what the heck, I can even guide you through some of it personally if you want, if we can get in touch privately)
David Zaslavsky
@Juan: funny, I have the same problem ("not finding basic stuff") on MS Windows. Anyway, if you program for a target platform, knowing it will not hurt your program...
@Juan: "...but I don't think I'll ever use linux again..." - I would recommend that you rethink this. If nothing else, there are some really interesting things going on over in Linux land that don't translate well to Windows.
TwentyMiles
The IDE becomes a crutch if you can't build your code without it. Regardless of platform, make sure you can make your deliverables without the IDE and rely on the IDE only as an accelerator to development. Independence is key.
yetanotherdave
Thanks for all the advice on trying linux, I think I'll try it a while, but if the learning curve is too steep, I'll come back here and try some of those tools people are recommending :)
Juan Manuel
I would recommend being patient. Why not set up a dual-boot or Wubi (http://wubi-installer.org/) installation of Ubuntu (for instance), then learn gradually? As far as your current project, you should be able to port work you do in MingW (or similar) to GNU/Linux as long as you stick to standard APIs. I find the Open Group website helpful for this. I also agree that IDEs can easily be overkill for small projects.
Matthew Flaschen
+5  A: 

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.

e.James
Thanks, I'll check it out
Juan Manuel
+5  A: 

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.

Reed Copsey
A lot of linux OSS projects are full of POSIX-isms that have no reasonable equivalent on Windows outside of things like Cygwin that provide it. Cross platform is simply harder than single platform coding because you have to recognize the portability gotchas as you go.
RBerteig
+3  A: 

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.

Nicholas Flynt
He could also use Eclipse CDT.
Mike Caron
Ooh, good call. I keep forgetting that Eclipse isn't only for Java. ^_^
Nicholas Flynt
+1  A: 

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.

cyberconte
+3  A: 

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.

Mark Ransom
Splitting up the files is a good idea, although if he decides to go with QT or WxWidgets, shouldn't have too much that's platform specific.
Mike Caron
A: 

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.

Klaim
actually, the compiler is still GCC, MinGW is an environment that GCC has been ported to run in.
Mike Caron
Actually, MinGW a distribution of GCC, as well as the team of people that make sure that the GCC compilers and related tools can be built as windows-native binaries and produce windows-native binaries. They leverage the existing MSVCRT.DLL C runtime library that ships as a system component to provide libc and libm, and provide independently written, GPL-compatible header files for the Windows API. Programs compiled with the MinGW distribution of GCC are native executables and (usually) have no unexpected runtime dependencies.
RBerteig
+3  A: 

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.

Artyom
+1  A: 

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.

macbirdie
+1  A: 

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.

Ben Voigt
hmm, I don't agree. It's just a matter of optimizing my time and prioritizing, it's not a matter of attitude or "hating linux", it's a small program for a university course... I already have a full time job programming, a pregnant wife, and several other subjects I'm taking =) But thanks for the advice
Juan Manuel
I don't agree with Ben's "you aren't cut out for it" assessment, but learning Linux is definitely worth the effort over the long run. Keep trying!
Parappa
+2  A: 

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.

Norman Ramsey