Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7407
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-22 00:14
文章分类

全部博文(6)

文章存档

2015年(6)

我的朋友
最近访客

分类: LINUX

2015-11-13 00:24:47

1:概述
   前面我们已经介绍了共享库和静态的库的构建,下面我们让动态库和静态库和平共处,这时候有人就说了,我们可以分别构建啊,说是这样说,但是这样难免会麻烦,何必呢,这时候有人又说了,我们可以将指令弄到一起,这样不就都构建了,一山不容二虎,我们还是要修改修改下的,所谓和谐嘛。

2:建立主工程目录
  1. [onezeroone@ ex-5]$ 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
跟上一节一样,只是内容要改。
我们现在不需要src下的任何东西,暂时不用关心。看看我们关心的东西吧。
  1. [onezeroone@ ex-5]$ cat CMakeLists.txt
  2. PROJECT(EX-5)
  3. ADD_SUBDIRECTORY(lib)

  1. [onezeroone@ lib]$ cat CMakeLists.txt
  2. SET(LIBHELLO_SRC hello.c)
  3. ADD_LIBRARY(hello SHARED STATIC ${LIBHELLO_SRC})
  4. #ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC})
  5. #INSTALL(TARGETS hello
  6. # ARCHIVE DESTINATION lib)
  7. #INSTALL(FILES hello.h DESTINATION include/hello)
我们先不考虑INSTALL问题,根据前面的约定,我们生成SHARED和STATIC库。

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

  14.     cmake_minimum_required(VERSION 2.8)

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

  19. -- Configuring done
  20. -- Generating done
  21. -- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
  22. [onezeroone@ build]$ make
  23. Scanning dependencies of target hello
  24. [100%] Building C object lib/CMakeFiles/hello.dir/hello.o
  25. Linking C static library libhello.a
  26. [100%] Built target hello
  27. [onezeroone@ build]$ ls ./lib/
  28. CMakeFiles cmake_install.cmake libhello.a Makefile
警告我们现在可以无视,我们看到生成了静态库,但是没有想要的动态库,因为target名字不可能一样啊,我们分开来再试试。
  1. [onezeroone@ lib]$ cat CMakeLists.txt
  2. SET(LIBHELLO_SRC hello.c)
  3. ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
  4. ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
  5. #INSTALL(TARGETS hello
  6. # ARCHIVE DESTINATION lib)
  7. #INSTALL(FILES hello.h DESTINATION include/hello)
我们清掉前面的结果,再cmake一把吧。
  1. [onezeroone@ build]$ rm -rf *
  2. [onezeroone@ build]$ ls
  3. [onezeroone@ build]$ cmake ..
  4. -- The C compiler identification is GNU
  5. -- The CXX compiler identification is GNU
  6. -- Check for working C compiler: /usr/bin/gcc
  7. -- Check for working C compiler: /usr/bin/gcc -- works
  8. -- Detecting C compiler ABI info
  9. -- Detecting C compiler ABI info - done
  10. -- Check for working CXX compiler: /usr/bin/c++
  11. -- Check for working CXX compiler: /usr/bin/c++ -- works
  12. -- Detecting CXX compiler ABI info
  13. -- Detecting CXX compiler ABI info - done
  14. CMake Warning (dev) in CMakeLists.txt:
  15.   No cmake_minimum_required command is present. A line of code such as

  16.     cmake_minimum_required(VERSION 2.8)

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

  21. -- Configuring done
  22. -- Generating done
  23. -- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
  24. [onezeroone@ build]$ make
  25. Scanning dependencies of target hello
  26. [ 50%] Building C object lib/CMakeFiles/hello.dir/hello.o
  27. Linking C shared library libhello.so
  28. [ 50%] Built target hello
  29. Scanning dependencies of target hello_s
  30. [100%] Building C object lib/CMakeFiles/hello_s.dir/hello.o
  31. Linking C static library libhello_s.a
  32. [100%] Built target hello_s
OK,两个库都有了,但是这显然不是我们想要的,我们要的是一个名字2种库文件。

3:发送指令啊
  1. [onezeroone@ lib]$ cat CMakeLists.txt
  2. SET(LIBHELLO_SRC hello.c)
  3. ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
  4. ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
  5. SET_TARGET_PROPERTIES(hello_s PROPERTIES OUTPUT_NAME "hello")
  6. #INSTALL(TARGETS hello
  7. # ARCHIVE DESTINATION lib)
  8. #INSTALL(FILES hello.h DESTINATION include/hello)
相当于给我们的hello_s起了个别名hello
SET_TARGET_PROPERTIES用法:
SET_TARGET_PROPERTIES(target1 target2 ...
                      PRPERTIES prop1 value1 prop2 value2 ...)
用来设置输出的名称,前面我们也看到了,可以设置动态库的版本号。

4:再cmake下吧
  1. [onezeroone@ build]$ rm -rf *
  2. [onezeroone@ build]$ ls
  3. [onezeroone@ build]$ cmake ..
  4. -- The C compiler identification is GNU
  5. -- The CXX compiler identification is GNU
  6. -- Check for working C compiler: /usr/bin/gcc
  7. -- Check for working C compiler: /usr/bin/gcc -- works
  8. -- Detecting C compiler ABI info
  9. -- Detecting C compiler ABI info - done
  10. -- Check for working CXX compiler: /usr/bin/c++
  11. -- Check for working CXX compiler: /usr/bin/c++ -- works
  12. -- Detecting CXX compiler ABI info
  13. -- Detecting CXX compiler ABI info - done
  14. CMake Warning (dev) in CMakeLists.txt:
  15.   No cmake_minimum_required command is present. A line of code such as

  16.     cmake_minimum_required(VERSION 2.8)

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

  21. -- Configuring done
  22. -- Generating done
  23. -- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
  24. [onezeroone@ build]$ make
  25. Scanning dependencies of target hello
  26. [ 50%] Building C object lib/CMakeFiles/hello.dir/hello.o
  27. Linking C shared library libhello.so
  28. [ 50%] Built target hello
  29. Scanning dependencies of target hello_s
  30. [100%] Building C object lib/CMakeFiles/hello_s.dir/hello.o
  31. Linking C static library libhello.a
  32. [100%] Built target hello_s
  33. [onezeroone@ build]$ ls ./lib/
  34. CMakeFiles cmake_install.cmake libhello.a libhello.so Makefile
呵呵,娘啊,终于看到了,而且名字相同,老版本的cmake可以会出现只有一个库文件的现象,这是因为构建新的target的时候,老版本的cmake可以会清理掉相同名字的库,没关系,我们可以让他不做清理操作。
为每个target添加属性:
SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(hello_s PROPERTIES CLEAN_DIRECT_OUTPUT 1)

5: 为动态库添加版本号
  1. [onezeroone@ lib]$ cat CMakeLists.txt
  2. SET(LIBHELLO_SRC hello.c)
  3. ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
  4. SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.0 SOVERSION 1)
  5. ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
  6. SET_TARGET_PROPERTIES(hello_s PROPERTIES OUTPUT_NAME "hello")
  7. #INSTALL(TARGETS hello
  8. # ARCHIVE DESTINATION lib)
  9. #INSTALL(FILES hello.h DESTINATION include/hello)
跟构建共享库一节一样,不多说了。

6:添加安装功能
  1. [onezeroone@ lib]$ cat CMakeLists.txt
  2. SET(LIBHELLO_SRC hello.c)
  3. ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
  4. SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.0 SOVERSION 1)
  5. ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
  6. SET_TARGET_PROPERTIES(hello_s PROPERTIES OUTPUT_NAME "hello")
  7. INSTALL(TARGETS hello
  8.         LIBRARY DESTINATION lib
  9.         ARCHIVE DESTINATION lib)
  10. INSTALL(FILES hello.h DESTINATION include/hello)
不多说了,只不过2个弄到一起而已。

7:该安装了吧
  1. [onezeroone@ build]$ rm -rf *
  2. [onezeroone@ build]$ cmake -DCMAKE_INSTALL_PREFIX=/usr ..
  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-5/build
  23. [onezeroone@ build]$ make
  24. Scanning dependencies of target hello
  25. [ 50%] Building C object lib/CMakeFiles/hello.dir/hello.o
  26. Linking C shared library libhello.so
  27. [ 50%] Built target hello
  28. Scanning dependencies of target hello_s
  29. [100%] Building C object lib/CMakeFiles/hello_s.dir/hello.o
  30. Linking C static library libhello.a
  31. [100%] Built target hello_s
  32. [onezeroone@ build]$ sudo make install
  33. [sudo] password for onezeroone:
  34. [ 50%] Built target hello
  35. [100%] Built target hello_s
  36. Install the project...
  37. -- Install configuration: ""
  38. -- Installing: /usr/lib/libhello.so.1.0
  39. -- Up-to-date: /usr/lib/libhello.so.1
  40. -- Up-to-date: /usr/lib/libhello.so
  41. -- Installing: /usr/include/hello/hello.h
前面安装过了,所以现在是update。
  1. [onezeroone@ build]$ ls /usr/lib/libhello.*
  2. /usr/lib/libhello.a /usr/lib/libhello.so /usr/lib/libhello.so.1 /usr/lib/libhello.so.1.0
OK了,看到了吧。

7:该用了吧
  1. [onezeroone@ ex-5]$ cat CMakeLists.txt
  2. PROJECT(EX-5)
  3. #ADD_SUBDIRECTORY(lib)
  4. ADD_SUBDIRECTORY(src bin)
该用的src了。
  1. [onezeroone@ src]$ cat CMakeLists.txt
  2. ADD_EXECUTABLE(main main.c)
  3. INCLUDE_DIRECTORIES(/usr/include/hello)
  4. TARGET_LINK_LIBRARIES(main hello)
这里我们只用了简单的hello,看看他能连到那个库,呵呵,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-5/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]$ ls ./bin/
  18. CMakeFiles cmake_install.cmake main Makefile
  19. [onezeroone@ build]$ ./bin/main
  20. Hello world

  1. [onezeroone@ build]$ ldd ./bin/main
  2.         linux-gate.so.1 => (0x002d8000)
  3.         libhello.so.1 => /usr/lib/libhello.so.1 (0x00f7e000)
  4.         libc.so.6 => /lib/libc.so.6 (0x0037c000)
  5.         /lib/ld-linux.so.2 (0x00872000)
呵呵,是动态库,那么静态库怎么办呢?我们直接改成libhello.a啊,呵呵。

8:优化
   前面我们都是指定路径的,那多麻烦啊,谁能记住呢,那么多库,那么多头文件,哎。
   我们系统环境变量,我们export库文件和头文件路径不就OK了吗,好
  1. [onezeroone@ build]$ export CMAKE_INCLUDE_PATH=/usr/include/hello
  2. [onezeroone@ build]$ export CMAKE_LIBRARY_PATH=/usr/lib
修改src/CMakeLists.txt文件:
  1. [onezeroone@ build]$ cat ../src/CMakeLists.txt
  2. ADD_EXECUTABLE(main main.c)
  3. FIND_PATH(Headers hello.h)
  4. IF(Headers)
  5.         INCLUDE_DIRECTORIES(${Headers})
  6. ENDIF(Headers)

  7. FIND_LIBRARY(SLibrarys libhello.a)
  8. FIND_LIBRARY(DLibrarys libhello.so)
  9. IF(SLibrarys)
  10.         TARGET_LINK_LIBRARIES(main ${SLibrarys})
  11. ELSE(SLibrarys)
  12.         TARGET_LINK_LIBRARIES(main ${DLibrarys})
  13. ENDIF(SLibrarys)
在export上面2个环境变量后,我们使用FIND_PATH指令,将从指定的目录中查找,懒的我们费事了。不过你也可能看到了,IF/ELSE/ENDIF有点那个,每个后面都要跟变量,那个烦啊。

9:真正make一把吧。
  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-5/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]$ ldd ./bin/main
  18.         linux-gate.so.1 => (0x00ec1000)
  19.         libc.so.6 => /lib/libc.so.6 (0x00593000)
  20.         /lib/ld-linux.so.2 (0x00a1c000)
  21. [onezeroone@ build]$ ./bin/main
  22. Hello world
可以看到main先找到静态库,所以先使用的是静态库。Done!

阅读(693) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~