John F.
John F.

Reputation: 43

Compile Windows C console applications in Linux

Can I compile a Windows C console application (.exe) in Linux? (more specific, Ubuntu)

I heard a long time ago of cross-compilers, but I wasn't interested in them at that time.

Best regards

Upvotes: 4

Views: 3751

Answers (8)

radu florescu
radu florescu

Reputation: 4343

You can also use standard gcc in terminal of linux , you can use same programs you need only to change the std.h with unistd.h (meaning you will need to change to linux specific libraries). I have a course about linux programming at school it except it's on ubuntu and library changes for input/output and some thread related linux specific ,all it's the same

example : gcc -c myprogram.c // to compile gcc -o myprogram.o name to make lib myprogram // with arguments if there are any

Upvotes: 0

Yacoby
Yacoby

Reputation: 55435

There is this post which gives some info, as well as this duplicate question

As you are using Ubuntu, the packages you need is (I think) mingw32

apt-get install mingw32

Then compile with i586-mingw32msvc-gcc, e.g.

i586-mingw32msvc-gcc helloworld.c -o helloworld.exe

Upvotes: 15

Dan Cristoloveanu
Dan Cristoloveanu

Reputation: 2058

Cross compiling is possible. AFAIK you get a windows executable, which of course will run under windows (and possibly WINE).

There are 2 possible choices for that: mingw and cygwin.

One tutorial here. MingW cross compile page is here.

Upvotes: 0

Sajad Bahmani
Sajad Bahmani

Reputation: 17459

You can use WINE and install VS for example .Other solution use MonoDevelop.
also you cans use cross compilers like Code::Blocks

Upvotes: 0

Martin Beckett
Martin Beckett

Reputation: 96119

Do you mean can you build a windows .exe on Linux (ie develop on linux and deliver the app to Windows users?)

Or do you mean take a Windows app and recompile (port) it to Linux so it's built and runs on Linux?

Upvotes: 0

Jay
Jay

Reputation: 484

If your looking to compile a program from Linux into a Windows executable, yes cross compilers would be an option. http://www.airs.com/ian/configure/configure_5.html helped me a bit.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245389

If you're talking about a strictly ANSI C application, then you shouldn't have any problem compiling with gcc on a Linux platform.

If you're liking in to Windows System libraries, you're hosed and would have to replace those calls with their *nix System equivelants (if there are any).

Upvotes: 0

Cheeso
Cheeso

Reputation: 192417

You can if it's standard C, and doesn't use Windows libraries.

C code itself is very portable, and the standard C libraries (libc) are available pretty much everywhere. If your code does printf() and sscanf() and fopen() and so on, then it will just compile and run on another platform. Windows, Linux, BSD, etc.

It's the libraries other than libc that introduce portability challenges.

Anything that links with Windows-specific platform libraries is trouble. Kernel32.lib, user32.lib, etc etc.

There are third-party libs, too, that, if written in C, should be available across Linux and Windows. PCRE is a good example here - it's a Regular Expression library written in C, and it's available on Windows as well as on Linux. there are literally hundreds of libraries in this set.

If you confine yourself to libc and library calls into portable libs, then you will have a portable C application.

Upvotes: 3

Related Questions