https://gitee.com/mrxiao_com/cpp-learning-record.git
cmake
1. 安装cmake和下载mingw64 gcc/g++编译器到指定的目录添加到环境变量
2. 添加cmake插件
3. 新建hello world.cpp 和CMakeLists.txt 文件
4. 在Files: Associations选项中添加CMakeLists.txt 为cmake打开格式以便高亮和补全提示
5. CMakeLists.txt 中添加下面的代码
# 版本设置
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# 声明项目名称与语言
project(demo LANGUAGES CXX)
# 构建hello
add_executable(hello-world hello-world.cpp)
5. 构建项目
5.1 在重新打开带有CMakeLists.txt的文件夹会出现下面提示栏,点击KitSelect出现之前在环境变量中添加的mingw64选择GCC 这个选项,如果不存在点击scan 扫描kits
5.2 点击build构建
6 生成动态库和静态库
# 版本设置
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# 声明项目名称与语言
project(demo LANGUAGES CXX)
# 编译选项
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
#代表头文件所在的路径
include_directories (lib/include)
#添加src子目录所有源文件
aux_source_directory(lib/src/ lib_src)
add_library(lib_obj
OBJECT
${lib_src})
set_target_properties(lib_obj
PROPERTIES
POSITION_INDEPENDENT_CODE 1
)
add_library(lib_share
SHARED
${lib_src})
add_library(lib_static
STATIC
${lib_src})
# 构建hello
add_executable(helloworld helloworld.cpp)
target_link_libraries(helloworld lib_static)
bigNumber
test 程序:
#include <iostream>
#include "bigNumber.h"
using namespace std;
int main()
{
// 在计算中使用long long基本类型的最大值
long long value1{9'223'372'036'854'775'807LL}; // 最大长长值
cout<<"long long value1: " << value1
<<"\nvalue1 - 1 = " << value1 - 1 // OK
<<"\nvalue1 + 1 = " << value1 + 1; // result is undefined
// 使用任意精度的整数
BigNumber value2{value1};
cout<<"\n\nBigNumber value2: "<<value2
<<"\nvalue2 - 1 = "<<value2 - 1 //ok
<<"\nvalue2 + 1 = " << value2 + 1;//ok
// 1亿的幂
long long value3{100'000'000};
cout<<"\n\nvalue3: " << value3;
int counter{2};
while(counter <= 5)
{
value3 *= 100'000'000;
cout<<"\nvalue3 to the power " << counter << ": " << value3;
++counter;
}
// 用BigNumber表示1000,000,000的幂
BigNumber value4{100'000'000};
cout << "\n\nvalue4: " << value4 << endl;
counter = 2;
while (counter <= 5)
{
cout<<"value4.pow("<<counter<<"): "<<value4.pow(counter)<<endl;
++counter;
}
cout<<endl;
return 0;
}
输出:
long long value1: 9223372036854775807
value1 - 1 = 9223372036854775806
value1 + 1 = -9223372036854775808
BigNumber value2: 9223372036854775807
value2 - 1 = 9223372036854775806
value2 + 1 = 9223372036854775808
value3: 100000000
value3 to the power 2: 10000000000000000
value3 to the power 3: 2003764205206896640
value3 to the power 4: -8814407033341083648
value3 to the power 5: -5047021154770878464
value4: 100000000
value4.pow(2): 10000000000000000
value4.pow(3): 1000000000000000000000000
value4.pow(4): 100000000000000000000000000000000
value4.pow(5): 10000000000000000000000000000000000000000