Run C program on CentOS written in Windows - c

I want to write a program which should be able to communicate over a network between Windows and CentOS machines.
Now, my problem is that I do not have access to a centOS (6/7 x64) machine to have it done / tested. Is there a way of building it on a Windows machine so that it will be compatible on centOS too? It is going to have a very simple command line interface so there's no need for a GUI compatibility.

If you mean to compile a file on Windows and then have the executable run on Centos then the answer no (see below).
If you mean to write code on Windows that you can move to a Centos box, compile and run, then the answer is yes. To do this you need to insure that you write code that is at least posix compliant. Note that there are a number of cross-platform frameworks if you want graphical interfaces (like QT).
N.B. -- It should be possible to cross-compile if you want to be able to move the executable from a windows box to a Linux box. You will (probably) need to use gcc (mingw) as you compiler on Windows.

My ELLCC cross compilation tool chain can compile C and C++ programs targeting Linux and Windows systems on both Windows and Linux hosts. Pre-built binary packages are available for download.
It's pretty easy to use. Here is an example of building a hello world program for Linux and Windows on a Linux host:
[~/ellcc/examples/hello] dev% ~/ellcc/bin/ecc -target x86_64-linux-eng main.c
[~/ellcc/examples/hello] dev% ./a.out
hello world
[~/ellcc/examples/hello] dev% ~/ellcc/bin/ecc -target x86_64-w64-mingw32 main.c
[~/ellcc/examples/hello] dev% ./a.exe
fixme:winediag:start_process Wine Staging is a testing version containing experimental patches.
fixme:winediag:start_process Please report bugs at http://bugs.wine-staging.com (instead of winehq.org).
hello world
and the same program on a Windows system:
C:\ellcc\examples\hello>c:\ellcc\bin\ecc -target x86_64-w64-mingw32 main.c
C:\ellcc\examples\hello>.\a.exe
hello world
C:\ellcc\examples\hello>C:\ellcc\examples\hello>c:\ellcc\bin\ecc -target x86_64-w64-mingw32 main.c
C:\ellcc\examples\hello>.\a.exe
hello world
C:\ellcc\examples\hello>
ELLCC targets a variety of Linux systems including ARM, Mips, PowerPC and X86 systems as well as 32 and 64 bit Windows systems.

The best solution is probably to install CentOS on a virtual machine such as VirtualBox and use that for test and development.

Related

How do you include external libraries when you cross-compile c programs using mingw?

I am building a simple command line game in C using the ncurses library on a Linux machine but I want to be able to run the compiled code on a Windows computer. To do this, I am using the MinGW-w64 cross compiler tool in Linux and compiling it to run in a 64 bit Windows environment. However, when I try to compile using this command:
x86_64-w64-mingw32-gcc -o game.exe barebones.c -lncurses
I get this error:
barebones.c:2:10: fatal error: ncurses.h: No such file or directory
2 | #include <ncurses.h>
| ^~~~~~~~~~~
compilation terminated.
I installed ncurses on my Ubuntu machine and can create and run the same simple program to run on Linux. I have been able to cross-compile and run simple programs that only use the default libraries. I think I must be listing the ncurses library incorrectly in the compliation command or that I am failing to understand other posts that show that this doesn't work.
I am using Windows 10 and Ubuntu 21.04.
Debian provides no cross-compiling packages for ncurses. (Ubuntu provides no additions or improvements to ncurses in any way, simply reusing packages from Debian). If you want to cross-compile ncurses, you'll have to build ncurses in cross-compiling form.
For development purposes, ncurses packages can be built using the scripts under the (sources) packages directory, e.g., after downloading the current source:
tar xf ncurses-whatever.tgz
cd ncurses-whatever
cp -var packages/debian-mingw64 ./debian
dpkg-buildpackage
That's a starting point. You'd have to do something about the email in the debian/control file to appease dpkg-buildpackage (tutorials are off-topic).

How to make a Windows CLI from a Mac

Wondering how to make a command-line interface / tool on a Mac that works on Windows. I know you can create an exe from the entire node.js repo, but I'm wondering how to do this at a lower level. Writing some C library and compiling with VisualStudio perhaps. Wondering what a hello world CLI (something that just prints hello world to the terminal) would look like for Windows, when compiling/testing from a Mac.
To produce a Windows executable on a Mac you need a cross-compiler. You can use either Brew or MacPorts to install MinGW (or MinGW-w64 for x86_64 binaries) toolchain.
See "Install MinGW-w64 using Brew" or select the required port from this list.
After installing, you will have the 'i686-w64-mingw32-gcc' binary and installed C run-time library, so that you may compile your 'app.c' file with
/path/to/i686-w64-mingw32-gcc -o app.exe app.c
command.
As a starter, I would use a "Hello, World" test like
#include <stdio.h>
int main() { return printf("Hello, world!\n"); }
If your CLI tool depends on third-party libraries, you would have to compile all libraries using 'i686-w64-mingw32-gcc' compiler (this usually requires some library-specific setup in each case).
To quickly debug some platform-independent things you may either compile the same app for MacOS or run compiled .exe file with Wine emulator.

Why do I have to recompile my C program when I move it from OSX to a Ubuntu machine?

I built a bunch of simple C programs for school on my Mac (OSX). I had compiled all of the programs and tested them all on my Mac with a Makefile. Everything worked well.
To prep for an assignment tomorrow, I decided to transfer all of these files (compiled and source code) via SSH to the class network (OS is Ubuntu). I wanted to make sure everything worked as expected there.
Once I transferred everything, when I tried to use the Emacs shell to run the compiled programs, I got a Cannot execute binary file error. Then, once I recompiled via my Makefile over SSH on the Ubuntu machine, it worked fine. But why not before?
I know this is obvious to some of you, but I don't know why a compiled C program will run fine on my machine, but then have to be recompiled on a different machine even with the operating systems being different?
Here is an example of my Makefile compile commands:
example: example.c
gcc -Wall -pedantic -ansi example.c -o example
I'm pretty new to C (obviously). This question, Why does my program run on Ubuntu gcc but not OSX gcc?, seems similar but I don't understand the answer.
Like other have mentioned the C code might be compatible but Linux and OSX use different binary formats which are not compatible. Thus you will need to recompile to make your code run on the other platform.
Linux uses a binary format called ELF
OSX uses a binary format called Mach-O
See Is a Linux executable “compatible” with OS X? for a more in depth explanation.
so to add to Marius's very good explanation:
they actually use the same x86-64 (amd64) calling convention (ABI) so they are compatible on another level, deeper than just C... but they are packaged in different object file formats (as described by Marius).
The other major difference is the linker... so while they both implement std C functions, they are in different libraries so if they are dynamically linked the symbols are in the wrong place.

Is it possible to create a Window or a Linux usable native library (DLL and SO) on a Mac OSX?

I created a software that has native components and therefore requires the creation of operating system specific shared libraries (.dylib , .dll and .so ). I have a Mac OSX and I have already created a version of the software that is compatible with Mac OSX machines. I need to release versions that are compatible with other operating systems. Can I simply go on my mac terminal and write:
gcc -o c_prog.dll -shared c_prog.c
and
gcc -o libc_prog.so -shared c_prog.c
Or do I have to create the .so and .dll files on their respective operating systems?
What you suggest is NOT possible. Your compiler can create code for ONE specific target platform, specified by the "host triplet", for example on my linux machine:
> gcc -dumpmachine
x86_64-linux-gnu
Meaning of the host triplet ist $(machine)-$(vendor)-$(operatingsystem).
GCC (and probably some other compilers) can be built as a cross compiler. This is a compiler with a different target host-triplet from the machine it is running on. For my system (Debian Linux), there is a ready made package of GCC for compiling windows 64bit binaries: gcc-mingw-w64-x86-64. So in theory, you can build everything on your system. But don't forget you need the target platform's header files, the matching binutils (for things like packaging a shared library) and a linker and standard C library for the target platform. All together is called a cross toolchain.
Now you could look around for a cross toolchain to target Linux and Windows on your Mac. Maybe you will find something working. Or you could take the though path and try to compile your cross tools yourself from gcc, glibc and binutils sources.
In the end, I resorted to using a real Windows virtual machine for building my software, that I developed on Linux, for Windows. This was a lot easier :)

Cross-compiling step by step to build a simple "hello world" that run into a WM8650 Tablet from Ubuntu 12

i'm a programmer that program in windows world. With experience in Delphi, C for microcontrollers (Motorola, AVR, PIC ) and Assembler. Few months ago i begin to walk into the Linux world, working with routers (DIR 600) like an interface between my hardware (we develop embedded systems) and a web-page.
Some weeks later we start to work with tablets (Chineese cheap based on WM8650) and write some bash programs to communicate with our hardware. All fine.
Later i read that Ubuntu have insede a cross-compiler, so i was install the last version 12.04
but when i try to compile a simple "Hello world" if i use simple gcc al fine inside the ubuntu machine, when compile with the arm-linux-gnueabi-gcc and put the program into the tablet and run it this error is displaying:
Segmentation fault
i was read this link : Cross compiling static C hello world for Android using arm-linux-gnueabi-gcc
But can't understand the right answer.
If some one knows a place or doc file to solve the simple job: cross compile a C program that run into my WM8650 tablet will be appreciated.
Best regards.
Try to specify architecture or cpu like -march=armv5 in front of arm-linux-gnueabi-gcc -static [cpu/architecture] files...

Resources