首页 > 编程知识 正文

cmake菜鸟教程,语法是什么

时间:2023-05-05 19:59:06 阅读:25045 作者:3474

在写这篇文章之前我就在考虑shell脚本和makefile的基础语法知识。 但我觉得太基础了。 对于Liunx shell脚本,教程http://c.biancheng.net/Linux _ tutorial/CMake是跨平台编译工具,cmake主要是cmakelists makefile定义了一组规则,用于指定哪些文件需要先编译、哪些文件需要后编译、哪些文件需要重新编译以及需要执行更复杂的功能操作。 实现自动编译。

用CMake方法编译生成库文件

通过一个简单的示例查看CMake语法来创建test项目。 项目结构如下

test目录

() CMakeLists.txt

() include目录

my print.h

() src目录

my print.CPP

() lib目录

() biuld目录

示例项目的目录结构. png

将头文件放在include目录中,将. c/.cpp源文件放在src目录下,biuld目录是用于生成的项目,而lib目录用于放置生成库文件。

包含目录的myprint.h头文件

#包含

#包含

voidmyprint(char*str;

src目录中的myprint.cpp文件

# include '/usr/demo/test5/include/my print.h '

voidmyprint(char*str ) {

printf('%s ',str;

}

如何使用CMake将项目编译为动态库,以便可以在其他项目中使用。 首先创建CMakeLists.txt文件。 简而言之,CMake输入编译信息。 CMake命令通过CMakeLists.txt生成编译所需的Makefile文件。 让我们来看看CMakeLists.txt的具体创建

指定CMake编译的最低要求版本

cake _ minimum _ required (版本3.14 ) )。

为项目命名

项目(我的打印)。

收集c/c文件并将其赋值给变量src _ list _ CPP $ { project _ source _ dir }以表示区域的当前条目目录

file (glob src _ list _ CPP $ {项目_ source _ dir }/src/*.CPP }

文件(glob src _ list _ c $ { project _ source _ dir }/src/*.c }

#指定头文件目录

include _ directories $ { project _ source _ dir }/include }

#指定生成库文件的目录

set (library _ output _ path $ { project _ source _ dir }/lib ) () ) ) ) ) ) ) )。

#解变量SRC_LIST_CPP和SRC_LIST_C指定libmyprint动态库的生成。 默认情况下,生成静态库。 SHARED指定动态库的生成库类型

add _ library (my print shared $ { src _ list _ CPP } $ { src _ list _ c } )

CMakeLists.txt文件创建cd并在项目的biuld目录中运行cmake命令时,将在biuld目录下生成Makefile文件,运行make命令时将编译项目不是比创建Makefile文件更容易吗? 生成的动态库文件如何链接和使用他呢? 继续往下看

CMake链接使用库文件

这里不创建新项目。 在src目录下创建新的源文件以创建hello.cpp,然后应用libmyprint.so库。

#包含

# include '/usr/demo/test5/include/my print.h '

int main () )。

我的打印(hello world (n ) );

返回0;

}

现在,按如下方式重新编写CMakeLists.txt文件

cake _ minimum _ required (版本3.14 ) )。

#指定项目名称

是项目(hello )

为SOURCE变量分配hello.cpp

set (source $ (项目_ source _ dir )/src/hello.CPP ) ) ) ) ) ) ) )。

#指定首字母

件目录

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)

#指定链接库文件目录

LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib)

#将hello.cpp生成可执行文件hello

ADD_EXECUTABLE(hello ${SOURCE})

#指定hello 链接库myprint

TARGET_LINK_LIBRARIES(hello myprint)

cd到项目biuld目录执行cmake命令 将会在biuld目录下生成Makefile文件,执行make命令,编译完后,将在biuld目录下生成可执行文件hello。执行hello

helloworld.png

Android程序员学习CMake最终还是要为我们Android项目服务,Android studio 2.2 之后开始采用 CMake 的这种方式来构建NDK项目。包括一些优秀的开源库也有采用CMake的方式来编译。具体看看CMake在Android中的使用。

Android中的CMakeLists.txt

由于后面会写一些关于NDK音视频的知识,就以引入FFmpeg音视频库来看看Android 中CMakeLists.txt是怎么编写的。

# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# 需要引入我们头文件,以这个配置的目录为基准

include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)

#添加共享库搜索路径

link_directories(${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi)

# 指定源文件目录

AUX_SOURCE_DIRECTORY(${CMAKE_SOURCE_DIR}/src/main/cpp SRC_LIST)

add_library(

# Sets the name of the library.

native-lib

# Sets the library as a shared library.

SHARED

# Provides a relative path to your source file(s).

# src/main/cpp/native-lib.cpp

${SRC_LIST}

)

find_library( # Sets the name of the path variable.

log-lib

# Specifies the name of the NDK library that

# you want CMake to locate.

log )

target_link_libraries( # Specifies the target library.

native-lib

avcodec-57

avdevice-57

avfilter-6

avformat-57

avutil-55

swresample-2

swscale-4

postproc-54

# Links the target library to the log library

# included in the NDK.

${log-lib} )

总结

了解了CMake语法,以及它正真的意义。以后遇上Android NDK项目便可以自己编写CMakeLists.txt文件内容。要想更深的深入可以去学习一下CMake文档。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。