Git Product home page Git Product logo

ccompileengine's Introduction

CCompileEngine

在Android平台用gcc和g++编译运行c和c++

Install

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // 初始化
        CCppEngine.install(this);
    }
}

Install Gcc

传入 gcc zip file 的文件路径,您可以吧gcc.zip放到 assets, 或者服务器中再下载到本地然后安装,以减少apk大小

  • 您应该在编译前安装好 Gcc
 CCppEngine.installGcc(this,new File("/storage/emulated/0/Android/data/com.xiaoyv.ccompileengine/files/gcc.zip"),new CCppEngine.OnInstallListener(){
            @Override
            public void onSuccess() {
                Log.e("aaaa", "安装成功");
                Toast.makeText(MainActivity.this, "安装成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String error) {
                Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
            }
        });

Compile C

        File cFile = new File("/storage/emulated/0/Android/data/com.xiaoyv.ccompileengine/files/Main.c");
        List<File> files = new ArrayList<>();
        files.add(cFile);

        Log.e("==========", "======================================================================");
        CCppEngine.getCCompiler().compile(files, new CompileCallback() {
            @Override
            public void onSuccess(String outPath) {
                Log.e("==========", "编译完成:" + outPath);


            }

            @Override
            public void onError(String error) {
                Log.e("==========", "编译失败:" + error);

            }
        });

Compile Cpp

        File cFile = new File("/storage/emulated/0/Android/data/com.xiaoyv.ccompileengine/files/Main.cpp");
        List<File> files = new ArrayList<>();
        files.add(cFile);

        Log.e("==========", "======================================================================");
        CCppEngine.getCppCompiler().compile(files, new CompileCallback() {
            @Override
            public void onSuccess(String outPath) {
                Log.e("==========", "编译完成:" + outPath);


            }

            @Override
            public void onError(String error) {
                Log.e("==========", "编译失败:" + error);

            }
        });

Run

下面是自带的CExecutor来运行,你可以引入终端模拟器来更好的执行编译出的二进制文件

                File file = new File(outPath);

                CCppEngine.getCExecutor().exec(file, new ExecuteListener() {
                    @Override
                    public void onExecuteStart(Process process) {
                        Log.e("开始>>>>>>>", " process start");
                    }

                    @Override
                    public void printStderr(Throwable error) {
                        Log.e("错误>>>>>>>", error.toString());
                        error.printStackTrace();
                    }

                    @Override
                    public void printStdout(CharSequence out) {
                        Log.e("输出>>>>>>>", out.toString());
                    }

                    @Override
                    public void onExecuteFinish(int waitFor, int exitValue) {
                        Log.e("完成>>>>>>>", "====================" + waitFor + "================" + exitValue);
                    }
                });

反馈

QQEmail:[email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.