Compile On Mac For Linux



  1. Compile On Mac For Linux Virtualbox
  2. Linux Development On Mac
  3. Run Mac App On Linux

So in this article, we have listed some best IDE for C and C which can be installed on different platforms like Linux, Windows, and Mac. The below given are the best C IDE. Secret: One of them is an Online C IDE to compile CPP Online. 1) CLION – Best C IDE for Mac One of the famous company JetBrains has developed Clion. Compile and run C# in the Command Line (Linux, Mac & Windows) Published in by Luis Lopez Last semester at my uni I had a subject called Object Oriented Programming where our teacher told us we were going to be using C# (C Sharp) as our language for the class. This article is intended primarily for students leaning C for the first time on a Mac.It’s not a step-by-step tutorial on how to write and compile code in the applications described.

  • You don't have to compile Mac-on-Linux (unless you want to). But you also can't run it under MacOS. You put Linux on your Mac and then run MacOS X on top of that. The reason why you have to go this way is because there are so many people who have detailed knowledge of the Linux kernel and have written similar programs for the PC.
  • Cross compiling Rust on Mac OS for an ARM Linux router. Wanting to compile a small program I’d written in Rust to run on my home router, I found this guide to cross compilation of Rust code. The router is a Netgear R7000 with an ARM processor, running FreshTomato, a distribution of Linux for ARM and MIPS architecture consumer routers.

tl;dr: you can install cross-compiler toolchains to compile C/C++ for Windows or Linux from macOS with these two Homebrew Formulas.

Cross-compiling C and C++ is dreadful.

While in Go you just need to set an environment variable, for C you need a whole separate toolchain, that might require an intermediate toolchain to build, and you need to know what you are targeting very well.

musl-cross-make

Thankfully, Rich Felker built a Makefile set to build musl-based cross-compilers, musl-cross-make. It took a few patches, but it runs well on macOS.

musl-cross-make builds beautifully self-contained cross-compilers, so you don't have to worry about pointing to the right libraries path or about where you keep the toolchain. Also, it can target Linux running on a number of different architectures.

Maybe most importantly, it's based on the musl C standard library. This means that the binaries will only run on a musl-based system, like Alpine. However, if you build them as static binaries by passing -static as a LDFLAG they will run anywhere, including in scratch Docker containers. musl is specifically engineered to support fully static binaries, which is not recommended with glibc.

homebrew-musl-cross

Still, I'm a big Homebrew fan. It lets you build software in a well defined sandbox, and only the binaries are linked into your PATH, GNU Stow style. Also, it manages resources and offers powerful dev tools.

So, I wrapped up musl-cross-make in a Homebrew Formula, FiloSottile/homebrew-musl-cross. It takes a long time to build, but it generates a full cross-compiler toolchain, and links into /usr/local/bin just the prefixed binaries, like x86_64-linux-musl-gcc.

It comes with a precompiled Homebrew Bottle for High Sierra, so if you want to build everything from source use brew install --build-from-source.

Other architectures are supported. For example to get a Raspberry Pi cross-compiler use:

You can also use --with-i486 (x86 32-bit), --with-aarch64 (ARM 64-bit), --with-arm (ARM soft-float) and --with-mips.

Compile On Mac For Linux Virtualbox

Using this with Go and Rust

To cross-compile cgo projects you can set the CC and CXX environment flags when building to x86_64-linux-musl-gcc and x86_64-linux-musl-g++ (or corresponding), on top of the usual GOOS and GOARCH.

To use this toolchain as the target linker for Rust cross-compilation, add lines like these to your .cargo/config:

A more complete guide to Rust cross-compilation is here.

mingw-w64

For Windows, there is now a Mingw-w64 Formula directly in homebrew-core, so you can install it simply with brew install mingw-w64.

The resulting GCC toolchain has prefixes x86_64-w64-mingw32- and i686-w64-mingw32-.

If you find cross-compilation more fun than it probably is, you might want to follow me on Twitter.

Last semester at my uni I had a subject called Object Oriented Programming where our teacher told us we were going to be using C# (C Sharp) as our language for the class. It meant that we had to find a way to install Visual Studio and get things done but the main problem was with those who had a Mac or me, the only one who dared to have a fresh Elementary OS installation (dual booted with Windows 10) into class. For the Mac people, it meant that they didn’t know how what tool to use apart from X-Code which of course didn’t work (but it did for C++) and me, it meant that I had to find a way to compile my code written in mere text editors like Atom or Sublime.

Thankfully I found three ways I could catch up with my classmates during our lab classes: REPL.it (an online tool for writing in a plethora of languages and seeing the STDOUT result), the CSC (C-Sharp compiler) command line tool in Windows and Mono (compiled from the Terminal or with MonoDevelop which is a cross-platform IDE).

Sponsored by Microsoft, Mono is an open-source implementation of Microsoft’s .NET Framework based on the ECMA standards for C# and the Common Language Runtime. A growing family of solutions and an active and enthusiastic contributing community is helping position Mono to become the leading choice for development of cross platform applications. - Mono’s Website

Compiling C# on Windows

Since this is Windows we’re talking about, you probably have the .NET Framework already installed on your computer; the usual path to it is C:/Windows/Microsoft.NET/Framework, inside you’ll see a bunch of folders with version names. Navigate to the version number that you want to work with (I always choose 4.x) and copy the absolute path of the folder by clicking on the address bar and copying its contents.

Now, you have to edit the PATH environment variable and add the directory address you just copied. This is easy on Windows 7+ because all you have to do is open the search box and typing “variable”, you’ll see something like “Edit System Environment Variables”, click on it and you’ll see something like this:

Click on PATH (sometimes it’s “Path”) and click on the Edit button, in the “Variable Value” text box, at the very end, if it doesn’t end with a semicolon, insert one and paste the address you copied.

Hit OK on both windows, exit the system configuration dialogs and then open the command line (Win + R and type cmd, then click on Run) and type in csc, if it says that the command isn’t recognized, you’re doing things wrong. Install the .NET Framework if you haven’t, localize the direct path to the latest version where the csc.exe executable is and copy the path, then follow the PATH variable instructions here from HowToGeek. Otherwise, you’ll be welcomed with the Microsoft C# Compiler and it’s copyright details.

Now, create a C# test file, it can be this:

Run the following command in the same directory as the file:

Once you fix any compilation warnings and errors that the compiler may throw at you, you won’t see any output but it means that you can now run the code by typing the name of the executable (adding .exe is optional), hit ENTER and you’ll see your console output! CSC has some options that you can use, for example, the file wildcard for compiling more than one C# files into a single executable by substituting source_file.cs with *.cs, also, refer to the documentation.

Compiling and running C# on Linux and Mac

You can run a GUI application and a console app using Mono, but to actually run ASP .NET web applications you need more than just Mono, you need the K Runtime Environment, I’m sorry I can’t help you with that (I haven’t even bothered to learn ASP .NET ─yet─) but I can refer you to a good and quick tutorial to get install it and get a .NET application running and its dependencies installed; click here for the TechRepublic tutorial. I also saw .NET Core being suggested so you may want to check that out.

Step 1: Install Mono

For Mac users, I won’t say more than this: all you have to do is download the .pkg file, skip to the next step and get going. Now, for the Linux users, you can go here and follow the instructions which I’m going to mention next anyway. Note that the instructions I’ll paste are valid for Ubuntu and Debian based distros (I’m using Elementary OS so, I had to choose this) but if you’re running CentOS, Fedora (or any of its derivatives) or OpenSUSE and SLES you better click on the link I mentioned a couple of sentences ago.

Linux download mac

Open up your terminal window and type in the following command, which is going to add a PPA key, you don’t need to worry about what this means if you are new to package management in Linux:

The following command will append a new line to our source list file so that apt-get can find the package instead of outputting a message saying it couldn’t find it:

Now, by rule of thumb, every time we modify the source list or intend to install a new package, we have to run a command to download the package lists from the repositories and update them to the latest versions (it will probably ask for your user password, type it in and hit ENTER):

Finally, it’s time to install Mono itself and all its dependencies, when you run the command you’ll see that it will ask you to download a ton of packages, it’s ok. What we’re doing here is tell apt-get to retrieve and install all of the packages mentioned; this process is detailed here.

Step 2: Compiling and running C# code

Compile

To make sure mono is working correctly, why don’t we set an example console STDOUT application? A simple “Hello World” will do the trick. Open the terminal and navigate to any directory you want, then create a file called hello.cs by typing touch hello.cs or editing directly into VIM or Nano and then save the file. Here are the contents of said file:

Now, save it and in the terminal window (making sure you’re still in the same directory as the C# file), execute the Mono compiler; now, there are three compilers that you should be aware of according to the Documentation:

  • mcs: references the 4.0-profile libraries (the APIs as defined in .NET 4.0) and supports C# 4.0.
  • gmcs: references the 2.0-profile libraries (the APIs as defined in .NET 2.0 and .NET 3.5) and exposes the full C# 3.0 language.
  • smcs: references the 2.1-profile libraries (the APIs defined for Silverlight) and exposes the full C# 3.0 language. This is the compiler used for creating Silverlight/Moonlight applications.

dmcs has now been deprecated and the compiler tells us to use mcs instead, so this is the one I’m using!

This command will use the mcs compiler and create a Windows executable with the name of “hello”, taking from source hello.cs. If the compiler has no warnings or errors to tell you about, you won’t see any output, why don’t you try to remove the semicolon from line 5 on the file? It will output something like this:

It’s telling us that there’s an error on line 6, column 3 (it says 6 and not 5 because of the missing semicolon, it thinks the line continues up to the curly brace) and the message is ; expected which is pretty self-explanatory. If you want to see all the options available in the compiler, why don’t you head up to the official documentation? One last tip, if you want to compile all the files in a folder you can use the wildcard mcs -out:my-program.exe *.cs asterisk.

But what if I want to compile a Windows desktop app with GUI that I built with Windows Forms? Then you’d have reference the appropriate DLLs and then run the following command:

Note that you can also start building with GTK, refer to the documentation for how to build GUIs with GTK using Mono.

Time to run the code

Linux

Linux Development On Mac

The mono command will allow you to execute the code we just created in the terminal or even a desktop GUI app (for web apps, that’s a whole different shabang). Open the terminal if you closed it by accident and navigate to the directory where the C# file was. Now, execute the following command:

You’ll see that the output is indeed “Hello from KozmicLuis!”:

Enter OmniSharp and MonoDevelop

For

OmniSharp is a server that will enable your code editor to have the properties of a C# IDE (still not comparable to a real IDE though) and you can also install Yeoman to use some pretty neat project generators (scaffolding). For SublimeText you can go ahead and use the package installer and fetch OmniSharp. If you’re running Atom (the experience is more pleasant here) you can install the package with the same name omnisharp-atom (via the app or using apm). For more information go to the official website or watch this video. For Omnisharp to work you need Mono 3 dot something and bigger, I am running the 4th version and still couldn’t get it to work on Elementary OS, I’m not sure if I’m stupid or if I am missing something but I really hope you can get it to work because it’s a blessing because it provides IntelliSense, errors and warnings, code autocompletion and more.

All due image credits go to Quique Fernández at EsMSDN’s blog.

What about the IDEs?

Run Mac App On Linux

Linux

As I mentioned somewhere in this tutorial, you can install two IDEs that are cross-platform and compatible with Mono. MonoDevelop works with C# and F# (functional programming language by Microsoft) and can be installed on the 3 major platforms.

And Xamarin is a mobile .NET platform, you can create Android, iOS and Windows Apps with it using C#.

Also, if you’re on Windows and can’t afford Visual Studio Professional, you can always rely on Visual Studio Community. Another good editor you can use is called VS Code, which can make your transition a bit more comfortable.

Conclusion

Whatever method or platform you use, make sure it’s what you need, I’m still very new to C# and I haven’t even looked into what ASP .NET has to offer because I’m too tangled into the cool kids’ stacks like MERN (MongoDB, Express, ReactJS, and NodeJS) or LEPP (Linux, Nginx, PostgreSQL, and PHP).

Did you know that there’s a C# interactive console? I didn’t, I just found out today while writing this thing. Type csharp in the terminal if you have Mono installed and enjoy!

Optional: Build with Sublime Text

I almost forgot! You can take advantage of Sublime Text’s builds, they are just shell commands you can execute from within ST by running the CTRL/CMD + B key combination (or going to Tools > Build).

To get started, go to the Tools menu and hit the Build System submenu, then click on New Build System.... You will be received with a new file that looks like a JSON literal, change the content of that file with this:

For Linux and MacOS
For Windows Machines

Now, save the file with the name of csharp.sublime-build in the suggested directory and now go to a c# file with Sublime Text, navigate to the Tools > Build submenu and click the “csharp” checkmark, now when you execute the Build command (CTRL/CMD + B) you’ll see a mini window down below like this:

There’s also a plugin called Script! for the Atom editor but it’s harder to configure as it uses csc as its default command for executing C# code.

Comments:

Please enable JavaScript to view the comments powered by Disqus.