一、CMake安装
Ubuntu Linux 下安装和卸载cmake 3.28.2版本-CSDN博客https://blog.csdn.net/weixin_41987016/article/details/135960115?spm=1001.2014.3001.5501二、安装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++网络程序开发♪(^∇^*)啦!!!
四、安装mysql
sudo apt-get install mysql-server =>安装最新版MYSQL服务器
sudo apt-get install libmysqlclient-dev => 安装开发包
ubuntu默认安装最新的mysql,但是初始的用户名和密码是自动生成的,按下面步骤修改mysql的root和用户密码为123456
sudo cat /etc/mysql/debian.cnf
命令解释:-u后面是上面查看的用户名 -p后面紧跟上面查看的密码
sudo mysql -u debian-sys-maint -pXGxB5JwQHPIJuwPw
mysql> update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';
mysql> update mysql.user set plugin='mysql_native_password';
mysql> flush privileges;
mysql> quit;
- 重新启动 mysql
heheda@linux:~$ sudo service mysql restart
输入设置好的密码:123456
设置编码:vim /etc/mysql/mysql.conf.d/mysqld.cnf
在最后添加一句:
character-set-server=utf8
查看字符编码
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)mysql>
heheda@linux:~$ sudo netstat -tnap
激活Internet连接 (服务器和已建立连接的)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 738/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1073/sshd
tcp 0 0 127.0.0.1:42039 0.0.0.0:* LISTEN 2802/node
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 917/cupsd
tcp 0 0 0.0.0.0:5112 0.0.0.0:* LISTEN 2933/next-server
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 23431/mysqld
tcp 0 0 127.0.0.1:34893 0.0.0.0:* LISTEN 3060/code-0ee08df0c
tcp 0 0 127.0.0.1:42039 127.0.0.1:56346 ESTABLISHED 2933/next-server
tcp 0 0 127.0.0.1:56338 127.0.0.1:42039 ESTABLISHED 2745/sshd: heheda@n
tcp 0 0 127.0.0.1:56346 127.0.0.1:42039 ESTABLISHED 2745/sshd: heheda@n
tcp 0 0 127.0.0.1:42952 127.0.0.1:34893 ESTABLISHED 3039/sshd: heheda@n
tcp 0 0 192.168.111.128:22 192.168.111.1:11667 ESTABLISHED 5196/sshd: heheda [
tcp 0 0 127.0.0.1:42039 127.0.0.1:56338 ESTABLISHED 2802/node
tcp 0 0 192.168.111.128:22 192.168.111.1:2303 ESTABLISHED 2964/sshd: heheda [
tcp 0 52 192.168.111.128:22 192.168.111.1:11666 ESTABLISHED 5121/sshd: heheda [
tcp 0 0 127.0.0.1:34893 127.0.0.1:42952 ESTABLISHED 3060/code-0ee08df0c
tcp 0 0 192.168.111.128:22 192.168.111.1:2282 ESTABLISHED 2652/sshd: heheda [
tcp6 0 0 :::22 :::* LISTEN 1073/sshd
tcp6 0 0 ::1:631 :::* LISTEN 917/cupsd
tcp6 0 0 :::5113 :::* LISTEN 2933/next-server
heheda@linux:~$
另外在ubuntu中,安装mysql文章推荐:
ubuntu 安装 MySql_ubuntu 安装mysql-CSDN博客https://blog.csdn.net/m0_56051805/article/details/127039085
debian启动mysql_mysql 安装使用笔记-CSDN博客https://blog.csdn.net/weixin_39610759/article/details/112179823
修改表的字符编码:alter table user default character set utf8;
修改属性的字符编码:alter table user modify column name varchar(50) character set utf8;