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.
Related
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).
I'm trying to compile the C program tsschecker for Windows(on my Mac). I installed mingw-w64 with brew install mingw-w64, but I'm not sure how to use it to compile the program for Windows.
The Github repo for tsschecker includes instructions to compile for macOS and Linux(using make), but not Windows(but it includes Windows in the compatibility list).
I have access to a Windows machine if needed.
How can I compile tsschecker for Windows?
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.
I just tried to write a simple C program on OSX Lion
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
Compiling with gcc
$ gcc hello.c
test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
Ok...fine
$ gcc -I /Developer/SDKs/MacOSX10.6.sdk/usr/include
ld: library not found for -lcrt1.10.6.o
collect2: ld returned 1 exit status
What? Let's see...
$ gcc -I /Developer/SDKs/MacOSX10.6.sdk/usr/include -L/Developer/SDKs/MacOSX10.6.sdk/usr/lib test.c
$ ./a.out
hello world
Finally!
That seems like a lot of effort just to get a hello world working, how do I make gcc find libraries and header files in the MacOSX10.6.sdk directory by default like it does on linux? On linux I find ldd and ldconfig quite useful, they don't seem to exist on OSX...is there an equivalent? What other useful tools are there for developing C on OSX?
Yes, I know xcode makes this easier, but suppose I wanted to use vim and the command line to work on an opensource c project.
I can think of three possibilities:
You had 10.6 installed and upgraded to 10.7. You need to install the new version of Xcode (4.1, get it from the app store) to get back the developer tools.
you installed Xcode, did a custom install, and unchecked "UNIX development" or something. Rerun the installer and install the missing parts.
You installed a broken third-party version of gcc. Try which gcc and see what you get.
On OS X 10.7, if you have downloaded Xcode 4.1 from the Mac App Store and then run the installer that it downloads (in /Applications), your first attempt should have worked just fine assuming you have not set some environment variables that are looked at and influence Apple's gcc tool chain. The object file that is produced in this case would be using the default 10.7 ABI (include files and libs). If you want to produce something that would be compatible with OS X 10.6, then you need to tell the tool chain to use the 10.6 SDK ABI, which is what you did in the third attempt. A standalone project typically handles this by setting up a Makefile to automate building. But, unless you are trying to build something on a version of OS X (10.7 here) that will also run on earlier versions of OS X (say 10.6), there is generally no need to use an SDK.
How do I compile a .c file on my Mac?
You'll need to get a compiler. The easiest way is probably to install XCode development environment from the CDs/DVDs you got with your Mac, which will give you gcc.
Then you should be able compile it like
gcc -o mybinaryfile mysourcefile.c
You will need to install the Apple Developer Tools. Once you have done that, the easiest thing is to either use the Xcode IDE or use gcc, or nowadays better cc (the clang LLVM compiler), from the command line.
According to Apple's site, the latest version of Xcode (3.2.1) only runs on Snow Leopard (10.6) so if you have an earlier version of OS X you will need to use an older version of Xcode. Your Mac should have come with a Developer Tools DVD which will contain a version that should run on your system. Also, the Apple Developer Tools site still has older versions available for download. Xcode 3.1.4 should run on Leopard (10.5).
In 2017, this will do it:
cc myfile.c
Just for the record in modern times,
for 2017 !
1 - Just have updated Xcode on your machine as you normally do
2 - Open terminal and
$ xcode-select --install
it will perform a short install of a minute or two.
3 - Launch Xcode. "New" "Project" ... you have to choose "Command line tool"
Note - confusingly this is under the "macOS" tab.
Select "C" language on the next screen...
4- You'll be asked to save the project somewhere on your desktop. The name you give the project here is just the name of the folder that will hold the project. It does not have any importance in the actual software.
5 - You're golden! You can now enjoy c with Mac and Xcode.
You can use gcc, in Terminal, by doing gcc -c tat.c -o tst
however, it doesn't come installed by default. You have to install the XCode package from tour install disc or download from http://developer.apple.com
Here is where to download past developer tools from, which includes XCode 3.1, 3.0, 2.5 ...
http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.3.3.3.1
Ondrasej is the "most right" here, IMO.
There are also gui-er ways to do it, without resorting to Xcode. I like TryC.
Mac OS X includes Developer Tools, a developing environment for making
Macintosh applications. However, if someone wants to study programming
using C, Xcode is too big and too complicated for beginners, to write
a small sample program. TryC is very suitable for beginners.
You don't need to launch a huge Xcode application, or type unfamiliar
commands in Terminal. Using TryC, you can write, compile and run a C,
C++ and Ruby program just like TextEdit. It's only available to
compile one source code file but it's enough for trying sample
programs.
Use the gcc compiler. This assumes that you have the developer tools installed.
STEP 1
Just check wheater your MacBook has the compiler or not using this command 👉🏻 clang --version in your command line interface. If the tool exists then you will be able to see the version like this
STEP 2
Next, go to the directory where your source code exists using CMD Interface, then run the command make "filename" without the .c extension.
STEP 3
The final command to run your source code after compiling it is ./filename without the .c extension.
This is how you can compile and run your program on the Macintosh system.