heheda@linux:~/Linux/multi-v1$ tree
.
├── calc
│ ├── add.cpp
│ ├── CMakeLists.txt
│ ├── div.cpp
│ ├── mult.cpp
│ └── sub.cpp
├── CMakeLists.txt
├── include
│ ├── calc.h
│ └── sort.h
├── sort
│ ├── CMakeLists.txt
│ ├── insert.cpp
│ └── select.cpp
├── test1
│ ├── calc.cpp
│ └── CMakeLists.txt
└── test2├── CMakeLists.txt└── sort.cpp5 directories, 15 files
heheda@linux:~/Linux/multi-v1$
1.include 目录:头文件目录
2.calc目录:目录中的四个源文件对应的加/减/乘/除算法
- 对应的头文件是include中的calc.h
3.sort目录:目录中的两个源文件对应的是插入排序和选择排序算法
- 对应的头文件是include中的sort.h
4.test1 目录:测试目录,对加、减、乘、除算法进行测试
5.test2 目录:测试目录,对排序算法进行测试
一、calc文件夹
- calc的add.cpp
#include <stdio.h>
#include "calc.h"int add(int a, int b)
{return a+b;
}
- calc的div.cpp
#include <stdio.h>
#include "calc.h"double divide(int a, int b)
{return (double)a/b;
}
- calc的mult.cpp
#include <stdio.h>
#include "calc.h"int multiply(int a, int b)
{return a*b;
}
- calc的sub.cpp
#include <stdio.h>
#include "calc.h"// 你好
int subtract(int a, int b)
{return a-b;
}
二、sort文件夹
- sort的insert.cpp
#include <iostream>
using namespace std;
void print(int a[], int n)
{for(int j= 0; j<n; j++){cout<<a[j] <<" ";}cout<<endl;
}void insert(int a[], int n)
{for(int i = 1; i < n; i++) //第一个元素作为基准元素,从第二个元素开始把其插到正确的位置{if(a[i] < a[i-1]) //如果第i个元素比前面的元素小{int j = i-1; //需要判断第i个元素与前面的多个元素的大小,换成j继续判断int x = a[i]; //将第i个元素复制为哨兵while(j >= 0 && x < a[j]) //找哨兵的正确位置,比哨兵大的元素依次后移{a[j+1] = a[j];j--;}a[j+1] = x; //把哨兵插入到正确的位置}}
}
- sort的select.cpp
#include<iostream>
using namespace std;
void print1(int a[], int n)
{for(int j= 0; j<n; j++){cout<<a[j] <<" ";}cout<<endl;
}void select(int a[], int len)
{int minindex, temp;for(int i = 0; i<len-1;i++){minindex = i;for(int j = i+1; j<len; j++){if(a[j]<a[minindex])minindex = j;}temp = a[i];a[i] = a[minindex];a[minindex] = temp;}
}
三、include文件夹
- calc.h
#ifndef _CALC_H
#define _CALC_H
#include <iostream>
using namespace std;
// 加法
int add(int a, int b);
// 减法
int subtract(int a, int b);
// 乘法
int multiply(int a, int b);
// 除法
double divide(int a, int b);
#endif
- sort.h
#ifndef _SORT_H
#define _SORT_H#include <iostream>
using namespace std;// 插入排序
void print(int a[], int n);
void insert(int a[], int n);// 选择排序
void print1(int a[], int n);
void select(int a[], int len);
#endif
四、test1文件夹
- calc.cpp
#include <stdio.h>
#include "calc.h"int main()
{int a = 20;int b = 12;printf("a = %d, b = %d\n", a, b);printf("a + b = %d\n", add(a, b));printf("a - b = %d\n", subtract(a, b));printf("a * b = %d\n", multiply(a, b));printf("a / b = %f\n", divide(a, b));return 0;
}
五、test2文件夹
- sort.cpp
#include "sort.h"
#include <iostream>
using namespace std;
int main()
{int a[10] = {8,1,9,7,2,4,5,6,10,3};insert(a,10);cout<<"插入排序结果:";print(a,10);select(a, 10);cout << "选择排序结果: ";print1(a, 10);
}
六、添加CMakeLists.txt
1.在calc文件夹中添加CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(calc)# 搜索源文件
aux_source_directory(./ SRC)
include_directories(${HEADPATH}) # 头文件路径 HEADPATHset(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(${CALCLIB} STATIC ${SRC}) # 生成静态库
2. 在test1文件夹中添加CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test1)aux_source_directory(./ SRC)
include_directories(${HEADPATH})set(EXECUTABLE_OUTPUT_PATH ${EXECPATH})# 包含静态库路径
link_directories(${LIBPATH})
# 链接静态库, calc为静态库的名字
link_libraries(${CALCLIB})add_executable(${APPNAME1} ${SRC})
3.在sort文件夹中添加CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(sort)# 搜索源文件
aux_source_directory(./ SRC)
include_directories(${HEADPATH}) # 头文件路径 HEADPATHset(LIBRARY_OUTPUT_PATH ${LIBPATH}) # 库生成的路径 LIBPATH
add_library(${SORTLIB} STATIC ${SRC}) # 生成静态库
4. 在test2文件夹中添加CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test2)aux_source_directory(./ SRC)
include_directories(${HEADPATH})set(EXECUTABLE_OUTPUT_PATH ${EXECPATH})# 包含静态库路径
link_directories(${LIBPATH})
# 链接静态库, calc为静态库的名字
link_libraries(${SORTLIB})add_executable(${APPNAME2} ${SRC})
5. 在该项目的根目录中添加CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test)
# 定义变量# 库生成的路径
set(LIBPATH ${PROJECT_SOURCE_DIR}/lib)# 可执行程序的存储目录(测试程序生成的路径)
set(EXECPATH ${PROJECT_SOURCE_DIR}/bin)# 头文件路径
set(HEADPATH ${PROJECT_SOURCE_DIR}/include)# 库文件的名字
set(CALCLIB calc)
set(SORTLIB sort)# 可执行程序的名字
set(APPNAME1 app1)
set(APPNAME2 app2)# 给当前节点添加子目录
add_subdirectory(calc)
add_subdirectory(sort)
add_subdirectory(test1)
add_subdirectory(test2)
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5.cd ../bin
6. ./app1
7. ./app2执行结果:
heheda@linux:~/Linux/multi-v1$ mkdir build
heheda@linux:~/Linux/multi-v1$ cd build
heheda@linux:~/Linux/multi-v1/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/heheda/Linux/multi-v1/build
heheda@linux:~/Linux/multi-v1/build$ make
Scanning dependencies of target calc
[ 8%] Building CXX object calc/CMakeFiles/calc.dir/add.cpp.o
[ 16%] Building CXX object calc/CMakeFiles/calc.dir/div.cpp.o
[ 25%] Building CXX object calc/CMakeFiles/calc.dir/mult.cpp.o
[ 33%] Building CXX object calc/CMakeFiles/calc.dir/sub.cpp.o
[ 41%] Linking CXX static library ../../lib/libcalc.a
[ 41%] Built target calc
Scanning dependencies of target sort
[ 50%] Building CXX object sort/CMakeFiles/sort.dir/insert.cpp.o
[ 58%] Building CXX object sort/CMakeFiles/sort.dir/select.cpp.o
[ 66%] Linking CXX static library ../../lib/libsort.a
[ 66%] Built target sort
Scanning dependencies of target app1
[ 75%] Building CXX object test1/CMakeFiles/app1.dir/calc.cpp.o
[ 83%] Linking CXX executable ../../bin/app1
[ 83%] Built target app1
Scanning dependencies of target app2
[ 91%] Building CXX object test2/CMakeFiles/app2.dir/sort.cpp.o
[100%] Linking CXX executable ../../bin/app2
[100%] Built target app2
heheda@linux:~/Linux/multi-v1/build$ cd ../bin
heheda@linux:~/Linux/multi-v1/bin$ ./app1
a = 20, b = 12
a + b = 32
a - b = 8
a * b = 240
a / b = 1.666667
heheda@linux:~/Linux/multi-v1/bin$ ./app2
插入排序结果:1 2 3 4 5 6 7 8 9 10
选择排序结果: 1 2 3 4 5 6 7 8 9 10
heheda@linux:~/Linux/multi-v1/bin$