一、环境配置

  • 最近从字节实习完回学校咯,要回来准备秋招了以及一些毕业的项目
  • 在去实习的两个月,老板几乎每周催着一次什么时候回来,真无语。。。:anger:
  • 今年的就业形势也很难啊,各位offer收割机快释放offer给我!!!:pray:

言归正传,之前给项目做的QT界面,还是在QT Creator里面实现的,整体的界面和代码风格都很难评,然后VSCODE用习惯了,所以这次就把VSCODE+QT+PCL+CMake结合在一起。

本地环境

  • Windows
  • PCL1.13
  • QT5.14
  • MSVC 2019

参考

二、问题解决

基本的步骤上面参考都写着很清楚了,跟着一步一步来配置就好,然后记录一些个人遇到的问题。

linker error

1
2
CallOneTimes.obj : error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __cdecl ImageDisplayThread::metaObject(void)const " (?metaObject@ImageDisplayThread@@UEBAPEBUQMetaObject@@XZ) [F:\code\qt_code\build\SR_SdkSample.vcxproj]
[build] CallOneTimes.obj : error LNK2001: 无法解析的外部符号 "public: virtual void * __cdecl

搜了这个问题,一般的解决方法是添加Q_OBJECT,然后删除make的文件,重新编译即可,但是我试了很久(我本来就声明了这个宏),最后还是报这个错误

解决方法:

说一下我的解决方法,一开始我的.h文件和.cpp文件是分别两个文件夹放着,后面将这个两个移动到同一个文件夹下面去,就没有这个linker error这个错误了

执行编译后的exe文件,会弹出控制台

这个在CMakeLists,添加set(CMAKE_WIN32_EXECUTABLE ON) # 使应用程序不依赖控制台 这一段便可以解决

CMakeLists

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
cmake_minimum_required(VERSION 3.5)
project(SR_SdkSample LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH "F:/qt5.14/5.14.2/msvc2017_64")
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_WIN32_EXECUTABLE ON) # 使应用程序不依赖控制台
find_package(Qt5 COMPONENTS Core Gui Widgets PrintSupport Network REQUIRED)
find_package(PCL 1.3 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

include_directories(
F:/code/qt_code/SR_SdkDll
F:/code/qt_code/3DShowDll
F:/PCL/3rdParty/Eigen/eigen3
F:/PCL/3rdParty/FLANN/include
F:/PCL/3rdParty/VTK/include/vtk-9.0
F:/PCL/3rdParty/Qhull/include
F:/PCL/include/pcl-1.12
F:/PCL/3rdParty/Boost/include/boost-1_76
F:/PCL/3rdParty/OpenNI2/Include
)

# 添加 Qwt include 目录
include_directories(${QWT_INCLUDE_DIR})

# 指定 MSVC UTF-8 编码
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

# 自动添加源文件
aux_source_directory(./src srcs)
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}/SR_SdkDll
${CMAKE_CURRENT_SOURCE_DIR}/3DShowDll
)

add_executable(SR_SdkSample ${srcs}) # 根据需要调整源文件
# 然后链接库
target_link_libraries(SR_SdkSample
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::PrintSupport
Qt5::Network
${PCL_LIBRARIES}
SR7Link
SR3dexe
)


tasks.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"version": "2.0.0",
"tasks": [
{
"label": "CMake configure release",
"type": "shell",
"command": "C:/Program Files/CMake/bin/cmake.EXE", // 指定CMake路径
"args": [
"-B",
"build/release",
"-DCMAKE_BUILD_TYPE=Release",
"-G",
"Visual Studio 16 2019", // 使用 MSVC 的生成器
"${workspaceFolder}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"detail": "Configure CMake for Release build"
},
{
"label": "CMake build release",
"type": "shell",
"command": "C:/Program Files/CMake/bin/cmake.EXE",
"args": [
"--build",
"build/release",
"--config",
"Release"
],
"group": "build",
"problemMatcher": [],
"dependsOn": [
"CMake configure release"
],
"detail": "Build the project in release mode"
}
]
}

效果

image-20241015145634862