概述
php通过扩展方式,调用c++源码,这样做的理由有很多,当你搜到这篇文章时,相信你已经有自己的考虑了。
写这篇博客的理由有二:
- 整理下php扩展调用c++代码的过程。嗯,对,网上有很多类似的文章,不过对于php扩展c++的方式,很多文章在关键的地方并没有说明,或者说,大部分都是扩展调用c代码的。
- 已经两年有余没写博客了,在这2017刚开始的时候,开个博客,开始新的旅程,也是不错的,哈哈。
大概目录
- c++编译静态库
- php 扩展编辑
- 结语
注:下面步骤的操作环境——系统macOS10.12.2,PHP版本 5.6.29.
c++编译静态库
头文件:hello.h
#include<string> std::string hello_joint(std::string a, std::string b);
实现头文件定义的函数:hello.cpp
#include "hello.h" std::string hello_joint(std::string a, std::string b) { std::string str = a+b; return str; }
生成hello.o文件
g++ -c hello.cpp
生成静态库libhello.a文件
ar -r libhello.a hello.o
写个简单的test.cpp测试下:
1 #include<iostream> 2 #include "hello.h" 3 int main() 4 { 5 std::string a = std::