LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cross platform c++ (https://www.linuxquestions.org/questions/programming-9/cross-platform-c-338864/)

nyomon 06-30-2005 05:26 PM

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()?

aikidoist72 06-30-2005 06:55 PM

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

Proud 06-30-2005 07:03 PM

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

puffinman 06-30-2005 07:44 PM

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

enemorales 07-01-2005 12:56 AM

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!

nyomon 07-03-2005 02:38 PM

Thanks for the replies. As for GUI libraries for Linux, do they make applications dependent on desktop environments?

Mara 07-03-2005 03:24 PM

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 02:15 AM.