例如:已经存在一个编译好的c++可执行文件:
简单代码(第一个可执行文件)
vim hello.cpp
#include <iostream>
using namespace std;
int main(){cout<<"Ubuntu调用C++可执行文件成功"<<endl;return 0;
}
编译成可执行文件
g++ -std=c++11 -o hello ./hello.cpp
在另一个项目中需要在代码中执行上一个编译好的程序hello 【在第二个程序中需要使用到第一个程序中的结果,必须要在代码中执行第一个可运行程序,得到结果】
vim ceshi.cpp
#include<iostream>
using namespace std;
int main()
{system("/home/zjs/Desktop/ceshi/hello");//上一个可执行文件的位置return 0;
}
其实与Windows上C++代码中执行.exe程序,几乎相同。
编译
g++ -std=c++11 -o ceshi ./ceshi.cpp
执行 ./ceshi
结果:
Ubuntu调用C++可执行文件成功