Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-30-2005, 05:26 PM   #1
LQ Newbie
Registered: Jun 2005
Posts: 4

Rep: Reputation: 0
cross platform c++

Hi I'm new here, it looks like a really nice forum.

I come from a Windows background, and want to start developing programs for Linux. I haven't been able to find any "hello world" tutorials, so i was hoping someone could help me.

#include <stdio.h>
int main() {
printf("hello world");
return 0;

How would i go about making this code compile and run for Linux? I tried compiling it as a .exe file using MS VC++ under Windows, but it didn't do anything when i clicked on the file (using KDE on MEPIS liveCD, reading from a WinXP NTFS partition).

Are there compilers specific to Linux or do I have to change my code in some way ? What about non-console programs? What is the equivalent of WinMain()?
Old 06-30-2005, 06:55 PM   #2
Registered: Jan 2005
Location: Australia
Distribution: Slackware Archlinux FreeBSD
Posts: 218

Rep: Reputation: 30
I can not offer any advice on this, but I would like to welcome you to Linuxquestions. I will follow your post with interest.

Old 06-30-2005, 07:03 PM   #3
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
There must be a million Hello World c++ tutorials online but anyway...

*saves as helloworld.cpp*
*at the command line runs g++ ./helloworld.cpp -o helloworld*
*./helloworld, gets hello world echoed back at the prompt*

If you can do that then you're well on your way to being able to try out C++ development in linux.

Remember linux executables dont have the .exe filename extention of Windows, but simply have their executable permission bit set.

I've never developed in Windows, and am currently improving my C++ on linux at uni, so I dunno what WinMain is, but as for GUI libraries for linux there's Qt, Gtk, WxWidgets...

Last edited by Proud; 06-30-2005 at 07:11 PM.
Old 06-30-2005, 07:44 PM   #4
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Windows .exe binaries are completely different from and incompatible with Linux (a.out or ELF) binaries. The extension doesn't matter, it's the content of the file that is important. If you want a program to run under a particular platform, you have to compile it with that platform as the target. Unless you have a cross-compiler, this means compiling on the same kind of platform that you want to run.

In any case, the GNU C compiler (GCC) is vital to and is present on any decent Linux system. Try "man gcc" for some information overload.

Also remember to look into the GNU make utility (man make) for any projects more complicated than "hello world".
Old 07-01-2005, 12:56 AM   #5
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Well, first of all I think that this is C (and no C++) code:

#include <stdio.h>
int main() {
  printf("hello world");
  return 0;
so It should be compiled with

gcc program_name.c -o progam_name
It's true that the code is also C++ code, but if you want it to be compiled as C++ code, then it will not work. You will need this kind of modifications (IIRC):

#include <cstdio>

int main() {
  printf("hello world");
  return 0;

#include <iostream>
using namespace std;

int main() {
  cout << "hello world" << endl;
  return 0;
and compile it with:

g++ program_name.cpp -o progam_name

In any case, the result is a file called "program_name" that you can run: by clicking on it or by executing it from the CLI. In that case you'll likely need to add "./" before the name of the file.

About non console programs, the same history applies. You have to compile them under Linux with a proper compiler, but since Windows GUI programs are based on the Windows API, which is not the same that Linux, they won't work anyway. If you want to write GUI programs for Linux, you'll need to learn one of the --many-- libraries available to do that. Some of them have been ported to Windows, in the case you are interested to develop for both platforms.

Old 07-03-2005, 02:38 PM   #6
LQ Newbie
Registered: Jun 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the replies. As for GUI libraries for Linux, do they make applications dependent on desktop environments?
Old 07-03-2005, 03:24 PM   #7
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
No, the apps only need the right library. Also, the most popular libraries (Qt and GTK) are cross-platform, so if you don't make your program system-dependant in other things, it should compile on any platform the library is available for.


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
Best cross platform ide? bobwall Programming 3 11-10-2011 03:16 PM
cross platform filesystem z9_87 Linux - Hardware 5 03-20-2005 06:49 PM
Cross-Platform LibAO Ross Clement Programming 0 03-05-2005 05:19 AM
Cross Platform Shell narusegawa Linux - Software 2 06-24-2004 08:50 AM
Cross Platform biggiefatts Linux - Software 4 06-04-2002 03:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 PM.

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