1:概述
有了上一节共享库的工作,这节的就简单多了。
2: 目录结构
- [onezeroone@ ex-4]$ tree
-
.
-
build
-
CMakeLists.txt
-
lib
-
CMakeLists.txt
-
hello.c
-
hello.h
-
src
-
CMakeLists.txt
-
main.c
-
-
3 directories, 6 files
3:文件内容
这里就不罗嗦了,直接看内容吧,参考前面共享库的制作,需要稍作修改就OK。
- [onezeroone@ ex-4]$ cat CMakeLists.txt
-
PROJECT(EX-4)
-
ADD_SUBDIRECTORY(lib)
现在是制作静态库时的状态,使用静态库的时候我们需要稍作修改。
- [onezeroone@ ex-4]$ cat ./lib/CMakeLists.txt
-
SET(LIBHELLO_SRC hello.c)
-
ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC})
-
INSTALL(TARGETS hello
-
ARCHIVE DESTINATION lib)
-
INSTALL(FILES hello.h DESTINATION include/hello)
需要修改只有2处,STATIC表示要生成静态库,ARCHIVE表示指定静态库的路径。hello文件跟上一节一样。
跟上一节一样,我们cmake一把吧。
- [onezeroone@ ex-4]$ cd build/
-
[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-4/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
呵呵,看到我们的静态库了。
4:安装静态库
安装前我们要制定安装路径,所以我们再重新构建吧。
- [onezeroone@ build]$ cmake -DCMAKE_INSTALL_PREFIX=/usr ..
-
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-4/build
-
[onezeroone@ build]$ make
-
[100%] Built target hello
-
[onezeroone@ build]$ sudo make install
-
[100%] Built target hello
-
Install the project...
-
-- Install configuration: ""
-
-- Installing: /usr/lib/libhello.a
-
-- Up-to-date: /usr/include/hello/hello.h
我们可以看到已经安装到指定的/usr目录下面了。
5:使用静态库
我们修改工程目录下的CMakeLists.txt文件
- [onezeroone@ ex-4]$ cat CMakeLists.txt
-
PROJECT(EX-4)
-
ADD_SUBDIRECTORY(src bin)
修改库路径为主程序路径,并指定生成bin文件路径。
- [onezeroone@ src]$ cat CMakeLists.txt
-
ADD_EXECUTABLE(main main.c)
-
INCLUDE_DIRECTORIES(/usr/include/hello)
-
TARGET_LINK_LIBRARIES(main libhello.a)
指定静态库文件。
再cmake一把把。
- [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-4/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]$ cd bin/
-
[onezeroone@ bin]$ ls
-
CMakeFiles cmake_install.cmake main Makefile
-
[onezeroone@ bin]$ ./main
-
Hello world
OK我们已经Hello world了,确认下吧。
- [onezeroone@ bin]$ ldd main
-
linux-gate.so.1 => (0x00e09000)
-
libc.so.6 => /lib/libc.so.6 (0x00110000)
-
/lib/ld-linux.so.2 (0x0078f000)
不再是上一节的动态库了,OK,Done!
6:注
这里只是简单的实例了cmake构建静态库,后面我们会逐渐丰富。
阅读(13305) | 评论(0) | 转发(1) |