本帖子适用于初学者,StepByStep的入门CMake,CMake也是一门编程语言,只不过是针对编译和链接这种程序构建的过程的语言,学习基本语法就可以初步入门,后面需要什么复杂的功能,进阶掌握需要自己去查找官方文档,后续会给出查阅的一些方式
+
|
+--- CMakeLists.txt
/--+ src/
|
+--- CMakeLists.txt
/--+ hellolib/
|
+--- CMakeLists.txt
/--+ include/
|
+--- hello.h
/--+ src/
|
+--- hello.cpp
|
/--+ build/
|
/--+ lib/
|
+--- libhello.so
// /CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(HELLOLIB)
ADD_SUBDIRECTORY(src)
// /src/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
ADD_SUBDIRECTORY(hellolib)
// /src/hellolib/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib")
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/src/hellolib/src HELLOlib_src)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/src/hellolib/include")
ADD_LIBRARY(hello SHARED ${HELLOlib_src})
// /src/hellolib/include/hello.h
# ifndef HELLO_H
# define HELLO_H
# include
void HelloFunc();
# endif
// /src/hellolib/src/hello.cpp
# include "hello.h"
using namespace std;
void HelloFunc(){
cout<<"Hello My Linux !"<
ADD_LIBRARY(hello SHARED ${HELLOlib_src})
添加编译库命令:
SHARED,动态库,libhello.so
STATIC,静态库,libhello.aSET(LIBRARY_OUTPUT_PATH “${PROJECT_BINARY_DIR}/lib”)
ELIBRARY_OUTPUT_PATH是cmake默认变量,修改它可以把库文件的路径修改,本例子中修改为build/lib
+
|
+--- CMakeLists.txt
/--+ src/
|
+--- CMakeLists.txt
/--+ hellolib/
|
+--- CMakeLists.txt
/--+ include/
|
+--- hello.h
/--+ src/
|
+--- hello.cpp
|
/--+ build/
|
/--+ lib/
|
+--- libhello.so
+--- libhello.a
// /src/hellolib/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib")
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/src/hellolib/src HELLOlib_src)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/src/hellolib/include")
ADD_LIBRARY(hello SHARED ${HELLOlib_src})
ADD_LIBRARY(hello_static STATIC ${HELLOlib_src})
SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")
#SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
#SET_TARGET_PROPERTIES(hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
#SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.2 SOVERSION 1)
其余同case4
INCLUDE_DIRECTORIES(“${PROJECT_SOURCE_DIR}/src/hellolib/include”)
头文件目录包含指令SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME “hello”)
一般我们希望同时编译出静态库和动态库,并且命名一致,但是cmake的target不允许重名,所以我们把静态库命名为hello_static,然后通过上述语句修改其输出的名字,为hello,这样就可以在lib目录下生成libhello.a和lobhello.so。SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
如果执行make后只生成了一种库,说明生成第二个库的时候删除了同名的其他库。上面的语句可以显示指定不删除同名的库。SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.2 SOVERSION 1)
一般动态库会包含一个版本号,使用上述命令可以携带版本号:VERSION代表同太苦版本,SOVERSION指代API版本。
+
|
+--- CMakeLists.txt
/--+ src/
|
+--- CMakeLists.txt
/--+ hellolib/
|
+--- CMakeLists.txt
/--+ include/
|
+--- hello.h
/--+ src/
|
+--- hello.cpp
/--+ mycmake/
|
+--- CMakeLists.txt
/--+ src/
|
+--- mycmake.cpp
|
/--+ build/
|
/--+ lib/
|
+--- libhello.so
+--- libhello.a
/--+ bin/
|
+--- mycmake
// /src/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
ADD_SUBDIRECTORY(hellolib)
ADD_SUBDIRECTORY(mycmake)
// /src/mycmake/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/src/mycmake/src mycmake_src)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/src/hellolib/include")
LINK_DIRECTORIES("${PROJECT_BINARY_DIR}/lib")
ADD_EXECUTABLE(mycmake ${mycmake_src})
TARGET_LINK_LIBRARIES(mycmake libhello.a)
#TARGET_LINK_LIBRARIES(mycmake libhello.so)
其余同case5
LINK_DIRECTORIES(“${PROJECT_BINARY_DIR}/lib”)
库文件目录包含指令TARGET_LINK_LIBRARIES(mycmake libhello.a)
链接指令,链接自己编译出的静态/动态库,或者链接网上下载的外部库