line15 ~ 21
#ifndef __mobile__
#ifndef NO_SERIAL_LINK
#include <QtSerialPort/QSerialPort>
#endif
#include "QGCSerialPortInfo.h"
#include "RunGuard.h"
#endif
顶层CMakeLists.txt规定:如果当前是ios/android环境,编译器添加__mobile__定义。
在src/comm/CMakeLists.txt有:
option(QGC_NO_SERIAL_LINK "Disable Serial Links" OFF)
if(QGC_NO_SERIAL_LINK)
target_compile_definitions(comm PUBLIC NO_SERIAL_LINK)
else()
… # 详见源码
endif()
默认打开Serial Links,也即编译器没有定义NO_SERIAL_LINK,如此main.cc将会包含<QtSerialPort/QSerialPort>,实现串口通信。
RunGuard.h头文件及RunGuard.cc源文件主要作用是提供QGC运行监测,在main.cc的main函数一开始,就尝试运行QGC,但是当发现本机有另一个QGC正在运行,就会直接退出main函数:
#ifndef __mobile__
// We make the runguard key different for custom and non custom
// builds, so they can be executed together in the same device.
// Stable and Daily have same QGC_APPLICATION_NAME so they would
// not be able to run at the same time
QString runguardString(QGC_APPLICATION_NAME);
runguardString.append("RunGuardKey");
RunGuard guard(runguardString);
if (!guard.tryToRun()) {
// QApplication is necessary to use QMessageBox
QApplication errorApp(argc, argv);
QMessageBox::critical(nullptr, QObject::tr("Error"),
QObject::tr("A second instance of %1 is already running. Please close the other instance and try again.").arg(QGC_APPLICATION_NAME)
);
return -1;
}
#endif
需要注意一点的是:
1)用户在自行编译QGC时,可以通过修改QGC_APPLICATION_NAME的值,实现多个QGC应用程序同时运行。
2)Stable和Daily版本的QGC有相同的QGC_APPLICATION_NAME,因此若不修改QGC_APPLICATION_NAME,还是不能同时运行Stable和Daily版本的QGC。
line23 ~ 28
#ifdef Q_OS_ANDROID
#include "AndroidInterface.h"
#ifndef NO_SERIAL_LINK
#include "qserialport.h"
#endif
#endif
使用vs code搜索Q_OS_ANDROID,并未搜索到其定义的位置,但是Q_OS_ANDROID实际上是Qt根据目标平台为android自动生成的,参考官方文档:<QtSystemDetection> - Platform-specific Macro Definitions | Qt Core 6.7.1
通过查阅AndroidInterface.cc源码,主要作用是在android机器上检查对存储介质的读写权限+获取SD卡存储路径。
注意当非移动平台:
#include <QtSerialPort/QSerialPort>
对于android平台:
#include "qserialport.h"
实际上<QtSerialPort/QSerialPort>内部只有一行代码:
#include "qserialport.h"
因此没区别。
line30 ~ 32
#ifdef UNITTEST_BUILD
#include "UnitTest.h"
#endif
在顶层CMakeLists.txt中,明确过如果构建类型是Debug,则向编译器添加UNITTEST_BUILD定义,此处包含UnitTest.h,为后续main函数启动单元测试准备。
line34 ~ 39
#ifdef QT_DEBUG
#include "CmdLineOptParser.h"
#ifdef Q_OS_WIN
#include <crtdbg.h>
#endif
#endif
QT_DEBUG
QT_DEBUG由Qt的<QtCore/qtconfigmarcos.h>定义:
#if !defined(QT_NO_DEBUG) && !defined(QT_DEBUG)
# define QT_DEBUG
#endif
在顶层CMakeLists.txt中有:
if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_definitions(
NDEBUG
QT_NO_DEBUG
QT_NO_DEBUG_OUTPUT)
CmdLineOptParser.h
选择Debug构建,不会产生QT_NO_DEBUG,导致产生QT_DEBUG宏,进一步包含CmdLineOptParser.h头文件,CmdLineParser.cc内只有一个ParseCmdLineOptions函数,作用主要是进行命令行的解析,对于命令参数中的选项,如果找到了会将CmdLineOpt_t结构体的bool变量设置为true。
if (found) {
*prgOpts[iOption].optionFound = true;
… // 详见源码
}
CmdLineOpt_t结构体定义如下:
typedef struct {
const char* optionStr; ///< Command line option, for example "--foo"
bool* optionFound; ///< If option is found this variable will be set to true
QString* optionArg; ///< Option has additional argument, form is option:arg
} CmdLineOpt_t;
回到main.cc,198~232行,在debug模式下有:
CmdLineOpt_t rgCmdLineOptions[] = {
{ "--unittest", &runUnitTests, &unitTestOptions },
{ "--unittest-stress", &stressUnitTests, &unitTestOptions },
{ "--no-windows-assert-ui", &quietWindowsAsserts, nullptr },
// Add additional command line option flags here
};
ParseCmdLineOptions(argc, argv, rgCmdLineOptions, sizeof(rgCmdLineOptions)/sizeof(rgCmdLineOptions[0]), false);
如果命令参数argv中检查到—unittest,会将runUnitTests设为true;如果检查到--unittest-stress,会将stressUnitTests设为true;如果检查到--no-windows-assert-ui,会将quietWindowsAsserts设为true。后续再根据三个变量的bool值,分类完成test处理。
crtdbg.h
<crtdbg.h>头文件主要作用是提供_CrtSetReportHook宏,用于将自定义的钩子函数WindowsCrtReportHook,作为_CrtSetReportHook宏的参数(函数指针),交给windows操作系统,替换windows默认的错误报告动作,关于_CrtSetReportHook请参考微软官方文档:
_CrtSetReportHook | Microsoft Learn
QGC注释意思是不想让windows在QGC发生异常时弹出警告框,而是使用std::err输出,WindowsCrtReportHook定义如下:
/// @brief CRT Report Hook installed using _CrtSetReportHook. We install this hook when
/// we don't want asserts to pop a dialog on windows.
int WindowsCrtReportHook(int reportType, char* message, int* returnValue)
{
Q_UNUSED(reportType);
std::cerr << message << std::endl; // Output message to stderr
*returnValue = 0; // Don't break into debugger
return true; // We handled this fully ourselves
}
根据_CrtSetReportHook文档,此函数返回true表示客户定义的报告函数可以完全处理调试消息,不需要进一步报告,当此函数返回false,系统将自动调用_CrtDbgReport生成调试报告,此时returnValue指定_CrtDbgReport返回值,当returnValue返回0,将不会发生调试中断。关于_CrtDbgReport请参考官方文档:_CrtDbgReport、_CrtDbgReportW | Microsoft Learn
line53 ~ 55
/* SDL does ugly things to main() */
#ifdef main
#undef main
#endif
在main.cc中,特别注意先对main函数进行取消定义,注释中提到SDL部分污染了main函数,使用vs code搜索main,发现SDL_main.h对main函数进行了定义,指向SDL_main函数,而SDL_main是一个SDL库函数,SDL官方文档给出注释:
/**
* Circumvent failure of SDL_Init() when not using SDL_main() as an entry
* point.
*
* This function is defined in SDL_main.h, along with the preprocessor rule to
* redefine main() as SDL_main(). Thus to ensure that your main() function
* will not be changed it is necessary to define SDL_MAIN_HANDLED before
* including SDL.h.
*
* \since This function is available since SDL 2.0.0.
*
* \sa SDL_Init
*/
line57 ~ 61
#ifndef __mobile__
#ifndef NO_SERIAL_LINK
Q_DECLARE_METATYPE(QGCSerialPortInfo)
#endif
#endif
根据Qt官方文档QMetaType Class | Qt Core 6.7.1
如果一个类型有:
1)默认的构造函数
2)默认的拷贝构造函数
3)默认析构函数
Q_DECLARE_METATYPE将会使此类型被QMetaType发现,有3点需要注意:
1)对于结构体类型,以上3个条件满足
2)一般在类型声明下面完成这个宏,如果有namespace,宏定义时需要加上
namespace MyNamespace
{
struct MyStruct
{
int i;
...
};
...
}
Q_DECLARE_METATYPE(MyNamespace::MyStruct)
3)常常用于queued类型信号槽中传递数据,但是用作此用途时需要调用qRegisterMetaType()
此处向MetaType元对象系统声明QGCSerialPortInfo类型,在main.cc第
181行有如下代码:
qRegisterMetaType<QGCSerialPortInfo>();
为什么要注册呢?使用vs code搜索QGCSerialPortInfo,发现一些端倪,比如在PX4FirmwareUpgradeThread.h的private slots中:
void _foundBoard(
bool firstAttempt,
const QGCSerialPortInfo& portInfo,
int type,
QString name)
{
emit foundBoard(firstAttempt, portInfo, type, name);
}
信号emit了QGCSerialPortInfo类型数据,因此从这一点出发按照官方文档,应该完成以下操作:
Q_DECLARE_METATYPE(QGCSerialPortInfo)
qRegisterMetaType<QGCSerialPortInfo>();
435


&spm=1001.2101.3001.11974&articleId=138576775&d=1&t=3&u=8d5289f9148440088b129b109e985111)

被折叠的 条评论
为什么被折叠?



