LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I compile c++ for Linux? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-compile-c-for-linux-668573/)

SimbaSpirit 09-09-2008 04:27 AM

How do I compile c++ for Linux?
 
I'm a newbie learning c++ and it's quite addicting...

However, due to some programs not yet working in *nix (3dsmax etc), I'm stuck with Vista on my laptop.
I'm programming using Code::Blocks, and when I compile it exports as .exe.

I don't recall Ubuntu being able to handle .exe files, so how do I compile my c++ programs such that I can run them on Ubuntu? Bonus points if one file runs on both :P

Thanks,
-SimbaSpirit

lawrence_lee_lee 09-09-2008 04:42 AM

Use g++. It is a free C++ compiler.

i92guboj 09-09-2008 04:43 AM

Quote:

Originally Posted by SimbaSpirit (Post 3274213)
I'm a newbie learning c++ and it's quite addicting...

However, due to some programs not yet working in *nix (3dsmax etc), I'm stuck with Vista on my laptop.
I'm programming using Code::Blocks, and when I compile it exports as .exe.

I don't recall Ubuntu being able to handle .exe files, so how do I compile my c++ programs such that I can run them on Ubuntu? Bonus points if one file runs on both :P

Thanks,
-SimbaSpirit

It's not just ubuntu. Linux itself (any distro) can't run windows .exe files. Binary files for windows run only in windows, just like binary files for linux run only in linux, Mac OSX programs run only in OSX, and SiperNintendo ROMS run only in supernintendo.

If we leave outside emulators and similar software.

To install the c++ compiler, you need to install gcc, and probably g++, depending on the distro. To compile a c++ program, you'd do

Code:

g++ file.cpp -o progname
If you don't like command line, code::blocks is also available for linux, and there are many more. They all are just frontends to g++ though. You still need it.

SimbaSpirit 09-09-2008 05:42 AM

I remember telling it to use gcc for compilation, and I know that linux can't run .exe files ( as i mentioned earlier ). The question is, how do I make the code::blocks that is running on my WINDOWS machine compile my program so that it can be run on a LINUX machine?

i92guboj 09-09-2008 06:36 AM

Quote:

Originally Posted by SimbaSpirit (Post 3274301)
I remember telling it to use gcc for compilation, and I know that linux can't run .exe files ( as i mentioned earlier ). The question is, how do I make the code::blocks that is running on my WINDOWS machine compile my program so that it can be run on a LINUX machine?

I see. There's no simple way around that. If you really want to use windows to compile linux stuff you will need to setup a cross-compiling environment. Or, alternatively, install cygwin and use gcc inside cygwin.

I can't really help you with either of these, other than pointing the options as I did.

jf.argentino 09-09-2008 06:44 AM

But why compiling linux code on a windows box? Code::blocks is available under Linux, and you can find more free open source IDE...

matthewg42 09-09-2008 09:31 AM

The GNU compiler can do cross compilation - compiling progams on one system which are to run on another system type. It is possible to build windows .exe files with a compiler running on Linux. However, it's fiddly. Don't expect that you can change a single setting in a Makefile and that will be all that is needed.

On Ubuntu, the packages you need to do this are mingw32, mingw32-binutils and mingw32-runtime.

It may be possible to do the reverse, running the compiler on a Windows platform, but I have never tried it.

Like I said though, it's no walk in the park.

keithieopia 09-09-2008 11:47 AM

To elaborate on what the last poster stated: I've run it on windows, so I can confirm that you can do the same (or really vice versa) on windows also.

Also, as stated you'll need to make sure you're computers are using cross compliant code. For instance, if the application in question is a GUI use GTK instead of MFC's builtin controls.

It's a pain in the rear, but it's a good practice to get use to.

matthewg42 09-09-2008 02:12 PM

If you build your program using a decently cross-platform like QT, the amount of platform conditional sections will be minimal. There is still likely to be the odd section of code which must be platform specific however.


All times are GMT -5. The time now is 10:25 PM.