Gaurav Marsoniya
Gaurav Marsoniya

Reputation: 11

how to build executable file for cpp programm

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

Upvotes: 0

Views: 249

Answers (4)

Noam Rathaus
Noam Rathaus

Reputation: 5588

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)

Upvotes: 2

mcleod_ideafix
mcleod_ideafix

Reputation: 11418

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.

Upvotes: 1

user1508519
user1508519

Reputation:

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.

Upvotes: 1

Matteo Pacini
Matteo Pacini

Reputation: 22846

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

Upvotes: 0

Related Questions