LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2006, 04:37 AM   #1
aaron4katie
Member
 
Registered: Dec 2006
Location: Bristol, England
Distribution: Fedora Core 6
Posts: 77

Rep: Reputation: 15
C++ Programming For Linux AND Windows?


Can programs be made in C++ and compiled for Linux and Windows, even if it is compiled differently for Windows, can the same C++ code be used?
 
Old 12-12-2006, 09:25 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Not easily. Platforms are very different. To do this, you have to stick with ANSI C++ and avoid any system calls. If you want to use a graphical UI, you need to stick with a UI that is available on both platforms, such as gtk-widgets.

Console apps that don't deal with the system can do it (although anything involving a directory gets into issues with forward slash vs backslash), but if you get more complicated it gets hard.
 
Old 12-12-2006, 10:16 AM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Moved: This thread is more suitable in the Programming Forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 12-12-2006, 11:37 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
For all your cross-platform C++ needs (use it with gcc
in Linux, and mingw or VC++ on windows).

http://www.trolltech.com/products/qt



Cheers,
Tink
 
Old 12-12-2006, 04:11 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by jiml8
Console apps that don't deal with the system can do it (although anything involving a directory gets into issues with forward slash vs backslash), but if you get more complicated it gets hard.
True that certain kinds of more highly system-specific code is more difficult (or impossible) to port between operating systems, but Windows NT -based systems are at least mostly POSIX-ly compliant. Also, Windows systems have treated forward slash as a directory separator for a long time, so there's no problem there. Of course, if you're looking for "/usr/local/bin", of course it's not to be found.
 
Old 12-12-2006, 05:28 PM   #6
Lazy Foo'
Member
 
Registered: Dec 2005
Location: LA
Distribution: SuSE 9.3 Pro
Posts: 32

Rep: Reputation: 15
Yes.

My game programming tutorials prove that.

You just have to use a cross platform library API like SDL, OpenGL, HawkNL, wxWidgets, etc.
 
Old 12-12-2006, 05:42 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
It depends on the code. As stated above, if you're using operating system-specific function calls, no. But there exist application development frameworks which implement common APIs on both platforms. There are some alternatives, and quite a few different ones with different emphasis:

QT. Excellent general purpose application development framework. They claim porting from one platform to another is as easy as re-building. I think it's a superb API. Well thought out, powerful etc. You can also flip a switch and have your app display in a style which looks native on each windowing system. QT is the underlying API for the KDE desktop environment.

wxwindows is "MFC, but done right". Personally I find wx apps on Linux to feel odd and clunky. The window borders draw funny under KDE, window resizing is not fast and so on, but that might just be a bad use of the API in the apps in which I've noticed it used.

GTK. The big rival to QT on Linux. It's a C API, but there's a C++ interface called gtkmm. GTK is the underlying API of the gnome environment. I belileve it was originally coded for The GIMP. I have heard that porting apps to Windows using GTK is a more difficult than with QT. I never used it apart from on Linux. It's nice.

SDL is not so general purpose. It's more for game-like apps. There are SDL components for doing graphics, audio, font handling, image file format handling etc. There's no GUI toolkit in SDL, it's rather lower level than that, but there are some GUI toolkits which run on top of it, like CEGUI. SDL is nice. One of the projects I am involved in, Stellarium, uses SDL as the underlying API. To get program to compile cross platform, you need to do a fair few #ifdefs, but it works.

Ogre3D is a more fully fledged game API. It's much heavier than SDL, but contains some amazing functionality for game devs.

The list goes on. I'm only mentioning the ones I've looked at personally.
 
Old 12-12-2006, 11:55 PM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
There's also FLTK.
 
Old 12-13-2006, 05:30 AM   #9
aaron4katie
Member
 
Registered: Dec 2006
Location: Bristol, England
Distribution: Fedora Core 6
Posts: 77

Original Poster
Rep: Reputation: 15
I was just wondering how some programs are made for single operating systems and the developers find it hard to produce programs for a range of operating systems. I saw how some programs are available for each version of mac, Linux, Unix and windows, this is rare but i didn't think they would have to reprogram something for a different operating system.
 
Old 12-13-2006, 09:28 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I think the main thing is to start a project with portability in mind, and choose libraries with this in mind. Many apps start with only one platform in mind, become successful and then have massive trouble porting because they basically have to re-write the whole thing for the new platforms.

A common example would be someone writing a program in MFC on Windows, and then finding out that 30% of their target market has moved to the Mac.... if they want to get that 30% of the market they need to re-write their program.

The cost of porting from scratch late in an application's life-cycle is high. Assuring consistent behaviour between separate code-cases is difficult. Worse, maintaining two separate code-bases significantly increases the cost of development of new features, and bugfixes.

All this cost can be effectively eliminated by choosing a good cross platform API at the start of the project.

There are some costs associated with choosing a cross platform API over the native API. The most obvious for most developers is learning a new API if the developer doesn't already know it. These frameworks are large and complex, and can require significant effort to learn well. Another potential cost is the loss of OS-specific features. The cross platform APIs may not implement OS integration as completely as native APIs. Lastly, the resulting API may not look native (e.g. audacity on Linux looks "weird"). This can put users off using the application.
 
Old 12-13-2006, 09:55 AM   #11
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by matthewg42
The cost of porting from scratch late in an application's life-cycle is high. Assuring consistent behaviour between separate code-cases is difficult. Worse, maintaining two separate code-bases significantly increases the cost of development of new features, and bugfixes.
One of the symptoms you see of this, and what really annoys me, is when they're well into version 5 or something on Windows, but the Linux and Mac versions are still stuck at 4. Let's you know where you stand, and I'd imagine it causes much customer dissatisfaction.

Last edited by 95se; 12-13-2006 at 09:57 AM.
 
Old 01-18-2007, 11:30 PM   #12
polpoint
LQ Newbie
 
Registered: Jan 2007
Posts: 1

Rep: Reputation: 0
I have a program that I took some minor pains to ensure it was portable and did not call anything from windows.h or and winapi etc.al (sdl & opengl are pretty good for that)

I use Visual Studio .NET 2003 and have the other free VS on another computer.
Is it possible to compile through VS for Linux? I do not have very high hopes that this is even possible, but I might be surprised.

Now that I have met all of the obstacles mentioned above regarding OS specific calls:
What can I use to compile on windows so it can run on linux?

What should I download??
 
Old 01-18-2007, 11:48 PM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
There is no VS for Linux. If you want to do compiler portability, that's great, but if all you really care about is platform portability, mingw is your friend.

I don't think there is any way for windows to cross-compile for linux.
 
Old 01-19-2007, 06:57 AM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
You could use premake to generate makefiles and vs projects, or you could use Codeblocks which is a cross platfrom IDE.

If you don't have a linux box and use premake you can then compile with the help of Cygwin(which makes a windows executable from linux source, requires more packages to do this), or you install VMware or VirtualPC and create a linux install on a windows partition or just create a linux partition on your box.

Last edited by dmail; 01-19-2007 at 07:08 AM.
 
Old 01-19-2007, 07:25 AM   #15
deroB
Member
 
Registered: Dec 2005
Location: Sydney, Australia
Distribution: Arch Linux
Posts: 208

Rep: Reputation: 30
Quote:
Originally Posted by Lazy Foo'
Yes.

My game programming tutorials prove that.

You just have to use a cross platform library API like SDL, OpenGL, HawkNL, wxWidgets, etc.
On a side note.. thanks for the great tutorials Lazy Foo
(i didn't realize you were on LQ)

I have been having a go at some of these at work on XP (when the boss isn't looking), and they work great both at work and on my Linux box at home.

Good stuff.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help!! Networking Programming in C (Windows/Linux) vibhory2j Programming 11 02-24-2007 05:46 AM
Programming in C++ in Linux vs. Windows Hellbreather Programming 6 01-18-2006 11:28 AM
Systems Programming - Linux vs Windows dipperdan Programming 8 09-30-2004 08:18 AM
Linux Programming vs. Windows LinuxBlackBox Programming 7 10-28-2003 09:03 AM
Exp. Windows/Linux-Script Programmer - Need Linux Programming Mentor! ruttiger Linux - Newbie 2 10-28-2001 11:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration