Is it possible to run C code directly in the browser? - c++

Performance considerations aside, is there any known way to take existing C, C++, or Objective C code and run it directly in the browser? For example, a compiler that converts all the code into some interpreted language that can be run in the browser. Like Javascript, or Actionscript and the Flash player, or I suppose Java and the JVM.
I recognize there are higher level languages like Haxe that can be compiled to different targets. And on the other side there are projects like Cappuccino and GWT that attempt to make Javascript development more like traditional desktop development.
But I was wondering if you had an application that worked on a desktop or an existing code base done in C, C++, or Objective C could it easily be converted to a web based application?
Is there work being done on this front? Is there any practical reason to do this? Basically turn the browser into the OS?
Beside the performance issues, and the entrenchment of OS vendors, are there any technical reasons this couldn't be accomplished? Could this kind of C like code be shoehorned into a virtual machine hosted in a browser?

Google's Native Client (NaCl) uses a tweaked compiler to create x86 object code that can be verified by the browser and run in a sandbox, without a major performance hit - pretty cool stuff. They've compiled Quake under it.
This Matasano article has a good run-down on how it works.

Here is a C compiler which targets a number of other languages, including Javascript:
http://cowlark.com/clue/
Not sure what state it's in - last I spoke to the author, it handled pure C89 (subject to the limitations of the compiler frontend). AFAIK there are no plans for it ever to support a GUI.
I was wondering if you had an application that worked on a desktop or an existing code base done in C, C++, or Objective C could it easily be converted to a web based application?
That's sort of what Silverlight is for (C# rather than Objective-C, of course), since it makes the .NET runtime available. Porting a desktop app is usually as much about the GUI as it is about the language - if you have a Cocoa app and you want to port it to another environment (whether that's a browser or Windows), then you'd need more than just an Objective-C cross-compiler, you need the Application Kit and so on. WINE being a notable counter-example, it's pretty rare for these OS-specific libraries to be available at all on other platforms, let alone efficiently. And even where they are available, there are look-and-feel and usability problems when the conventions of one UI are bolted on top of another. So people tend to either use portable frameworks to start with, or else completely rewrite the presentation layer of the app.
Basically turn the browser into the OS?
There are several projects underway to turn the browser into a fully-featured environment for applications (not sure whether or not this is what you mean by "OS"). Flash and AIR, Silverlight, HTML 5. None of them plan to provide C as a programming language, as far as I'm aware.

Emscripten allows you to compile your code into javascript, which is then platform and browser independent.

I think the closest thing you are looking for is Google Native Client. It is still in early development stages though.

You may be interested in LLVM, the Low Level Virtual Machine. It would be possible to implement an LLVM inside a Java applet, Flash applet, or even in Javascript (I wouldn't be surprised if somebody hasn't already done some or all of the above).
Converting an existing application is a completely different kettle of fish, however. The paradigms of user interaction are so completely different between a "desktop" app and a "browser" app that a lot of it will have to be redesigned before a port is reasonable.

Check out Adobe Alchemy (formerly known as FlaCC), which uses LLVM to compile C/C++ to Flash.

This is possible with an ActiveX control, but this works only in microsoft internet explorer.

http://code.google.com/p/cibyl/wiki/Cibyl can make Java sources, so you could compile that for the Java plugin in a browser. Given that Java plugins are much less common nowadays though, you may be better off with a solution that compiles to Javascript.

It seems to me that the major challenges are not related to the language being used. I suspect C would be a very difficult language to implement in JavaScript, but it is possible.
It just seems like a bad idea to me.
First off, I would not write a desktop application in C, much less a web application. Second, web applications require a completely different architecture than desktop applications. Simply cross compiling a desktop application will not make it a web application. If it is portability you are looking for, I suggest using a high-level language targeting the JVM.

Maybe you should consider http://ideone.com for compiling c++ in the browser? You can also compile or interpret other languages, I personally use it rather for more exotic languages - I have c and c++ on my PC :)

Related

Wanted: Compiler Tool for Users of Software System

I am not sure if the title of this question gets to the point. I have written a large software system in C C++ for Windows, and want to give the users of this system the option to add compiled code to it. The user should be able to do basic stuff, and exchange data with my program.
Currently the implemented way is via DLLs. But for this, a grown up compiler is needed, and it is not as easy as I wished. Is there a tiny C compiler that can create Windows DLLs?
Another idea is the Java native interface. But this requires a complete Java system to run in the background, and it is not easy to run code in it.
Do you have any other ideas?
Any interpreted language? (TCL and Lua were designed as extension languages, but you can nearly as easily interface with any other).
How about python integration?
You could create an python interface that interfaces with your application. Python is rather easy to learn and should integrate easily with c/c++. The python documentation has an own chapter on that.
You could also use a tool like swig to generate the interface.
The advantage of this is that they wouldn't have to compile anything. They could just supply python files that could be loaded into your application and run within the software. This is a well known use for python, and something its simple syntax promotes.
As the other says you will be best of by providing an embedded language.
I would chip in for javascript and use the google v8 engine
By using javascript you get a language nearly everbody can use and program in.
There is other javascript engines you can embed like SpiderMonkey.
See this answer for what to choose.
An interpreted language is not good enough. I need speed. The software itself is an interpreted language. So I added support for the tiny C compiler. It is only C, and I do check mingw, which probably would not be as tiny as this. Thanks for all your hints.
Added after several months:
I have now two tools, actually: TinyC and Python. The speed difference between those is noticable (factor 5-10), but that usually does not matter too much. Python is much easier for the user, though I managed to integrate both into the Euler GUI quite nicely.
One of the ways is to add scripting. You application can host scripting environment and expose its services there. Users would be able to execute JScript/VBScript scripts and interact with your application. The advantage is that with reasonable effort you can get the power of well known and well documented scripting languages into your application (I suppose there is even debugger for scripting there). You will be required to shape your app services as COM interfaces and scripts will be able to access them automatically using method names you assigned on C++ side.
C++, Win32 and Scripting: Quick way to add Scripting support to your applications
MSDN Entry Point - IActiveScript interface

Info on CrossPlatform

I have looked at some of the info on cross platform design. I can't find the answer to one question I have however.
What I would like to do is design my UI's in Delphi (Windows) and X-Code (Mac) with the underlying guts of the program being OS agnostic C++ code. Is this possible to achieve? I see lots of talk about Cross Platform Compilers, Frameworks and GUI tools but I really want to keep the interfaces 100% native.
Please forgive me if this an absurd question but I am relatively new to the world of programming and learning along the way. My company has an extensive catalog of Delphi applications for windows developed over the past 15 years and Delphi is where I have spent most of the last year.
If you are comfortable working with Object Pascal take a look to the FreePascal compiler and Lazarus which is a free cross-platform IDE (available for Windows, Linux, Mac OS X).
You should have a look at wxWidgets http://www.wxwidgets.org/ for programming 100%-native.
From my point of view I can't see the benefit of using two different and specific gui designers for two (or more) target platforms. It should be easy to share the code if the platform specific parts are clearly separated in your architecture. wxWidgets gives you the ability to use the same gui definition in both platforms without loosing the native character.
Personally, I wouldn't use C++ for UI design anymore. But why are you using two IDE's? I suggest to use a more abstract language (like Java with Netbeans/Eclipse or even better C#/.Net with VS/Blend) for this UI/event/interaction stuff keeping only some specific parts in an native optimized binary code.
.Net/Mono can use differnt toolkits for the ui style. Today, being "native" within the context of UX and look and feel does mean to follow system specific guidelines, I think.
This could be reached by views and adpaters (see google for some design pattern).
Look and feel is just a point of styling an app and it could be a native looking style, too. In the WPF-world handles are used only for the host window (and some legacy compatibility reasons using windows forms) but the user won't feel the differenc if you are styling your app like the former win-api-ones.
Qt has also an platform independent approach for the C++-world but is not really native. Qt draws its own controls with a native look and feel (if needed) and comes also with the option to use/share one definition of the UI between different platforms.
Your question is not absurd by any means - you're looking for the solution to a problem we've all been dealing with for many years - how to achieve cross platform compatibility and code reuse. But perhaps there are some points that you need clarification on:
As others have said, C++ is not the only 'OS agnostic' language - in fact most languages are 'OS agnostic' - what normally isn't (and cannot be) OS agnostic is the compiler, the GUI libraries etc. Pascal, C++, Python, C#, Java and many others have SDK's for various OS's, much of it open source.
If for some reason you are set on C++, check out Embarcedaro C++ - a Delphi clone using C++ instead of Object Pascal.
And IMO you should investigate Qt: http://doc.qt.nokia.com/latest/index.html - Qt is an excellent 'OS agnostic' framework that includes GUI widgets as well as many other powerful libraries that span pretty much the whole scope of modern PC computing.
Although c++ is the native language for Qt development, there are quite a few 'language bindings' available to make Qt programming possible with other languages. (varying degrees of functionality are supported depending on the binding implementation) I understand there is an Object Pascal language binding for Qt using Lazarus - I haven't tried it yet but it sounds very interesting.
Also, a word of caution: separating your GUI from the 'underlying guts' of your application such that it is OS agnostic can be quite challenging in complex applications - I think a good place to start is by reading up on the MVC paradigm.
HTH

Are there any browser-based multi-player capable libraries for C++?

I've made a bunch of games from my own homegrown C++/DirectX 2D engine. I was thinking some of them would be more fun with the introduction of multi-player and at the very least it would be easier to distribute them and get people to play if they could run in a browser.
I'm looking to port my games into a web format and I don't think there is anything I'm doing that Flash or Silverlight can't handle. However I don't know either of those so while I could learn something new it would save time and make porting easier if I could find something in C++. Does anyone know of a preferably open source, or otherwise freely available, library I could use to give myself a leg up?
I've heard of Haxe and it seems to be similar to what I want although it introduces a new language that can be converted to C++, ActionScript, etc. I'd prefer C++ so I can reuse some code without much of a fuss.
I also found something called RakNet which may only be useful as a networking layer to my existing C++/DirectX games but less useful for a browser based games. Has anyone used this with success? How was it to implement and integrate with existing projects?
The short answer is no. C++ requires the code to be compiled into a binary executable, and for various reasons, such code is not allowed to be run in the browser.
The long answer: The native client by Chromium/Google allows you to write native C++ code and run it in the browser. However, support is very limited, in the sense that almost no browser allows it (beyond some experimental nightly builds of Chromium and such), and you're likely going to face the same issues when porting C++ code to a different OS (aka, just because it's in the browser doesn't mean it's going to run on that obscure Linux OS).
If you want to port your games to the web, you options are either re-write it for the web, or wait a few years/decades for the native client to become widespread.
Funfact: Most mobile devices allow for C++ code to be run with a minimal wrapper. It's not the web, but it's an option if your goal is to get more people playing your games.

Easiest way to build a cross-platform application

I have read a few articles in the cross-platform tag. However, as I'm starting a fresh application (mostly a terminal/console app), I'm wondering about the easiest way to make it cross-platform (i.e. working for Linux, Mac OS X, and Windows). I have thought about the following:
adding various macro/tags in my code to build different binary executables for each operating system
use Qt platform to develop a cross-functional app (although the GUI and platform component would add more development time as I'm not familiar with Qt)
Your thoughts? Thanks in advance for your contribution!
Edit: Sounds like there are a lot of popular responses on Java and Qt. What are the tradeoffs between these two while we're at it?
Do not go the first way. You'll encounter a lot of problems that are already solved for you by numerous tools.
Qt is an excellent choice if you definitely want C++. In fact, it will speed up development even if you aren't familiar with it, as it has excellent documentation and is easy to use. The good part about it is that it isn't just a GUI framework, but also networking, XML, I/O and lots of other stuff you'll probably need.
If not necessary C++, I'd go with Java. C++ is far too low level language for most applications. Debugging memory management and corrupt stacks can be a nightmare.
To your edited question:
The obvious one: Java has garbage collection, C++ doesn't. It means no memory leaks in Java (unless you count possible bugs in JVM), no need to worry about dangling pointers and such.
Another obvious one: it is extremely easy to use platform-dependent code in C++ using #ifdefs. In Java it is a real pain. There is JNI but it isn't easy to use at all.
Java has very extensive support of exceptions. While C++ has exceptions too, Qt doesn't use them, and some things that generate exceptions in Java will leave you with corrupt memory and crashes in C++ (think buffer overflows).
"Write once, run everywhere." Recompiling C++ program for many platforms can be daunting. Java programs don't need to be recompiled.
It is open to debate, but I think Java has more extensive and well-defined library. The abstraction level is generally higher, the interfaces are cleaner. And it supports more useful things, like XML schemas and such. I can't think of a feature that is present in Qt, but absent in Java. Maybe multimedia or something, I'm not sure.
Both languages are very fast nowadays, so performance is usually not an issue, but Java can be a real memory hog. Not extremely important on modern hardware too, but still.
The least obvious one: C++ can be more portable than Java. One example is FreeBSD OS which had very poor support for Java some time ago (don't know if it is still the case). C++/Qt works perfectly there. If you plan on supporting a wide range of Unix systems, C++ may be a better choice.
Use Java. As much bashing as it gets/used to get, it's the best thing to get stuff working across any platform. Sure, you will still need to handle external OS related functions you may be using, but it's much better than using anything else.
Apart from Java, there are a few things you can run on the JVM - JRuby, Jython, Scala come to mind.
You could also write with the scripting languages directly( Ruby, Python, etc ).
C/C++ is best left for applications that demand complete memory control and high controllability.
I'd go with the QT (or some other framework) option. If you went with the first you'd find it considerably harder. After all, you have to know what to put into the various conditionally compiled sections for all the platforms you're targeting.
I would suggest using a technology designed for cross-platform application development. Here are two technologies I know of that -- as long as you read the documentation and use the features properly -- you can build the application to run on all 3 platforms:
Java
XULRunner (Mozilla's Development Platform)
Of course, there is always the web. I mostly use web applications not just for their portability, but also because they run on my Windows PC, my Ubuntu computer, and my Mac.
We mainly build web applications because the web is the future. Local applications are viewed in my organization as mostly outdated, unless there is of course some feature or technology the web doesn't yet support that holds that application back from being fully web-based.
I would also suggest Github's electron which allows to build cross platform desktop applications using NodeJs and the Google's Chromium. The only drawback for this method is that an electron application run much slower than a native C++ application due to the abstraction layers between Javascript and native C++.
If you're making a console app, you should be able to use the same source for all three platforms if you stick to the functions defined in the POSIX libraries. Setting up your build environment is the most complicated part, especially if you want to be able to build for multiple platforms out of the same source tree.
I'd say if you really want to use C++, QT is the easiest way for cross-platform application, I found myself using QT when I need an UI even though QT has a large set of library which makes pretty much everything easier in C++.
If you don't want to use QT then you need a good design and a lot of abstraction to make cross-platfform application.
However I'm using more and more Python bindinq to QT for medium size application.
If you are working on a console application and you know a bit of python, you might find Python scripting much more comfortable than C++. It keeps the time comsuming stuff away to be able to focus on your application.

C/C++ for Core Logic Development of a Web Application?

Can C/C++ be choice of keeping all your logic (business/domain) for web application?
Why?
I've two resources (cousins) having knowledge on C/C++ and me also good in C/C++, Python, HTML, CSS and JavaScript.
We like to utilize our free time to work on our some good ideas we developed together. The ideas require knowledge of web application development. And I'm the only one who has it.
Is there a way they developed the core in C/C++ and I do the rest of scripting for front-end development?
Thanks.
C/C++ and Python can be integrated fairly easily, but Python really should be a snap for anyone that knows C well to pick up in a week.
I don't think you should use a compiled language (at least not c++) for web programming. I thought about doing this once too but remember that for any change you'll have to compile etc.
Facebook uses php and it's hip hop application changes php into c++. Maybe you should take a look at that.
Of course c++ (or any compiled language) will be faster than an interpreted language, but you also have to take in account the development time of the web site/app.
Hope this helps :)
cython http://wwww.cython.org is a nice way to interface python and C/C++