CMake 实例学习(3)构建静态库

1:概述
   有了上一节共享库的工作,这节的就简单多了。

2: 目录结构
  1. [onezeroone@ ex-4]$ tree
  2. .
  3.  build
  4.  CMakeLists.txt
  5.  lib
  6.   CMakeLists.txt
  7.   hello.c
  8.   hello.h
  9.  src
  10.      CMakeLists.txt
  11.      main.c

  12. 3 directories, 6 files

3:文件内容
   这里就不罗嗦了,直接看内容吧,参考前面共享库的制作,需要稍作修改就OK。
  1. [onezeroone@ ex-4]$ cat CMakeLists.txt
  2. PROJECT(EX-4)
  3. ADD_SUBDIRECTORY(lib)
现在是制作静态库时的状态,使用静态库的时候我们需要稍作修改。
  1. [onezeroone@ ex-4]$ cat ./lib/CMakeLists.txt
  2. SET(LIBHELLO_SRC hello.c)
  3. ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC})
  4. INSTALL(TARGETS hello
  5.         ARCHIVE DESTINATION lib)
  6. INSTALL(FILES hello.h DESTINATION include/hello)
需要修改只有2处,STATIC表示要生成静态库,ARCHIVE表示指定静态库的路径。hello文件跟上一节一样。

跟上一节一样,我们cmake一把吧。
  1. [onezeroone@ ex-4]$ cd build/
  2. [onezeroone@ build]$ cmake ..
  3. -- The C compiler identification is GNU
  4. -- The CXX compiler identification is GNU
  5. -- Check for working C compiler: /usr/bin/gcc
  6. -- Check for working C compiler: /usr/bin/gcc -- works
  7. -- Detecting C compiler ABI info
  8. -- Detecting C compiler ABI info - done
  9. -- Check for working CXX compiler: /usr/bin/c++
  10. -- Check for working CXX compiler: /usr/bin/c++ -- works
  11. -- Detecting CXX compiler ABI info
  12. -- Detecting CXX compiler ABI info - done
  13. CMake Warning (dev) in CMakeLists.txt:
  14.   No cmake_minimum_required command is present. A line of code such as

  15.     cmake_minimum_required(VERSION 2.8)

  16.   should be added at the top of the file. The version specified may be lower
  17.   if you wish to support older CMake versions for this project. For more
  18.   information run "cmake --help-policy CMP0000".
  19. This warning is for project developers. Use -Wno-dev to suppress it.

  20. -- Configuring done
  21. -- Generating done
  22. -- Build files have been written to: /home/onezeroone/work/cmake/ex-4/build
  23. [onezeroone@ build]$ make
  24. Scanning dependencies of target hello
  25. [100%] Building C object lib/CMakeFiles/hello.dir/hello.o
  26. Linking C static library libhello.a
  27. [100%] Built target hello
  28. [onezeroone@ build]$ ls ./lib/
  29. CMakeFiles cmake_install.cmake libhello.a Makefile
呵呵,看到我们的静态库了。

4:安装静态库
   安装前我们要制定安装路径,所以我们再重新构建吧。
  1. [onezeroone@ build]$ cmake -DCMAKE_INSTALL_PREFIX=/usr ..
  2. CMake Warning (dev) in CMakeLists.txt:
  3.   No cmake_minimum_required command is present. A line of code such as

  4.     cmake_minimum_required(VERSION 2.8)

  5.   should be added at the top of the file. The version specified may be lower
  6.   if you wish to support older CMake versions for this project. For more
  7.   information run "cmake --help-policy CMP0000".
  8. This warning is for project developers. Use -Wno-dev to suppress it.

  9. -- Configuring done
  10. -- Generating done
  11. -- Build files have been written to: /home/onezeroone/work/cmake/ex-4/build
  12. [onezeroone@ build]$ make
  13. [100%] Built target hello
  14. [onezeroone@ build]$ sudo make install
  15. [100%] Built target hello
  16. Install the project...
  17. -- Install configuration: ""
  18. -- Installing: /usr/lib/libhello.a
  19. -- Up-to-date: /usr/include/hello/hello.h
我们可以看到已经安装到指定的/usr目录下面了。

5:使用静态库
   我们修改工程目录下的CMakeLists.txt文件
  1. [onezeroone@ ex-4]$ cat CMakeLists.txt
  2. PROJECT(EX-4)
  3. ADD_SUBDIRECTORY(src bin)
修改库路径为主程序路径,并指定生成bin文件路径。
  1. [onezeroone@ src]$ cat CMakeLists.txt
  2. ADD_EXECUTABLE(main main.c)
  3. INCLUDE_DIRECTORIES(/usr/include/hello)
  4. TARGET_LINK_LIBRARIES(main libhello.a)
指定静态库文件。

再cmake一把把。
  1. [onezeroone@ build]$ cmake ..
  2. CMake Warning (dev) in CMakeLists.txt:
  3.   No cmake_minimum_required command is present. A line of code such as

  4.     cmake_minimum_required(VERSION 2.8)

  5.   should be added at the top of the file. The version specified may be lower
  6.   if you wish to support older CMake versions for this project. For more
  7.   information run "cmake --help-policy CMP0000".
  8. This warning is for project developers. Use -Wno-dev to suppress it.

  9. -- Configuring done
  10. -- Generating done
  11. -- Build files have been written to: /home/onezeroone/work/cmake/ex-4/build
  12. [onezeroone@ build]$ make
  13. Scanning dependencies of target main
  14. [100%] Building C object bin/CMakeFiles/main.dir/main.o
  15. Linking C executable main
  16. [100%] Built target main
  17. [onezeroone@ build]$ cd bin/
  18. [onezeroone@ bin]$ ls
  19. CMakeFiles cmake_install.cmake main Makefile
  20. [onezeroone@ bin]$ ./main
  21. Hello world
OK我们已经Hello world了,确认下吧。
  1. [onezeroone@ bin]$ ldd main
  2.         linux-gate.so.=> (0x00e09000)
  3.         libc.so.=> /lib/libc.so.(0x00110000)
  4.         /lib/ld-linux.so.(0x0078f000)
不再是上一节的动态库了,OK,Done!

6:注
   这里只是简单的实例了cmake构建静态库,后面我们会逐渐丰富。

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

罗列一下cmake常用的命令。

CMake支持大写、小写、混合大小写的命令。

 

1. 添加头文件目录INCLUDE_DIRECTORIES

语法:

include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])

它相当于g++选项中的-I参数的作用,也相当于环境变量中增加路径到CPLUS_INCLUDE_PATH变量的作用。

include_directories(../../../thirdparty/comm/include)

 

2. 添加需要链接的库文件目录LINK_DIRECTORIES

语法:

link_directories(directory1 directory2 ...)

它相当于g++命令的-L选项的作用,也相当于环境变量中增加LD_LIBRARY_PATH的路径的作用。

link_directories("/home/server/third/lib")

 

3. 查找库所在目录FIND_LIBRARY

语法:

复制代码
A short-hand signature is:

find_library (<VAR> name1 [path1 path2 ...])
The general signature is:

find_library (
          <VAR>
          name | NAMES name1 [name2 ...] [NAMES_PER_DIR]
          [HINTS path1 [path2 ... ENV var]]
          [PATHS path1 [path2 ... ENV var]]
          [PATH_SUFFIXES suffix1 [suffix2 ...]]
          [DOC "cache documentation string"]
          [NO_DEFAULT_PATH]
          [NO_CMAKE_ENVIRONMENT_PATH]
          [NO_CMAKE_PATH]
          [NO_SYSTEM_ENVIRONMENT_PATH]
          [NO_CMAKE_SYSTEM_PATH]
          [CMAKE_FIND_ROOT_PATH_BOTH |
           ONLY_CMAKE_FIND_ROOT_PATH |
           NO_CMAKE_FIND_ROOT_PATH]
         )
复制代码

例子如下:

FIND_LIBRARY(RUNTIME_LIB rt /usr/lib  /usr/local/lib NO_DEFAULT_PATH)

cmake会在目录中查找,如果所有目录中都没有,值RUNTIME_LIB就会被赋为NO_DEFAULT_PATH

 

4. 添加需要链接的库文件路径LINK_LIBRARIES

语法:

link_libraries(library1 <debug | optimized> library2 ...)
复制代码
# 直接是全路径
link_libraries(“/home/server/third/lib/libcommon.a”)
# 下面的例子,只有库名,cmake会自动去所包含的目录搜索
link_libraries(iconv)

# 传入变量
link_libraries(${RUNTIME_LIB})
# 也可以链接多个
link_libraries("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so" "/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")
复制代码

可以链接一个,也可以多个,中间使用空格分隔.

 

5. 设置要链接的库文件的名称TARGET_LINK_LIBRARIES 

语法:

target_link_libraries(<target> [item1 [item2 [...]]]
                      [[debug|optimized|general] <item>] ...)
复制代码
# 以下写法都可以: 
target_link_libraries(myProject comm)       # 连接libhello.so库,默认优先链接动态库
target_link_libraries(myProject libcomm.a)  # 显示指定链接静态库
target_link_libraries(myProject libcomm.so) # 显示指定链接动态库

# 再如:
target_link_libraries(myProject libcomm.so)  #这些库名写法都可以。
target_link_libraries(myProject comm)
target_link_libraries(myProject -lcomm)
复制代码

6. 为工程生成目标文件
语法:
add_executable(<name> [WIN32] [MACOSX_BUNDLE]
               [EXCLUDE_FROM_ALL]
               source1 [source2 ...])

简单的例子如下:

add_executable(demo
        main.cpp
)

6. 最后贴一个完整的例子

复制代码
cmake_minimum_required (VERSION 2.6)

INCLUDE_DIRECTORIES(../../thirdparty/comm)

FIND_LIBRARY(COMM_LIB comm ../../thirdparty/comm/lib NO_DEFAULT_PATH)
FIND_LIBRARY(RUNTIME_LIB rt /usr/lib  /usr/local/lib NO_DEFAULT_PATH)

link_libraries(${COMM_LIB} ${RUNTIME_LIB})

ADD_DEFINITIONS(
-O3 -g -W -Wall
 -Wunused-variable -Wunused-parameter -Wunused-function -Wunused
 -Wno-deprecated -Woverloaded-virtual -Wwrite-strings
 -D__WUR= -D_REENTRANT -D_FILE_OFFSET_BITS=64 -DTIXML_USE_STL
)


add_library(lib_demo
        cmd.cpp
        global.cpp
        md5.cpp
)

link_libraries(lib_demo)
add_executable(demo
        main.cpp
)

# link library in static mode
target_link_libraries(demo libuuid.a)
复制代码


另外,使用cmake生成makefile之后,make edit_cache可以编辑编译选项。

不熟悉的命令可以去查找文档,贴个cmake3.0官方帮助文档地址
https://cmake.org/cmake/help/v3.0/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值