当前位置: 首页 > 工具软件 > Crow > 使用案例 >

c++框架crow搭建web服务入门

漆雕伟志
2023-12-01

环境
Ubuntu
gcc 7.5.0
按照github官方的安装

cd /usr/local 
git clone https://github.com/ipkn/crow.git
cd crow 
mkdir build
cd build
cmake ..
make

执行到make时候报错了

[  4%] Built target amalgamation
[  9%] Generating ws.html
[  9%] Built target example_ws_copy
[ 19%] Built target example_websocket
[ 28%] Built target helloworld
make[2]: *** No rule to make target '/usr/lib/libtcmalloc_minimal.so', needed by 'examples/example'.  Stop.
CMakeFiles/Makefile2:249: recipe for target 'examples/CMakeFiles/example.dir/all' failed
make[1]: *** [examples/CMakeFiles/example.dir/all] Error 2
Makefile:94: recipe for target 'all' failed

放狗搜了一圈,把crow和crow.h cp到/usr/local/include 下
并且按照官方说明安装依赖库

sudo apt-get install build-essential libtcmalloc-minimal4 && sudo ln -s /usr/lib/libtcmalloc_minimal.so.4 /usr/lib/libtcmalloc_minimal.so

使用g++ -o target helloworld.cpp 编译

/tmp/cciae6FI.o: In function `__static_initialization_and_destruction_0(int, int)':
helloworld.cpp:(.text+0x93e): undefined reference to `boost::system::generic_category()'
helloworld.cpp:(.text+0x94a): undefined reference to `boost::system::generic_category()'
helloworld.cpp:(.text+0x956): undefined reference to `boost::system::system_category()'

查了一下,应该是boost库的问题
再次编译 g++ -o target helloworld.cpp -lboost_system

/usr/bin/ld: /tmp/cc5f9293.o: undefined reference to symbol 'pthread_sigmask@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

最后编译命令 g++ -o target helloworld.cpp -lboost_system -lpthread
顺利通过!

helloworld.cpp

#include "crow.h"

int main()
{
    crow::SimpleApp app;

    CROW_ROUTE(app, "/")([](){
        return "Hello world";
    });

    app.port(18080).multithreaded().run();
}
reference

https://github.com/ipkn/crow
https://www.jianshu.com/p/e6ad3e505d5b
https://blog.csdn.net/qq_21567767/article/details/105911906
https://blog.csdn.net/zmyer/article/details/18963323

 类似资料: