cursor是啥就不介绍了,好像是目前最好用的ai ide,下面主要是配置远程ssh连接linux机器进行qt5 c++程序运行的配置过程记录。
一、c_cpp_properties.json
在项目根目录的.vscode目录里面新建c_cpp_properties.json文件,根据你的实际情况配置该文件内容:
{
"configurations": [
// {
// "name": "Win32",
// "includePath": [
// "${workspaceFolder}/**",
// "D:/msys64/mingw64/include/**",
// "${VCPKG_ROOT}/installed/x64-windows/include/**"
// ],
// "defines": [
// "_DEBUG",
// "UNICODE",
// "_UNICODE"
// ],
// "compilerPath": "D:/msys64/mingw64/bin/gcc.exe",
// "cStandard": "c11",
// "cppStandard": "c++17",
// "intelliSenseMode": "gcc-x64"
// },
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**",
"${VCPKG_ROOT}/installed/x64-linux/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
我这里是用vcpkg在linux下面编译安装的qt,所以include目录要加上它,当然你需要提前配置VCPKG_ROOT环境变量值,我这里是怎么配置的,具体要不要配置看你用没用vcpkg。
export VCPKG_ROOT=/data/cpp/vcpkg
二、tasks.json
还是在项目根目录的.vscode目录里面配置tasks.json,参考配置内容如下:
{
"version": "2.0.0",
"tasks": [
{
"label": "qmake",
"type": "shell",
"command": "/data/cpp/vcpkg/installed/x64-linux/tools/qt5/bin/qmake",
"args": [
"${workspaceFolder}/quickjstest.pro",
"-spec",
"linux-g++",
"CONFIG+=debug",
"CONFIG+=qml_debug"
],
"group": "build"
},
{
"label": "make",
"type": "shell",
"command": "make",
"args": [
"-j8"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": [
"qmake"
]
},
{
"label": "clean",
"type": "shell",
"command": "make clean",
"group": "build"
}
]
}
三、launch.json
同样的路径配置launch.json,参考配置内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/quickjstest",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "DISPLAY",
"value": "${env:DISPLAY}"
},
{
"name": "XAUTHORITY",
"value": "${env:HOME}/.Xauthority"
},
{
"name": "QT_X11_NO_MITSHM",
"value": "1"
},
{
"name": "QT_QPA_PLATFORM",
"value": "xcb"
},
{
"name": "LD_LIBRARY_PATH",
"value": "/usr/lib/x86_64-linux-gnu:/data/cpp/vcpkg/installed/x64-linux/lib:/usr/lib/qt5/lib"
}
],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make"
}
]
}
四、qt工程文件修改
打开你的qt工程文件.pro文件,添加以下内容,用于指定编译过程中生成的临时文件存储路径
# 默认规则使生成的可执行文件带有调试信息
CONFIG += debug
# 输出目录
DESTDIR = $$PWD/build/debug
# 中间文件位置
OBJECTS_DIR = $$PWD/build/debug/.obj
MOC_DIR = $$PWD/build/debug/.moc
RCC_DIR = $$PWD/build/debug/.rcc
UI_DIR = $$PWD/build/debug/.ui
五、修改sshd服务,启用x11转发
修改远程Linux主机的/etc/ssh/sshd_config文件,启用下面这两行配置内容:
X11Forwarding yes
X11DisplayOffset 10
然后systemctl restart sshd 重启sshd服务
六、linux系统环境变量配置
根据你自己的配置习惯,是放在~/.bashrc里面还是/etc/profile文件自己选
export PATH=/data/protoc/bin:$PATH:/data/cpp/vcpkg:/opt/qtcreator-15.0.0/bin:/data/cpp/vcpkg/installed/x64-linux/tools/qt5/bin
export VCPKG_ROOT=/data/cpp/vcpkg
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/data/cpp/vcpkg/installed/x64_linux/lib:$LD_LIBRARY_PATH
export DISPLAY=:10
export QT_QPA_PLATFORM_PLUGIN_PATH=/data/cpp/vcpkg/installed/x64-linux/plugins/platforms/
export QT_DEBUG_PLUGINS=1
这一步的几个关键配置,要是你只是解决qt程序的界面在ssh远程开发的时候出不来的问题,配置最后三个就行了,其他的要是你的qt程序运行时的一些依赖项找不到,把PATH、LD_LIBRARY_PATH这些都配上就行了。
整完以后source 你的配置文件使其生效,实在不行reboot机器也行。
七、cursor/vscode的ssh配置文件修改
ctrl+shift+p 输入或者选择open SSH configureation file,在你的远程主机配置下面添加
Host 目标ssh主机名称
HostName 1.2.3.4
ForwardX11 yes
ForwardX11Trusted yes
ForwardAgent yes
User root
Port 22
IdentityFile C:/Users/你的windows登录用户名/.ssh/id_rsa
八、其他
我的linux系统是ubuntu ,执行qmake以后其实并不是真正的qt make工具,而是一个qtchooser程序,刚才也说了我的qt5是vcpkg自己编译安装的,这里还需要做个简单的配置
# 创建配置目录
sudo mkdir -p /usr/share/qtchooser
# 创建配置文件,假设我们命名为 vcpkg-qt5.conf
sudo vim /usr/share/qtchooser/vcpkg-qt5.conf
在这个vcpkg-qt5.conf配置文件中添加以下内容:
/data/cpp/vcpkg/installed/x64-linux/tools/qt5/bin
/data/cpp/vcpkg/installed/x64-linux/tools/qt5/lib
# 列出所有可用的Qt版本
qtchooser -l
# 设置默认使用vcpkg的Qt5
export QT_SELECT=vcpkg-qt5
# 验证qmake路径
which qmake
qmake --version 要是没有问题这一步就应该显示正确的qmake执行信息了。
#永久固化QT_SELECT的结果避免每次重启都要从头配置
echo 'export QT_SELECT=vcpkg-qt5' >> ~/.bashrc
九、验证一下吧
reboot linux机器,关掉cursor/vscode ide重开,连接远程ssh主机,运行qt项目,linux下面的qt程序gui在我的windows下面显示正常,加断点正常。
剩下的就是愉快的TAB、TAB了。