cmake

第一种:在项目目录写一个CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

project ("rtsp")

该命令会查找指定目录下的所有源文件,然后将结果存进指定变量名。

 #aux_source_directory(<dir> <variable>

aux_source_directory(src/xop/ XOP_SRCS)
aux_source_directory(src/net/ NET_SRCS) #遍历文件夹下的所有文件
# 将源代码添加到此项目的可执行文件。
link_libraries(-lrt -pthread -lpthread -ldl -lm -std=c++11) #链接系统库
add_executable (rtsp ${XOP_SRCS} ${NET_SRCS} "src/3rdpart/md5/md5.hpp" "rtsp_h264_file.cpp")

https://github.com/MoonXu0722/cmake_one.git

---------------------------------------------------------------------------------------------------------------------------------------------

第二种:在每个子目录创建CMakeLists.txt生成库供项目目录调用

项目目录:

cmake_minimum_required (VERSION 3.8)

project ("demo")
add_subdirectory(math)#添加子文件夹
# 将源代码添加到此项目的可执行文件。
add_executable(demo main.cpp)
target_link_libraries(demo add)#add为子目录生成的库名字

子目录:

aux_source_directory(. SRCS_DIR)
add_library(add ${SRCS_DIR})#add为子目录生成的库名字

https://github.com/MoonXu0722/cmake_two.git

------------------------------------------------------------------------------------------------------------------------------------------------

https://github.com/MoonXu0722/share.git

编译动态库

cmake_minimum_required(VERSION 3.16.0)
project(hello)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_library(add SHARED ${SOURCES})
install(TARGETS add DESTINATION shared)#这一步没有成功

------------------------------------------------------------------------------------------------------------------

编译静态库

cmake_minimum_required(VERSION 3.16.0)
project(hello)
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_library(add STATIC ${SOURCES})
install(TARGETS add DESTINATION static)#这一步没有成功

-----------------------------------------------------------------

使用动态库

cmake_minimum_required(VERSION 3.16.0)
project(hello)
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")

set(PROJECT_LINK_LIBS libadd.so)
link_directories(BUILD)
add_executable(hello src/hello.cpp)
target_link_libraries(hello ${PROJECT_LINK_LIBS})

----------------------------------------------------------

使用静态库

cmake_minimum_required(VERSION 3.16.0)
project(hello)
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")

set(PROJECT_LINK_LIBS libadd.a)

link_directories(BUILD)
add_executable(hello src/hello.cpp)
target_link_libraries(hello ${PROJECT_LINK_LIBS})

-------------------------------------------------------------------------------------

 

posted @ 2020-06-25 00:59  MoonXu  阅读(154)  评论(0编辑  收藏  举报