前面我们已经介绍了共享库和静态的库的构建,下面我们让动态库和静态库和平共处,这时候有人就说了,我们可以分别构建啊,说是这样说,但是这样难免会麻烦,何必呢,这时候有人又说了,我们可以将指令弄到一起,这样不就都构建了,一山不容二虎,我们还是要修改修改下的,所谓和谐嘛。
2:建立主工程目录
- [onezeroone@ ex-5]$ tree
-
.
-
build
-
CMakeLists.txt
-
lib
-
CMakeLists.txt
-
hello.c
-
hello.h
-
src
-
CMakeLists.txt
-
main.c
-
- 3 directories, 6 files
我们现在不需要src下的任何东西,暂时不用关心。看看我们关心的东西吧。
- [onezeroone@ ex-5]$ cat CMakeLists.txt
-
PROJECT(EX-5)
- ADD_SUBDIRECTORY(lib)
- [onezeroone@ lib]$ cat CMakeLists.txt
-
SET(LIBHELLO_SRC hello.c)
-
ADD_LIBRARY(hello SHARED STATIC ${LIBHELLO_SRC})
-
#ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC})
-
#INSTALL(TARGETS hello
-
# ARCHIVE DESTINATION lib)
- #INSTALL(FILES hello.h DESTINATION include/hello)
cmake一把把。
- [onezeroone@ build]$ cmake ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - done
-
-- Check for working CXX compiler: /usr/bin/c++
-
-- Check for working CXX compiler: /usr/bin/c++ -- works
-
-- Detecting CXX compiler ABI info
-
-- Detecting CXX compiler ABI info - done
-
CMake Warning (dev) in CMakeLists.txt:
-
No cmake_minimum_required command is present. A line of code such as
-
-
cmake_minimum_required(VERSION 2.8)
-
-
should be added at the top of the file. The version specified may be lower
-
if you wish to support older CMake versions for this project. For more
-
information run "cmake --help-policy CMP0000".
-
This warning is for project developers. Use -Wno-dev to suppress it.
-
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
-
[onezeroone@ build]$ make
-
Scanning dependencies of target hello
-
[100%] Building C object lib/CMakeFiles/hello.dir/hello.o
-
Linking C static library libhello.a
-
[100%] Built target hello
-
[onezeroone@ build]$ ls ./lib/
- CMakeFiles cmake_install.cmake libhello.a Makefile
- [onezeroone@ lib]$ cat CMakeLists.txt
-
SET(LIBHELLO_SRC hello.c)
-
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
-
ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
-
#INSTALL(TARGETS hello
-
# ARCHIVE DESTINATION lib)
- #INSTALL(FILES hello.h DESTINATION include/hello)
- [onezeroone@ build]$ rm -rf *
-
[onezeroone@ build]$ ls
-
[onezeroone@ build]$ cmake ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - done
-
-- Check for working CXX compiler: /usr/bin/c++
-
-- Check for working CXX compiler: /usr/bin/c++ -- works
-
-- Detecting CXX compiler ABI info
-
-- Detecting CXX compiler ABI info - done
-
CMake Warning (dev) in CMakeLists.txt:
-
No cmake_minimum_required command is present. A line of code such as
-
-
cmake_minimum_required(VERSION 2.8)
-
-
should be added at the top of the file. The version specified may be lower
-
if you wish to support older CMake versions for this project. For more
-
information run "cmake --help-policy CMP0000".
-
This warning is for project developers. Use -Wno-dev to suppress it.
-
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
-
[onezeroone@ build]$ make
-
Scanning dependencies of target hello
-
[ 50%] Building C object lib/CMakeFiles/hello.dir/hello.o
-
Linking C shared library libhello.so
-
[ 50%] Built target hello
-
Scanning dependencies of target hello_s
-
[100%] Building C object lib/CMakeFiles/hello_s.dir/hello.o
-
Linking C static library libhello_s.a
- [100%] Built target hello_s
3:发送指令啊
- [onezeroone@ lib]$ cat CMakeLists.txt
-
SET(LIBHELLO_SRC hello.c)
-
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
-
ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
-
SET_TARGET_PROPERTIES(hello_s PROPERTIES OUTPUT_NAME "hello")
-
#INSTALL(TARGETS hello
-
# ARCHIVE DESTINATION lib)
- #INSTALL(FILES hello.h DESTINATION include/hello)
SET_TARGET_PROPERTIES用法:
SET_TARGET_PROPERTIES(target1 target2 ...
PRPERTIES prop1 value1 prop2 value2 ...)
用来设置输出的名称,前面我们也看到了,可以设置动态库的版本号。
4:再cmake下吧
- [onezeroone@ build]$ rm -rf *
-
[onezeroone@ build]$ ls
-
[onezeroone@ build]$ cmake ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - done
-
-- Check for working CXX compiler: /usr/bin/c++
-
-- Check for working CXX compiler: /usr/bin/c++ -- works
-
-- Detecting CXX compiler ABI info
-
-- Detecting CXX compiler ABI info - done
-
CMake Warning (dev) in CMakeLists.txt:
-
No cmake_minimum_required command is present. A line of code such as
-
-
cmake_minimum_required(VERSION 2.8)
-
-
should be added at the top of the file. The version specified may be lower
-
if you wish to support older CMake versions for this project. For more
-
information run "cmake --help-policy CMP0000".
-
This warning is for project developers. Use -Wno-dev to suppress it.
-
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
-
[onezeroone@ build]$ make
-
Scanning dependencies of target hello
-
[ 50%] Building C object lib/CMakeFiles/hello.dir/hello.o
-
Linking C shared library libhello.so
-
[ 50%] Built target hello
-
Scanning dependencies of target hello_s
-
[100%] Building C object lib/CMakeFiles/hello_s.dir/hello.o
-
Linking C static library libhello.a
-
[100%] Built target hello_s
-
[onezeroone@ build]$ ls ./lib/
- CMakeFiles cmake_install.cmake libhello.a libhello.so Makefile
为每个target添加属性:
SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(hello_s PROPERTIES CLEAN_DIRECT_OUTPUT 1)
5: 为动态库添加版本号
- [onezeroone@ lib]$ cat CMakeLists.txt
-
SET(LIBHELLO_SRC hello.c)
-
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
-
SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.0 SOVERSION 1)
-
ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
-
SET_TARGET_PROPERTIES(hello_s PROPERTIES OUTPUT_NAME "hello")
-
#INSTALL(TARGETS hello
-
# ARCHIVE DESTINATION lib)
- #INSTALL(FILES hello.h DESTINATION include/hello)
6:添加安装功能
- [onezeroone@ lib]$ cat CMakeLists.txt
-
SET(LIBHELLO_SRC hello.c)
-
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
-
SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.0 SOVERSION 1)
-
ADD_LIBRARY(hello_s STATIC ${LIBHELLO_SRC})
-
SET_TARGET_PROPERTIES(hello_s PROPERTIES OUTPUT_NAME "hello")
-
INSTALL(TARGETS hello
-
LIBRARY DESTINATION lib
-
ARCHIVE DESTINATION lib)
- INSTALL(FILES hello.h DESTINATION include/hello)
7:该安装了吧
- [onezeroone@ build]$ rm -rf *
-
[onezeroone@ build]$ cmake -DCMAKE_INSTALL_PREFIX=/usr ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - done
-
-- Check for working CXX compiler: /usr/bin/c++
-
-- Check for working CXX compiler: /usr/bin/c++ -- works
-
-- Detecting CXX compiler ABI info
-
-- Detecting CXX compiler ABI info - done
-
CMake Warning (dev) in CMakeLists.txt:
-
No cmake_minimum_required command is present. A line of code such as
-
-
cmake_minimum_required(VERSION 2.8)
-
-
should be added at the top of the file. The version specified may be lower
-
if you wish to support older CMake versions for this project. For more
-
information run "cmake --help-policy CMP0000".
-
This warning is for project developers. Use -Wno-dev to suppress it.
-
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
-
[onezeroone@ build]$ make
-
Scanning dependencies of target hello
-
[ 50%] Building C object lib/CMakeFiles/hello.dir/hello.o
-
Linking C shared library libhello.so
-
[ 50%] Built target hello
-
Scanning dependencies of target hello_s
-
[100%] Building C object lib/CMakeFiles/hello_s.dir/hello.o
-
Linking C static library libhello.a
-
[100%] Built target hello_s
-
[onezeroone@ build]$ sudo make install
-
[sudo] password for onezeroone:
-
[ 50%] Built target hello
-
[100%] Built target hello_s
-
Install the project...
-
-- Install configuration: ""
-
-- Installing: /usr/lib/libhello.so.1.0
-
-- Up-to-date: /usr/lib/libhello.so.1
-
-- Up-to-date: /usr/lib/libhello.so
- -- Installing: /usr/include/hello/hello.h
- [onezeroone@ build]$ ls /usr/lib/libhello.*
- /usr/lib/libhello.a /usr/lib/libhello.so /usr/lib/libhello.so.1 /usr/lib/libhello.so.1.0
7:该用了吧
- [onezeroone@ ex-5]$ cat CMakeLists.txt
-
PROJECT(EX-5)
-
#ADD_SUBDIRECTORY(lib)
- ADD_SUBDIRECTORY(src bin)
- [onezeroone@ src]$ cat CMakeLists.txt
-
ADD_EXECUTABLE(main main.c)
-
INCLUDE_DIRECTORIES(/usr/include/hello)
- TARGET_LINK_LIBRARIES(main hello)
- [onezeroone@ build]$ cmake ..
-
CMake Warning (dev) in CMakeLists.txt:
-
No cmake_minimum_required command is present. A line of code such as
-
-
cmake_minimum_required(VERSION 2.8)
-
-
should be added at the top of the file. The version specified may be lower
-
if you wish to support older CMake versions for this project. For more
-
information run "cmake --help-policy CMP0000".
-
This warning is for project developers. Use -Wno-dev to suppress it.
-
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
-
[onezeroone@ build]$ make
-
Scanning dependencies of target main
-
[100%] Building C object bin/CMakeFiles/main.dir/main.o
-
Linking C executable main
-
[100%] Built target main
-
[onezeroone@ build]$ ls ./bin/
-
CMakeFiles cmake_install.cmake main Makefile
-
[onezeroone@ build]$ ./bin/main
- Hello world
- [onezeroone@ build]$ ldd ./bin/main
-
linux-gate.so.1 => (0x002d8000)
-
libhello.so.1 => /usr/lib/libhello.so.1 (0x00f7e000)
-
libc.so.6 => /lib/libc.so.6 (0x0037c000)
- /lib/ld-linux.so.2 (0x00872000)
8:优化
前面我们都是指定路径的,那多麻烦啊,谁能记住呢,那么多库,那么多头文件,哎。
我们系统环境变量,我们export库文件和头文件路径不就OK了吗,好
- [onezeroone@ build]$ export CMAKE_INCLUDE_PATH=/usr/include/hello
- [onezeroone@ build]$ export CMAKE_LIBRARY_PATH=/usr/lib
- [onezeroone@ build]$ cat ../src/CMakeLists.txt
-
ADD_EXECUTABLE(main main.c)
-
FIND_PATH(Headers hello.h)
-
IF(Headers)
-
INCLUDE_DIRECTORIES(${Headers})
-
ENDIF(Headers)
-
-
FIND_LIBRARY(SLibrarys libhello.a)
-
FIND_LIBRARY(DLibrarys libhello.so)
-
IF(SLibrarys)
-
TARGET_LINK_LIBRARIES(main ${SLibrarys})
-
ELSE(SLibrarys)
-
TARGET_LINK_LIBRARIES(main ${DLibrarys})
- ENDIF(SLibrarys)
9:真正make一把吧。
- [onezeroone@ build]$ cmake ..
-
CMake Warning (dev) in CMakeLists.txt:
-
No cmake_minimum_required command is present. A line of code such as
-
-
cmake_minimum_required(VERSION 2.8)
-
-
should be added at the top of the file. The version specified may be lower
-
if you wish to support older CMake versions for this project. For more
-
information run "cmake --help-policy CMP0000".
-
This warning is for project developers. Use -Wno-dev to suppress it.
-
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/cmake/ex-5/build
-
[onezeroone@ build]$ make
-
Scanning dependencies of target main
-
[100%] Building C object bin/CMakeFiles/main.dir/main.o
-
Linking C executable main
-
[100%] Built target main
-
[onezeroone@ build]$ ldd ./bin/main
-
linux-gate.so.1 => (0x00ec1000)
-
libc.so.6 => /lib/libc.so.6 (0x00593000)
-
/lib/ld-linux.so.2 (0x00a1c000)
-
[onezeroone@ build]$ ./bin/main
- Hello world