Linux下SGX的使用(3)-编译多个文件

来源:互联网 发布:淘宝什么推广效果好 编辑:程序博客网 时间:2024/06/07 01:07

在上一篇文章中,我们写出了Hello Enclave,但是只有一个cpp文件,较为简单,如果我们想同时编译多个文件我们应该怎么写每个CPP文件对应的EDl文件呢?本次我们就对这些内容做分析:
正如C++支持多个文件同时编译一样,SGX也同时支持类似的方式,我们还是以Hello Enclave的例子做改进,首先,我们看一下C++多个文件同时编译。本质上和原来的C++编译方案无区别,
首先来看一个简单的makefile编译多个文件:
hello.cpp

#include <iostream>#include "world.h"using namespace std;int main(){    char A[10]="hello";    trans(A);    cout << A << endl;}

world.cpp

#include "world.h"void trans(char *A){    const char *B="hello world";    memcpy(A,B,strlen(B)+1);}

world.h

#include <string.h>#include <string.h>void trans(char *A);

makefile文件内容如下:

calc: hello.cpp world.cpp    g++ -o calc hello.cpp world.cpp

那么结果就是

hello world

下面来看enclave版本的多个文件编译
只要包括相应的头文件就可以,还是我们原来的hello enclave为例
enclave.cpp:

#include "enclave_t.h"#include "sgx_trts.h"#include <string.h>#include "hello.h"#include "sgx_urts.h"class test{public:    void test_dck(char *buf_t,size_t len_t)    {        const char *secret="Hello Enclave123123!";        if(len_t>=0)        {        memcpy(buf_t,secret,strlen(secret)+1);        }    }};/*void hello(char *buf_t,size_t len_t){    const char *secret="dck!";        if(len_t>=0)        {        memcpy(buf_t,secret,strlen(secret)+1);        }}*/void  dck_test(char *buf,size_t len){    test test123;    test123.test_dck(buf,len);        hello(buf,len);    //调用hello.h中定义的函数}

同文件夹下的hello.h

#include "stdio.h"#include "stdlib.h"void hello(char *buf_t,size_t len_t);

同文件夹下的hello.cpp

#include "hello.h"#include "string.h"void hello(char *buf_t,size_t len_t){    const char *secret="dck12312312312!";        if(len_t>=0)        {        memcpy(buf_t,secret,strlen(secret)+1);        }}

在编译的过程中我们将hello.cpp和enclave.cpp一同编译
在makefile中的语句是

Enclave/%.o: Enclave/%.cpp    @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@    @echo "CXX  <=  $<"

会将整个enclave文件夹下的cpp都编译,因此一旦将参数通过edl文件传递过去,我们就能能够按照和平时一样的编译策略去编译

阅读全文
0 0
原创粉丝点击