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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-30-2005, 05:26 PM
|
#1
|
LQ Newbie
Registered: Jun 2005
Posts: 4
Rep:
|
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()?
|
|
|
06-30-2005, 06:55 PM
|
#2
|
Member
Registered: Jan 2005
Location: Australia
Distribution: Slackware Archlinux FreeBSD
Posts: 218
Rep:
|
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 
|
|
|
06-30-2005, 07:03 PM
|
#3
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
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.
|
|
|
06-30-2005, 07:44 PM
|
#4
|
Member
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217
Rep:
|
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".
|
|
|
07-01-2005, 12:56 AM
|
#5
|
Member
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410
Rep:
|
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!
|
|
|
07-03-2005, 02:38 PM
|
#6
|
LQ Newbie
Registered: Jun 2005
Posts: 4
Original Poster
Rep:
|
Thanks for the replies. As for GUI libraries for Linux, do they make applications dependent on desktop environments?
|
|
|
07-03-2005, 03:24 PM
|
#7
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696
|
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.
|
|
|
All times are GMT -5. The time now is 04:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|