CMAKE 《多模块例子》
概述
生成sort\calc的静态库,并生成app1.exe app2.exe
目录结构
CMakeLists.txt 位置以及配置
根CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(mulitiple_modules)
set(CMAKE_CXX_STANDARD 17)
# define variables
# LIBPATH 库存储位置
set(LIBPATH ${PROJECT_SOURCE_DIR}/lib)
# EXECPATH 可执行文件位置
set(EXECPATH ${PROJECT_SOURCE_DIR}/bin)
# HEADER 头文件存储位置
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)
calc目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_calc)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 生成动态库位置
set(LIBRARY_OUTPUT_PATH ${LIBPATH})
# 生成动态库
add_library(${CALCLIB} STATIC ${SRC})
sort目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_sort)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 生成动态库位置
set(LIBRARY_OUTPUT_PATH ${LIBPATH})
# 生成动态库
add_library(${SORTLIB} STATIC ${SRC})
test1目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_test1)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 可执行文件位置
set(EXECUTABLE_OUTPUT_PATH ${EXECPATH})
# 链接静态库
link_directories(${LIBPATH})
link_libraries(${CALCLIB})
add_executable(${APPNAME1} ${SRC})
test2目录下CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(sub_modules_test2)
set(CMAKE_CXX_STANDARD 17)
# 搜索源文件
aux_source_directory(./ SRC)
# 搜索头文件
include_directories(${HEADPATH})
# 可执行文件位置
set(EXECUTABLE_OUTPUT_PATH ${EXECPATH})
# 链接静态库
link_directories(${LIBPATH})
link_libraries(${SORTLIB})
add_executable(${APPNAME2} ${SRC})
在跟目录下新建build
mkdir build
cd build
# 生成makefiel
cmake ..
# 生成静态库,exe
make
源代码
include/m_calc.h
#ifndef M_CALC_H
#define M_CALC_H
#ifdef __cplusplus
extern "C" {
#endif
int m_add(const int a,const int b);
int m_div(const int a,const int b);
int m_mult(const int a,const int b);
int m_sub(const int a,const int b);
#ifdef __cplusplus
}
#endif
#endif
include/m_sort.h
#ifndef M_SORT_H
#define M_SORT_H
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#ifdef __cplusplus
extern "C"{
#endif
void m_sort(vector<int> &v);
void m_insert(vector<int> &v,const int i);
#ifdef __cplusplus
}
#endif
#endif
calc/m_calc.cpp
#include "m_calc.h"
int m_add(const int a,const int b){
return a+b;
}
int m_div(const int a,const int b){
return a-b;
}
int m_sub(const int a,const int b){
return a/b;
}
int m_mult(const int a,const int b){
return a*b;
}
sort/m_sort.cpp
#include "m_sort.h"
void m_sort(vector<int> &v)
{
sort(v.begin(),v.end(),[](int a,int b)->bool{return a>b;});
}
void m_insert(vector<int> &v,const int i)
{
v.push_back(i);
}
test1/test1.cpp
#include<iostream>
#include"m_calc.h"
using namespace std;
int main(){
cout<<"test1"<<endl;
cout<<m_add(2,2)<<endl;
cout<<m_div(10,5)<<endl;
cout<<m_mult(3,3)<<endl;
cout<<m_sub(9,3)<<endl;
return 0;
}
test2/test1.cpp
#include"m_sort.h"
void print_vector(vector<int> &v){
for (size_t i = 0; i < v.size(); i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
int main(){
vector<int> v;
for (size_t i = 0; i < 50; i++)
{
m_insert(v,i);
}
print_vector(v);
m_sort(v);
print_vector(v);
return 0;
}
本文来自博客园,作者:一个小笨蛋,转载请注明原文链接:https://www.cnblogs.com/paylove/p/18337129