LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-09-2008, 04:27 AM   #1
SimbaSpirit
Member
 
Registered: Feb 2008
Posts: 109

Rep: Reputation: 15
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
 
Old 09-09-2008, 04:42 AM   #2
lawrence_lee_lee
Member
 
Registered: May 2007
Posts: 141

Rep: Reputation: 16
Use g++. It is a free C++ compiler.
 
Old 09-09-2008, 04:43 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Quote:
Originally Posted by SimbaSpirit View Post
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.
 
Old 09-09-2008, 05:42 AM   #4
SimbaSpirit
Member
 
Registered: Feb 2008
Posts: 109

Original Poster
Rep: Reputation: 15
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?
 
Old 09-09-2008, 06:36 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Quote:
Originally Posted by SimbaSpirit View Post
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.
 
Old 09-09-2008, 06:44 AM   #6
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
But why compiling linux code on a windows box? Code::blocks is available under Linux, and you can find more free open source IDE...
 
Old 09-09-2008, 09:31 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 09-09-2008, 11:47 AM   #8
keithieopia
Member
 
Registered: Feb 2005
Posts: 69

Rep: Reputation: 18
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.
 
Old 09-09-2008, 02:12 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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


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
[Compile kernel] How to compile/install the modules ? frenchn00b Linux - General 1 09-06-2009 04:18 PM
why I want to compile in my Linux? hongxing Linux - Software 7 11-30-2005 05:28 AM
linux-2.6.11.7 won't compile hubabuba Slackware 3 04-16-2005 11:44 AM
ntop compile/post-compile problem? tjb Linux - Software 3 12-28-2004 05:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration