1. 环境配置
推荐还是源码编译到自己的位置,毕竟装到系统位置,导致的版本冲突挺严重
1.1 Boost
wget https://boostorg.jfrog.io/artifactory/main/release/1.80.0/source/boost_1_80_0.tar.gz
- 解压
sudo sh bootstrap.sh
sudo ./b2
sudo gedit /etc/ld.so.conf
在最下面加上安装的提示位置:(linker library paths)sudo ldconfig
1.2 wt-4.8.0
wget https://github.com/emweb/wt/archive/4.8.0.tar.gz
- 解压
gedit CMakeLists.txt
- 在第二行添加下面的内容,主要表示Boost的位置还有自定义安装位置
set(Boost_DIR /home/tacom/Development/Tmps/boost/boost_1_80_0/stage/lib/cmake/Boost-1.80.0)
set(CMAKE_INSTALL_PREFIX /home/tacom/Development/Envs/wt_4.8.0)
mkdir build && cd build
cmake ..
make
sudo make install
2. CLion cmake配置
2.1 添加cmake
我的Boost是安装在自定义位置所以需要添加
Wt库的cmake方案我在官方没找到,添加的两个是Config.cmake里的内容
cmake_minimum_required(VERSION 3.24)
project(WtTest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_VERBOSE_MAKEFILE ON) # output makefile
set(Wt_DIR /home/tacom/Development/Envs/wt_4.8.0/lib/cmake/wt)
set(Boost_DIR /home/tacom/Development/Tmps/boost/boost_1_80_0/stage/lib/cmake/Boost-1.80.0)
find_package(Wt)
find_package(Boost)
add_compile_options()
add_executable(WtTest main.cpp)
target_link_libraries(
${PROJECT_NAME}
PRIVATE
Wt::Wt
Wt::HTTP
Boost::boost
)
2.2 添加官方示例代码
#include <Wt/WApplication.h>
#include <Wt/WBreak.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("Hello world");
root()->addWidget(std::make_unique<Wt::WText>("Your name, please? "));
nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>());
Wt::WPushButton *button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet me."));
root()->addWidget(std::make_unique<Wt::WBreak>());
greeting_ = root()->addWidget(std::make_unique<Wt::WText>());
auto greet = [this]{
greeting_->setText("Hello there, " + nameEdit_->text());
};
button->clicked().connect(greet);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) {
return std::make_unique<HelloApplication>(env);
});
}
2.3 添加启动参数
--docroot . --http-address 0.0.0.0 --http-port 9090