生成一个库文件
TgMath.h
#ifndef TGMATH_H
#define TGMATH_H
#include "tgmath_global.h"
class TGMATHSHARED_EXPORT TgMath
{
public:
TgMath();
static int add(const int a, const int b);
};
#endif // TGMATH_H
TgMath.cpp
#include "TgMath.h"
TgMath::TgMath()
{
}
int TgMath::add(const int a, const int b)
{
return a + b;
}
加载库文件
CMakeLists.txt
project(lesson001)
# 使用Qt的模块
set(QT Core Widgets)
find_package(Qt5 COMPONENTS REQUIRED ${QT})
include_directories( /opt/TgMath/include )
link_directories( /opt/TgMath/lib )
add_executable(lesson001 lesson001.cpp)
target_link_libraries(lesson001 TgMath ${OpenCV_LIBS})
qt5_use_modules(lesson001 ${QT})
关键三行
include_directories: 指定库的头文件所在目录
link_directories: 指定库文件所在目录
target_link_libraries: 链接库文件
调用库
lesson001.cpp
#include <stdio.h>
#include <QDebug>
#include "TgMath.h"
int main(int argc, char *argv[])
{
qDebug() << "application start";
qDebug() << "TgMath::add(5, 8) " << TgMath::add(5, 8);
return 0;
}