一、搭建開發環境
1.1 c++開發環境
yum -y install gcc gcc-c++ gdb git
1.2 安裝crow所需依賴
yum install boost boost-devel
yum install openssl openssl-devel
1.3 安裝cmake_3.27.9
可以借鑒此安裝:https://blog.csdn.net/i_coding_/article/details/131883590
二、Crow和Asio
2.1 下載Crow
從Crow的github倉庫拉取倉庫,在源碼根目錄下找到include子目錄,其中包括了corw.h以及crow文件夾
2.2 下載Asio
Asio的官網是:http://think-async.com/Asio/,其發布版本放在了sourceforge:https://sourceforge.net/projects/asio/files/asio/,我使用的版本1.28.2,下載之后也是只有一個include目錄,目錄下包含了asio.hpp和asio文件夾。
三、測試項目搭建
3.1 項目目錄
創建crowTest項目:mkdir crowTest && cd crowTest
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── Makefile
│ └── server_manage
├── CMakeLists.txt
├── include
│ ├── asio
│ ├── asio.hpp
│ ├── crow
│ └── crow.h
└── main.cpp
include
:我們將2.1下載的后include目錄中crow
目錄和crow.h
拷貝到項目include
目錄中,以及2.2中的下載的后include目錄中asio
目錄和asio.hpp
拷貝到項目include
目錄中
3.2 cmake配置
CMakeLists.txt:
cmake_minimum_required(VERSION 3.27)
project(crowTest)set(CMAKE_CXX_STANDARD 11)include_directories(${PROJECT_SOURCE_DIR}/include)add_executable(crowTest main.cpp)
target_link_libraries(crowTest pthread)
3.3 編譯項目
寫一個簡單的測試主程序:
main.cpp:
#include<iostream>
using namespace std;
#include "crow.h"int main(){crow::SimpleApp app; //define your crow application//define your endpoint at the root directoryCROW_ROUTE(app, "/")([](){return "Hello world";});//set the port, set the app to run on multiple threads, and run the appapp.port(8667).multithreaded().run();return 0;
}
進入bulid進行編譯:
cd build
cmake ..
make
./crowTest # 啟動項目
啟動成功顯示:
(2024-07-01 01:36:35) [INFO ] Crow/master server is running at http://0.0.0.0:8667 using 4 threads
(2024-07-01 01:36:35) [INFO ] Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.