二、安装boost
boost官网:boost官网
我下载的boost版本:
- windows:boost_1_84_0.zip
- linux:boost_1_84_0.tar.gz
2.1 在windows中安装boost和测试
(1)在windows中,解压这个压缩包boost_1_84_0.zip,路径为:D:\boost_1_84_0
- 在这个路径下有一个文件的文件名是bootstrap.bat:
运行cmd,切换到D:\boost_1_84_0这个路径,然后D:\boost_1_84_0>bootstrap.bat
(2)接着执行
.\b2
(3)生成b2.exe文件,默认执行b2.exe
D:\boost_1_84_0>b2.exe
(4)打开visual stdio 2022,在创建好的C++项目中,点击项目>属性>VC++目录这里添加路径到包含目录和库目录
- 包含目录
D:\boost_1_84_0
- 库目录
D:\boost_1_84_0\stage\lib
- main.cpp
#include <iostream>
#include <boost/bind.hpp>
#include <string>
using namespace std;
class Person {
public:
void say(string name) {
cout << name << "说: 三弟你真可爱!" << endl;
}
};
int main() {
Person h;
auto func = boost::bind(&Person::say, &h, "关羽");
func();
return 0;
}
至此,成功验证!!!
2.2 在Linux中安装boost和测试
heheda@linux:~$ cd ~
heheda@linux:~$ pwd
/home/heheda
heheda@linux:~$ mkdir package
heheda@linux:~$ ls
examples.desktop Linux llvm.sh package projects vim配置 公共的 模板 视频 图片 文档 下载 音乐 桌面
将boost_1_69_0.tar.gz 用 FinalShell等上传文件工具,上传到/home/heheda/package这个目录下
heheda@linux:~$ cd package/
heheda@linux:~/package$ ls
boost_1_84_0.tar.gz
heheda@linux:~/package$ tar -zxvf boost_1_69_0.tar.gz
heheda@linux:~/package$ ls
boost_1_84_0 boost_1_84_0.tar.gz
heheda@linux:~/package$ cd boost_1_84_0
heheda@linux:~/package/boost_1_84_0$ ./bootstrap.sh
heheda@linux:~/package/boost_1_84_0$ ./b2
heheda@linux:~/package/boost_1_84_0$ ./b2 install
打开visual stdio 2022,选择Linux,选择控制台应用程序
- 工具>选项>跨平台>连接管理器
打开visual stdio 2022,在创建好的C++项目中,点击项目>属性>VC++目录这里添加路径到包含目录和库目录
- 包含目录
/home/heheda/package/boost_1_84_0
- 库目录
/home/heheda/package/boost_1_84_0/stage/lib
参考和推荐文章:
C++网络编程 - Boost::asio异步网络编程 - 01- boost库源码编译安装_the boost c++ libraries were successfully built!-CSDN博客https://blog.csdn.net/QIANGWEIYUAN/article/details/88792874
三、muduo库
heheda@linux:~/package$ ls
muduo-master.zip
heheda@linux:~/package$ unzip muduo-master.zip
heheda@linux:~/package/muduo-master$ ls
BUILD.bazel build.sh ChangeLog ChangeLog2 CMakeLists.txt contrib examples License muduo patches README WORKSPACE
heheda@linux:~/package/muduo-master$ vim CMakeLists.txt
按下i键,注释掉这一行。按ESC键,然后:wq实现保存退出
- 注意:muduo是用cmake来构建的,所以要先安装cmake
Ubuntu Linux 下安装和卸载cmake 3.28.2版本-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/135960115?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22135960115%22%2C%22source%22%3A%22weixin_41987016%22%7D
./build.sh
heheda@linux:~/package/muduo-master$ ls
BUILD.bazel ChangeLog2 examples patches
build.sh CMakeLists.txt License README
ChangeLog contrib muduo WORKSPACE
heheda@linux:~/package/muduo-master$ ./build.sh
...
[ 93%] Building CXX object muduo/net/inspect/CMakeFiles/muduo_inspect.dir/PerformanceInspector.cc.o
[ 95%] Building CXX object muduo/net/inspect/CMakeFiles/muduo_inspect.dir/ProcessInspector.cc.o
[ 97%] Building CXX object muduo/net/inspect/CMakeFiles/muduo_inspect.dir/SystemInspector.cc.o
[100%] Linking CXX static library ../../../lib/libmuduo_inspect.a
[100%] Built target muduo_inspect
./build.sh install
heheda@linux:~/package/muduo-master$ ls
BUILD.bazel CMakeLists.txt License WORKSPACE
build.sh compile_commands.json muduo
ChangeLog contrib patches
ChangeLog2 examples README
heheda@linux:~/package/muduo-master$ ./build.sh install
heheda@linux:~/package/muduo-master$ cd ..
heheda@linux:~/package$ ls
muduo-master build
muduo-master.zip
heheda@linux:~/package$ cd build
heheda@linux:~/package/build$ ls
release-cpp11 release-install-cpp11
heheda@linux:~/package/build$ cd release-install-cpp11/
heheda@linux:~/package/build/release-install-cpp11$ ls
include lib
heheda@linux:~/package/build/release-install-cpp11$
- 为了让g++会自动从/usr/include和/usr/local/lib路径下寻找所需要的文件
heheda@linux:~/package/build/release-install-cpp11$ cd include/
heheda@linux:~/package/build/release-install-cpp11/include$ ls
muduo
heheda@linux:~/package/build/release-install-cpp11/include$ sudo mv muduo/ /usr/include/
[sudo] heheda 的密码:
heheda@linux:~/package/build/release-install-cpp11/include$ cd ..
heheda@linux:~/package/build/release-install-cpp11$ ls
include lib
heheda@linux:~/package/build/release-install-cpp11$ cd lib/heheda@linux:~/package/build/release-install-cpp11/lib$ ls
libmuduo_base.a libmuduo_inspect.a
libmuduo_http.a libmuduo_net.a
heheda@linux:~/package/build/release-install-cpp11/lib$ sudo mv * /usr/local/lib/
heheda@linux:~/package/build/release-install-cpp11/lib$
测试代码:参考和推荐这篇文章:
C++ muduo网络库知识分享01 - Linux平台下muduo网络库源码编译安装-CSDN博客https://blog.csdn.net/QIANGWEIYUAN/article/details/89023980测试结果:
heheda@linux:~/Linux/Server$ g++ main.cpp -lmuduo_net -lmuduo_base -lpthread -std=c++11
In file included from main.cpp:3:0:
/usr/local/include/boost/bind.hpp:41:1: note: #pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.
)
^
heheda@linux:~/Linux/Server$ ./a.out
20240131 14:55:09.155231Z 47676 INFO pid = 47676 - main.cpp:61
20240131 14:56:17.169399Z 47676 INFO TcpServer::newConnection [EchoServer] - new connection [EchoServer-0.0.0.0:8888#1] from 127.0.0.1:39424 - TcpServer.cc:80
20240131 14:56:17.169444Z 47676 INFO EchoServer - 127.0.0.1:39424 -> 127.0.0.1:8888 is UP - main.cpp:43
20240131 14:56:17.169460Z 47676 INFO EchoServer-0.0.0.0:8888#1 echo 12 bytes, data received at 1706712977.169453 - main.cpp:54
heheda@linux:~$ echo "hello world" | nc localhost 8888
hello world
另外,我们也可以在visual stdio 2022来测试muduo是否安装成功。点击项目>属性>链接器>输入>库依赖项
muduo_net
muduo_base
pthread
至此,muduo安装成功,开始正常进行C++网络程序开发♪(^∇^*)啦!!!
另外在ubuntu中,安装mysql文章推荐:
ubuntu 安装 MySql_ubuntu 安装mysql-CSDN博客https://blog.csdn.net/m0_56051805/article/details/127039085