How to run C++ files on the Google Cloud Functions?

十年热恋 提交于 2019-12-11 04:37:49

问题


From what I am aware, Google Cloud Functions only allows you to deploy NodeJs or Python scripts.

Question: How would I be able to deploy a simple Hello_World.cpp on Google Cloud Functions? For example, writing a hello world HTTP function.

What are alternate methods to do this? I want to use serverless approach, since it's cheapest method. Therefore, that is why I'm going with Google Cloud Functions. Would I have to use docker in order to run C++ files? I've been stuck on this for a while and any guidance or help would be appreciated.


回答1:


You can't use C++ on Cloud Functions, period. You can only use Node.js 6.14, Node.js 8.11.1 (beta) and Python 3.7 (also beta).

If you wish to use C++ in GCP with a serverless approach, my best suggestion would be running your own Custom Runtime in App Engine. You would still need to configure some instances options, but you don't have to manage servers and so on.




回答2:


You can compile your C++ function into a WebAssembly module using emscripten. Then you can call it from a small nodejs glue code.

I built an example for you here: https://github.com/ArthurSonzogni/gcloud-cpp-starter




回答3:


You can only use App Engine Flexible Environment (or, of course, standard VM architecture, Compute Engine). Extract from the docs (https://cloud.google.com/appengine/docs/flexible/):

Runtimes - The flexible environment includes native support for Java 8 
(with no web-serving framework), Eclipse Jetty 9, Python 2.7 and Python 3.6,
 Node.js, Ruby, PHP, .NET core, and Go. Developers can customize these 
runtimes or provide their own runtime by supplying a custom Docker image 
or Dockerfile from the open source community.

As an interesting side note, Google Serverless Containers will give you the chance to deploy your dockerized application but in a serverless flavour (in fact it's built on top of Google Cloud Functions technology). It's currently in Alpha stage.




回答4:


You can run C++ Code by node.js on google cloud functions (tested with node.js 10)

  1. how to using C++ and N-API (node-addon-api) https://medium.com/@atulanand94/beginners-guide-to-writing-nodejs-addons-using-c-and-n-api-node-addon-api-9b3b718a9a7f

  2. use https://console.cloud.google.com/functions and click CREATE FUNCTION to upload .zip or gcloud functions deploy --runtime nodejs10 --trigger-http

The trick is when you zip file you need to remove /build and /node_modules folder then use command line by cd to folder of index.js and 'zip your_name.zip -r *'

ps. when I use firebase deploy --only functions it will error because it doesn't know file addon.node format (in fact it shouldn't read this file because it need to be recompiled) but I think if we using gcloud functions command line with .gcloudignore for /build and /node_modules it will success https://cloud.google.com/functions/docs/deploying/filesystem

HOW DOES IT WORK

I think when you deploy node.js source code to cloud functions it will run npm install and your C++ code will be compiled too (like npm run build will be auto run after npm install)



来源:https://stackoverflow.com/questions/52621198/how-to-run-c-files-on-the-google-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!