0

I'm working on Ubuntu and write normal c++ program.

compile program using gcc.

$ gcc test.cpp

I want to distribute my program as application.when i double click on a.out file.it's work on my PC but not work on other(Linux) PC.

My question is :


  1. GCC require on client PC to execute application ?

  2. how can i build executable file for all platform(Win,Linux,OS x) ?

2
  • You should really be calling g++. Commented Dec 26, 2013 at 10:19
  • 1
    You don't need GCC on client, but you must compile for the target architecture and OS on the client. Executables are not in general portable between platforms, you have to rebuild for each target. Commented Dec 26, 2013 at 11:25

4 Answers 4

2

You are trying to compile a Windows PE file on a Linux environment?

If so, you will need the mingW tool-chaining

See here http://sourceforge.net/projects/mingw-w64/ (But since the mingw-w64 project on sourceforge.net is moving to mingw-w64.org i suggest also to check mingw-w64.org)

Sign up to request clarification or add additional context in comments.

Comments

1

The Windows and Linux runtimes are different. You will need to recompile your code on your target architecture. If you compiled your code on one linux distro, there's a chance that you may be able to run it on another, given there are no library conflicts, and that the architecture is the same. You can use the -static flag to avoid possible issues with shared libraries. A cross-compiler toolchain is what you need if you have to compile your code on one machine that targets another machine. To give a small example, if you wanted your code to run on an embedded system, you would need to port OS-specific functions using something like newlib. Because Linux distros all use the same kernel, this is not an issue. Clearly, though, Windows and Linux are completely different.

Comments

1

Answer to #1. No. But the target platform needs the same (or compatible) dynamic libraries the application expects. If in doubt, compile it with static link. It may help you on running the same code on different Linux distros.

gcc -static -o your_exe_file test.cpp

Answer to #2. Several options:

  • Write portable code and compile it with a C compiler for every platform you want to deploy to. You will end up with three executables: one for Linux, another for Windows, and another one for OS X.

  • Release the source code and let the user to do the compiling stuff.

  • Use a language such as Java, Python, C# or any other that doesn't need a recompilation to run on several platforms.

Comments

0

Additional note:

Use g++ to compile a C++ sources (gcc is for plain C and doesn't include C++ standard library).
If you want to use gcc (strongly discouraged), then link the source file against the standard C++ library.

gcc source.cpp -lstdc++

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.