在浏览器中运行 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8003670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:50:25  来源:igfitidea点击:

Running c++ in browser

c++html

提问by Nick

I have written some basic c++ programs in one of my classes for school. I was wondering if it was possible to somehow virtually run the program in a broswer. I would like to post the program to my website. Once its posted, a person could access the program, run the program, and, interact with the program. I'm not trying to write C++ for my website, it would be more for an interactive portfolio.

我在学校的一门课上编写了一些基本的 C++ 程序。我想知道是否有可能以某种方式在浏览器中虚拟运行该程序。我想将该程序发布到我的网站。一旦发布,一个人就可以访问该程序、运行该程序并与该程序交互。我不是要为我的网站编写 C++,它更适合用于交互式投资组合。

Is this possible?

这可能吗?

采纳答案by vz0

Use codepad, a website which lets you compile and share code online.

使用codepad,一个可以让您在线编译和共享代码的网站

#include <iostream>

int main(int argc, char** argv) {
  std::cout << "Hello, Stack Overflow!" << std::endl;
  return 0;
}

回答by parapura rajkumar

There is also Google Native Client SDKthat allows C++ code to run in browser. Microsoft Active X is also a viable option. I am only saying it is possible not recommended.

还有允许 C++ 代码在浏览器中运行的Google Native Client SDK。Microsoft Active X 也是一个可行的选择。我只是说可能不推荐。

回答by Dennis

You can only run the program on your server, not on the client's machine.

您只能在您的服务器上运行该程序,而不能在客户端的机器上运行。

At least not without downloading and manually executing it. Anything else would be an open door for malware...

至少在没有下载并手动执行它的情况下不能。其他任何事情都会为恶意软件敞开大门......

回答by Joey

I see two options, but both very overkill:

我看到两个选项,但都非常矫枉过正:

  • Write (or find) a C++ interpreter in JavaScript
  • Use a VM running an operating system (e.g. jslinuxand demonstrate your programs there.
  • 用 JavaScript 编写(或查找)C++ 解释器
  • 使用运行操作系统的虚拟机(例如jslinux并在那里演示您的程序。

The sensible option is to just give people a way to view and download the source code, I guess.

我猜,明智的选择是让人们可以查看和下载源代码。

回答by Chris Eberle

Google chrome supports this: http://www.readwriteweb.com/cloud/2011/08/google-officially-announces-cc.php

谷歌浏览器支持这个:http: //www.readwriteweb.com/cloud/2011/08/google-officially-announces-cc.php

But it's by no means "mainstream" or standards-based.

但它绝不是“主流”或基于标准的。

回答by Anderson Green

You can use Emscripten to compile C++ to Javascript. Emscripten can compile LLVM bitcode to Javascript. Some demos of Emscripten can be found here, including a raytracer and a text-to-speech engine that was compiled from C++ to Javascript.

您可以使用 Emscripten 将 C++ 编译为 Javascript。Emscripten 可以将 LLVM 位码编译为 Javascript。可以在此处找到 Emscripten 的一些演示,包括光线跟踪器和从 C++ 编译为 Javascript 的文本到语音引擎。

To run x86 binaries in a web browser, you could also use an emulator such as v86. This is one possible way to compile and run C++ programs in a browser.

要在 Web 浏览器中运行 x86 二进制文件,您还可以使用模拟器,例如v86。这是在浏览器中编译和运行 C++ 程序的一种可能方式。

回答by Vadorequest

Another solution (codepad like)would be to use https://ideone.com/which seems much nicer to use than codepad, more user-friendly, but does the same:

另一种解决方案(类似键盘)是使用https://ideone.com/,它似乎比键盘更好用,更用户友好,但也是如此:

Allow you to write C++ (60 languages possibles) directly from the browser and compile it and render result in the browser (I tried using printfand it worked fine). Possibility of forking source code.

允许您直接从浏览器编写 C++(60 种可能的语言)并编译它并在浏览器中呈现结果(我尝试使用printf并且效果很好)。分叉源代码的可能性。

https://ideone.com/baYzfe

https://ideone.com/baYzfe

回答by abe312

The following two programs are quite useful :

以下两个程序非常有用:

1)Ideone

1)爱迪奥

2) Codepad

2)键盘

You can compile, run, and share code online in any browser.

您可以在任何浏览器中在线编译、运行和共享代码。

回答by Abraham Hernandez

One of the best sites for running C++ and other multiple languages online is Repl.it

在线运行 C++ 和其他多种语言的最佳网站之一是Repl.it

This example: repl.it/@abranhe/stackoverflow

这个例子:repl.it/@abranhe/stackoverflow

#include <iostream>

int main() {
  std::cout << "Hello Stackoverflow\n";
  return 0;
}

One of the biggest pros it has is that you can work with multiple files, working with header (header.h) files etc. None of the below websites provide this option:

它最大的优点之一是您可以处理多个文件,处理头 ( header.h) 文件等。以下网站均不提供此选项:

I really recommend it! You will love it!

我真的推荐它!你会喜欢的!