LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 06-30-2005, 05:26 PM   #1
nyomon
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
aikidoist72
Member
 
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.

Cheers
 
Old 06-30-2005, 07:03 PM   #3
Proud
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
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
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
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

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

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

Code:
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):

Code:
#include <cstdio>

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

Code:
#include <iostream>
using namespace std;

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

Code:
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.

HTH!
 
Old 07-03-2005, 02:38 PM   #6
nyomon
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
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
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.
 
  


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
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


All times are GMT -5. The time now is 09:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration