7.1 CMake 简介
CMakeLists.txt 1.语法比较简单,由命令、注释和空格组成,其中命令是不区分大小写的。 2.命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔,每条语句后面不用加; 3.符号 # 注释 4.cmake中所有的变量都是字符串
7.2 CMake 环境安装 项目编译
环境安装
https://blog.csdn.net/weixin_41477306/article/details/108035004
https://blog.csdn.net/weixin_41477306/article/details/108035004
项目编译
linux
1.项目根目录下创建:CMakeLists.txt
2.执行
mkdir build
cd build
cmake ..
make all -j5
windows
1.项目根目录下创建:CMakeLists.txt
2.执行
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make
7.3 CMake 使用
7.3.1 单个文件编译
main.c
#include<stdio.h>
int main() {
printf("hello this is cmake\n");
}
CMakeLists.txt
# Cmake 当文件进行编译
cmake_minimum_required(VERSION 3.0) # 设置最小编译版本
project(cmake_1) # 设置工程名
aux_source_directory(. DIR_SRC) # 设置源文件名
add_executable(cmake_1 ${DIR_SRC}) # 设置生成可执行文件
7.3.2 多个进行文件编译
目录结构
E:\GIT\NDK\CMAKE\CMAKE\2
├─include
head.h
└─src
| func.c
| main.c
|_CMakeLists.txt```
#ifndef _HEAD_H_
#define _HEAD_H_
void fun(int a,int b);
#endif
#include<stdio.h>
void fun(int a,int b)
{
printf("[%d] [%d]\n",a,b);
}
#include<stdio.h>
#include"..\include\head.h"
int main() {
fun(10,20);
}
cmake_minimum_required(VERSION 3.0)
project(cmake_2)
include_directories(include)
aux_source_directory(src DIR_SRCS)
add_executable(cmake_2 ${DIR_SRCS})
7.3.3 静态库
E:\GIT\NDK\CMAKE\CMAKE\3
├─include head.h
├─lib
│ ├─include subhead.h
│ └─src subfun.c
| |_CMakeLists.txt
└─src fun.c main.c
|
|__ CMakeLists.txt
#ifndef _HEAD_H_
#define _HEAD_H_
void fun(int a,int b);
#endif
#ifndef _SUBHEAD_H_
#define _SUBHEAD_H_
void subfun(int a,int b,int c);
#endif
#include<stdio.h>
void subfun(int a,int b,int c)
{
printf("sub [%d] [%d] [%d]\n",a,b,c);
}
#include<stdio.h>
void fun(int a,int b)
{
printf("-----------[%d] [%d]\n",a,b);
}
#include<stdio.h>
#include"..\include\head.h"
#include"..\lib\include\subhead.h"
int main() {
fun(1,2);
subfun(100,200,300);
return 0;
}
#lib/CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
include_directories(include)
aux_source_directory(src DIR_SRCS)
add_library(FUNCLIB STATIC ${DIR_SRCS})
#CmakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(cmake_3)
include_directories(include)
add_subdirectory(lib)
aux_source_directory(src DIR_SRCS)
add_executable(cmake_3 ${DIR_SRCS})
target_link_libraries(cmake_3 FUNCLIB)
7.3.4 生成动态库
E:\GIT\NDK\CMAKE\CMAKE\4
├─include head.h link.h
├─link link.cpp
└─src main.cpp
#ifndef _HEAD_H_
#define _HEAD_H_
#include<iostream>
#include"link.h"
namespace android{
class HEAD {
private:
android::LINK * link;
public:
void setLink(android::LINK *_link)
{
link = _link;
}
void add(int a,int b)
{
int result;
link->add(a,b,result);
std::cout<<__LINE__<<result<<std::endl;
}
void sub(int a,int b)
{
int result;
link->sub(a,b,result);
std::cout<<__LINE__<<result<<std::endl;
}
};
}
#endif
#ifndef _LINK_H_
#define _LINK_H_
namespace android {
class LINK {
public:
void add(int a,int b,int &result);
void sub(int a,int b,int &result);
};
}
#endif
#include<iostream>
#include"../include/link.h"
using namespace std;
namespace android {
void LINK::add(int a,int b,int &result){
int result0;
result = a+b;
std:cout<<__LINE__<<" "<<result<<std::endl;
}
void LINK::sub(int a,int b,int &result){
result = a-b;
std:cout<<__LINE__<<" "<<result<<std::endl;
}
}
#include<iostream>
#include"../include/link.h"
#include"../include/head.h"
using namespace android;
int main() {
HEAD *hash = new HEAD();
LINK *link = new LINK();
int result = 0;
hash->setLink(link);
hash->add(10,1000);
hash->sub(12345,45678);
}
cmake_minimum_required(VERSION 3.0)
project(c)
set(SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(LINK ${CMAKE_CURRENT_SOURCE_DIR}/link)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
aux_source_directory(${SRC} AUX)
add_executable(main1 ${AUX})
aux_source_directory(${LINK} link)
add_library(link SHARED ${link})
target_link_libraries(main1 link)
7.3.5 链接已存在的动态库(重要)
E:\GIT\NDK\CMAKE\CMAKE\5
├─include HEAD.h Test.h
├─so
│ └─so libfun.dll
| |_ Head.c
└─src main.cpp Test.cpp
|_ CMakeLists.txt
#ifndef _HEAD_H_
#define _HEAD_H_
#include<stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
void lin_fun(int a,int b);
#ifdef __cplusplus
}
#endif
#endif
#ifndef _TEST_H_
#define _TEST_H_
#include"HEAD.h"
namespace iauto
{
class Test
{
public:
static void fun();
};
}
#endif
#include<stdio.h>
void lin_fun(int a,int b)
{
printf("a[%d] b[%d]\n",a,b);
}
#include"Test.h"
#include"HEAD.h"
int main()
{
iauto::Test::fun();
}
#include"Test.h"
#include"HEAD.h"
#include<iostream>
namespace iauto
{
void Test::fun()
{
std::cout<<"fun-->so"<<std::endl;
lin_fun(10,11);
}
}
cmake_minimum_required(VERSION 3.0)
project(cmake_5)
# 生成动态库(fun) ,cmake_5_exe 进行链接
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SO ${CMAKE_CURRENT_SOURCE_DIR}/so)
aux_source_directory(${SO} LINK_SO)
add_library(fun SHARED ${LINK_SO})
# aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
# add_executable(cmake_5_exe ${SRC})
# target_link_libraries(cmake_5_exe fun)
#-----------------------------------------------------------------
#注意链接已经存在的so库时,运行时环境变量中要存在此库
#方式1 链接已经存在的fun so库
#CMAKE_CXX_FLAGS方式
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_CURRENT_SOURCE_DIR}/so/so/")
# aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
# add_executable(cmake_5_exe ${SRC})
# target_link_libraries(cmake_5_exe fun)
#-----------------------------------------------------------------
#方式2 链接已经存在的so库
#target_link_libraries方式
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
# file(GLOB SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
# add_executable(cmake_5_exe ${SOURCE})
# target_link_libraries(cmake_5_exe ${CMAKE_CURRENT_SOURCE_DIR}/so/so/libfun.dll)
#-----------------------------------------------------------------
#方式3 链接已经存在的so库
#find_library方式
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
# file(GLOB SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
# add_executable(cmake_5_exe ${SOURCE})
# find_library(myfun libfun.dll ${CMAKE_SOURCE_DIR}/cmake/5/so/so/)
# target_link_libraries(cmake_5_exe ${myfun})
#-----------------------------------------------------------------
#方式4 链接已经存在的so库
#link_directories方式(link_directories 要放在add_executable 前面)
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
# file(GLOB SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
# link_directories(${CMAKE_SOURCE_DIR}/cmake/5/so/so/)
# add_executable(cmake_5_exe ${SOURCE})
# target_link_libraries(cmake_5_exe fun)
#-----------------------------------------------------------------
#方式5 链接已经存在的so库
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/)
# set(PATH_SO ${CMAKE_SOURCE_DIR}/cmake/5/so/so/)
# set_target_properties(fun PROPERTIES IMPORTED_LOCATION ${PATH_SO}/libfun.dll)
# file(GLOB SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
# add_executable(cmake_5_exe ${SOURCE})
# target_link_libraries(cmake_5_exe fun)
7.3.6 生成动态库去链接动态库(重要)
7.4 CMake 总结
|