Skip to content

Commit 4892763

Browse files
committed
子库改成静态库链接,最终生成单一二进制文件。
1 parent 827d8ec commit 4892763

23 files changed

+694
-532
lines changed

box_calendar/CMakeLists.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ cmake_minimum_required(VERSION 3.8)
33
project(box_calendar LANGUAGES CXX)
44

55
include_directories(${CMAKE_SOURCE_DIR}/3rd/sxtwl_cpp/src)
6-
add_library(box_calendar SHARED
7-
"calendar_v1.h" "calendar_v1.cpp"
8-
"calendar_v1_data.h" "calendar_v1_data.cpp"
9-
"calendar_base.h" "calendar_base.cpp"
10-
"calendar_v2.h" "calendar_v2.cpp"
6+
add_library(box_calendar STATIC
7+
"calendar_v1.h"
8+
"calendar_v1.cpp"
9+
"calendar_v1_data.h"
10+
"calendar_v1_data.cpp"
11+
"calendar_base.h"
12+
"calendar_base.cpp"
13+
"calendar_v2.h"
14+
"calendar_v2.cpp"
1115
)
1216
target_link_libraries(box_calendar PRIVATE sxtwl)
1317
target_compile_definitions(box_calendar PRIVATE CPP_CALENDAR_LIB)

box_calendar/calendar_base.cpp

+38-28
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,29 @@
1111

1212
namespace cppbox {
1313

14-
bool CDate::operator==(const CDate& rh) const {
14+
bool CDate::operator==(const CDate& rh) const
15+
{
1516
if (m_nYear == rh.m_nYear && m_nMon == rh.m_nMon && m_nDay == rh.m_nDay) {
1617
return true;
1718
}
1819
return false;
1920
}
2021

2122
CDate::CDate() = default;
22-
CDate::CDate(const CDate& date) {
23+
CDate::CDate(const CDate& date)
24+
{
2325
m_nYear = date.m_nYear;
2426
m_nMon = date.m_nMon;
2527
m_nDay = date.m_nDay;
2628
}
27-
CDate::CDate(int y, int m, int d) {
29+
CDate::CDate(int y, int m, int d)
30+
{
2831
m_nYear = y;
2932
m_nMon = m;
3033
m_nDay = d;
3134
}
32-
CDate& CDate::operator=(const CDate& date) {
35+
CDate& CDate::operator=(const CDate& date)
36+
{
3337
if (this == &date) {
3438
return *this;
3539
}
@@ -39,7 +43,8 @@ CDate& CDate::operator=(const CDate& date) {
3943
return *this;
4044
}
4145

42-
CDateTime::CDateTime(const CDate& rh) {
46+
CDateTime::CDateTime(const CDate& rh)
47+
{
4348
m_date.m_nYear = rh.m_nYear;
4449
m_date.m_nMon = rh.m_nMon;
4550
m_date.m_nDay = rh.m_nDay;
@@ -48,7 +53,8 @@ CDateTime::CDateTime(const CDate& rh) {
4853
m_time.m_nSec = 0;
4954
}
5055

51-
CDateTime::CDateTime(int y, int m, int d, int h, int min, int sec) {
56+
CDateTime::CDateTime(int y, int m, int d, int h, int min, int sec)
57+
{
5258
m_date.m_nYear = y;
5359
m_date.m_nMon = m;
5460
m_date.m_nDay = d;
@@ -57,7 +63,8 @@ CDateTime::CDateTime(int y, int m, int d, int h, int min, int sec) {
5763
m_time.m_nSec = sec;
5864
}
5965

60-
CDateTime& CDateTime::operator=(const CDateTime& datetime) {
66+
CDateTime& CDateTime::operator=(const CDateTime& datetime)
67+
{
6168
if (this == &datetime) {
6269
return *this;
6370
}
@@ -78,28 +85,27 @@ CDateTime& CDateTime::operator=(const CDate& date)
7885
return *this;
7986
}
8087

81-
CCalenderBase* CCalenderFactory::creatInstance(CalendarType etype) {
88+
CCalenderBase* CCalenderFactory::creatInstance(CalendarType etype)
89+
{
8290
CCalenderBase* pResult = nullptr;
8391

8492
switch (etype) {
85-
case CALENDAR_V1: {
86-
pResult = new CCalenderV1();
87-
break;
88-
}
89-
case CALENDAR_V2: {
90-
pResult = new CCalenderV2();
91-
break;
92-
}
93-
default:
94-
break;
93+
case CALENDAR_V1: {
94+
pResult = new CCalenderV1();
95+
break;
96+
}
97+
case CALENDAR_V2: {
98+
pResult = new CCalenderV2();
99+
break;
100+
}
101+
default:
102+
break;
95103
}
96104
return pResult;
97105
}
98106

99107
// 释放内存
100-
void CCalenderFactory::freeCalender(CCalenderBase* pCalender) {
101-
delete pCalender;
102-
}
108+
void CCalenderFactory::freeCalender(CCalenderBase* pCalender) { delete pCalender; }
103109

104110
CCalenderBase::CCalenderBase() = default;
105111

@@ -108,7 +114,8 @@ CDateTime const& CCalenderBase::getDateTime() const { return m_datetime; }
108114
CDateTime const& CCalenderBase::getLunarDateTime() const { return m_ldatetime; }
109115
#ifdef _WIN32
110116
// 获取系统时间
111-
void CCalenderBase::getNowDateTime(CDateTime& datetime) {
117+
void CCalenderBase::getNowDateTime(CDateTime& datetime)
118+
{
112119
SYSTEMTIME nowTime;
113120
GetLocalTime(&nowTime);
114121
datetime.m_date.m_nYear = nowTime.wYear;
@@ -120,7 +127,8 @@ void CCalenderBase::getNowDateTime(CDateTime& datetime) {
120127
}
121128
#else
122129
// 获取系统时间
123-
void CCalenderBase::getNowDateTime(CDateTime& datetime) {
130+
void CCalenderBase::getNowDateTime(CDateTime& datetime)
131+
{
124132
struct std::tm* nowTime = nullptr;
125133
std::time_t _otime_t;
126134
std::time(&_otime_t);
@@ -201,7 +209,8 @@ bool CCalenderBase::isLeapYear(int year)
201209
}
202210

203211
// 求余数(结果大于0)
204-
int CCalenderBase::getRemainder(int nBase, int nValue) {
212+
int CCalenderBase::getRemainder(int nBase, int nValue)
213+
{
205214
int nRet = nValue % nBase;
206215
if (nRet < 0) {
207216
nRet += nBase;
@@ -210,7 +219,8 @@ int CCalenderBase::getRemainder(int nBase, int nValue) {
210219
}
211220

212221
// 复制日期
213-
void CCalenderBase::copyDateTime(const CDateTime& datetime, CDateTime& outtime) {
222+
void CCalenderBase::copyDateTime(const CDateTime& datetime, CDateTime& outtime)
223+
{
214224
outtime.m_date.m_nYear = datetime.m_date.m_nYear;
215225
outtime.m_date.m_nMon = datetime.m_date.m_nMon;
216226
outtime.m_date.m_nDay = datetime.m_date.m_nDay;
@@ -219,7 +229,6 @@ void CCalenderBase::copyDateTime(const CDateTime& datetime, CDateTime& outtime)
219229
outtime.m_time.m_nSec = datetime.m_time.m_nSec;
220230
}
221231

222-
223232
// 获取四柱
224233
CGanZhi const& CCalenderBase::getSizhu() const { return m_sizhu; }
225234

@@ -228,7 +237,8 @@ CJieQi const& CCalenderBase::getJieFirst() const { return m_first; }
228237
// 获取第二个节气
229238
CJieQi const& CCalenderBase::getJieSecond() const { return m_second; }
230239

231-
CGanZhi& CGanZhi::operator=(const CGanZhi& ganzhi) {
240+
CGanZhi& CGanZhi::operator=(const CGanZhi& ganzhi)
241+
{
232242
if (this == &ganzhi) {
233243
return *this;
234244
}
@@ -242,4 +252,4 @@ CGanZhi& CGanZhi::operator=(const CGanZhi& ganzhi) {
242252
this->m_nYZhi = ganzhi.m_nYZhi;
243253
return *this;
244254
}
245-
} // namespace cppbox
255+
} // namespace cppbox

box_calendar/calendar_base.h

+36-28
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#ifndef BOX_CALENDAR_BASE_H
22
#define BOX_CALENDAR_BASE_H
33

4-
#if defined(_MSC_VER)
5-
#define CPP_CALENDAR_EXPORT __declspec(dllexport)
6-
#define CPP_CALENDAR_IMPORT __declspec(dllimport)
4+
#if defined (DYNAMIC_DLL)
5+
#if defined(_MSC_VER)
6+
#define CPP_CALENDAR_EXPORT __declspec(dllexport)
7+
#define CPP_CALENDAR_IMPORT __declspec(dllimport)
8+
#else
9+
#define CPP_CALENDAR_EXPORT __attribute__((visibility("default")))
10+
#define CPP_CALENDAR_IMPORT __attribute__((visibility("default")))
11+
#endif
12+
13+
#ifdef CPP_CALENDAR_LIB
14+
#define CPP_CALENDAR_API CPP_CALENDAR_EXPORT
15+
#else
16+
#define CPP_CALENDAR_API CPP_CALENDAR_IMPORT
17+
#endif
718
#else
8-
#define CPP_CALENDAR_EXPORT __attribute__((visibility("default")))
9-
#define CPP_CALENDAR_IMPORT __attribute__((visibility("default")))
10-
#endif
11-
12-
#ifdef CPP_CALENDAR_LIB
13-
#define CPP_CALENDAR_API CPP_CALENDAR_EXPORT
14-
#else
15-
#define CPP_CALENDAR_API CPP_CALENDAR_IMPORT
19+
#define CPP_CALENDAR_API
20+
#if defined(_MSC_VER)
21+
#pragma warning(disable: 4251)
22+
#endif
1623
#endif
1724

1825
namespace cppbox {
@@ -21,12 +28,12 @@ namespace cppbox {
2128
enum CalendarType {
2229
// 日历实现类的第一个版本,基于查表实现的日历,有效范围(1901 ~ 2099)
2330
CALENDAR_V1 = 0,
24-
CALENDAR_V2 // 日历实现类的第二个版本,基于天文历算法实现的日历,有效范围(公元前722年
25-
// ~ 9999)
31+
CALENDAR_V2 // 日历实现类的第二个版本,基于天文历算法实现的日历,有效范围(公元前722年
32+
// ~ 9999)
2633
};
2734

2835
// 四柱
29-
struct CPP_CALENDAR_API CGanZhi {
36+
struct CGanZhi {
3037
CGanZhi& operator=(const CGanZhi& ganzhi);
3138
int m_nYGan = -1;
3239
int m_nYZhi = -1;
@@ -76,7 +83,8 @@ struct CPP_CALENDAR_API CJieQi {
7683
};
7784

7885
// 日历处理基类
79-
class CPP_CALENDAR_API CCalenderBase {
86+
class CPP_CALENDAR_API CCalenderBase
87+
{
8088
public:
8189
CCalenderBase();
8290
virtual ~CCalenderBase() = default;
@@ -106,8 +114,7 @@ class CPP_CALENDAR_API CCalenderBase {
106114
// 返回两个日期之间的天数差
107115
virtual int getDiffByTwoDate(const CDate& dateA, const CDate& dateB) = 0;
108116
// 基于基础时间和差值计算新的日期
109-
virtual void getDateTimeBySecond(const CDateTime& basetime,
110-
CDateTime& outtime, long long nSecond) = 0;
117+
virtual void getDateTimeBySecond(const CDateTime& basetime, CDateTime& outtime, long long nSecond) = 0;
111118
// 返回距离 00:00:00 的秒数
112119
virtual int getSecondsFromBase(const CTime& time) = 0;
113120

@@ -117,8 +124,8 @@ class CPP_CALENDAR_API CCalenderBase {
117124
virtual int getDiffByTwoTime(const CTime& timeA, const CTime& timeB) = 0;
118125

119126
// 返回两个日期时间的秒数差
120-
virtual long long getSecondByTwoDateTime(const CDateTime& datetimeA,
121-
const CDateTime& datetimeB) = 0;
127+
virtual long long getSecondByTwoDateTime(const CDateTime& datetimeA, const CDateTime& datetimeB) = 0;
128+
122129
public:
123130
// 获取四柱
124131
CGanZhi const& getSizhu() const;
@@ -128,17 +135,18 @@ class CPP_CALENDAR_API CCalenderBase {
128135
CJieQi const& getJieSecond() const;
129136

130137
protected:
131-
CDateTime m_datetime; // 传入的时间日期
132-
CDateTime m_ldatetime; // 计算的农历日期
133-
bool m_isLeap{}; // 当月是否是闰月
134-
bool m_bigMon{}; // 当月是否是大月
135-
CJieQi m_first; // 当月第一个节气
136-
CJieQi m_second; // 当月第二个节气
137-
CGanZhi m_sizhu; // 此时的四柱
138+
CDateTime m_datetime; // 传入的时间日期
139+
CDateTime m_ldatetime; // 计算的农历日期
140+
bool m_isLeap{}; // 当月是否是闰月
141+
bool m_bigMon{}; // 当月是否是大月
142+
CJieQi m_first; // 当月第一个节气
143+
CJieQi m_second; // 当月第二个节气
144+
CGanZhi m_sizhu; // 此时的四柱
138145
};
139146

140147
// 日历类实例生成工厂
141-
class CPP_CALENDAR_API CCalenderFactory {
148+
class CPP_CALENDAR_API CCalenderFactory
149+
{
142150
private:
143151
CCalenderFactory() = default;
144152
~CCalenderFactory() = default;
@@ -149,5 +157,5 @@ class CPP_CALENDAR_API CCalenderFactory {
149157
// 释放内存
150158
static void freeCalender(CCalenderBase* pCalender);
151159
};
152-
} // namespace cppbox
160+
} // namespace cppbox
153161
#endif

0 commit comments

Comments
 (0)