CMake 实例学习(五) 动/静态库共存
2015-04-17 12:27
288 查看
转载 http://blog.chinaunix.net/uid-25696269-id-1564981.html
如有版权问题,请联系QQ: 858668791
1:概述
前面我们已经介绍了共享库和静态的库的构建,下面我们让动态库和静态库和平共处,这时候有人就说了,我们可以分别构建啊,说是这样说,但是这样难免会麻烦,何必呢,这时候有人又说了,我们可以将指令弄到一起,这样不就都构建了,一山不容二虎,我们还是要修改修改下的,所谓和谐嘛。
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)
我们先不考虑INSTALL问题,根据前面的约定,我们生成SHARED和STATIC库。
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
警告我们现在可以无视,我们看到生成了静态库,但是没有想要的动态库,因为target名字不可能一样啊,我们分开来再试试。
[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)
我们清掉前面的结果,再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_s.a
[100%] Built target hello_s
OK,两个库都有了,但是这显然不是我们想要的,我们要的是一个名字2种库文件。
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)
相当于给我们的hello_s起了个别名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
呵呵,娘啊,终于看到了,而且名字相同,老版本的cmake可以会出现只有一个库文件的现象,这是因为构建新的target的时候,老版本的cmake可以会清理掉相同名字的库,没关系,我们可以让他不做清理操作。
为每个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)
不多说了,只不过2个弄到一起而已。
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
前面安装过了,所以现在是update。
[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
OK了,看到了吧。
7:该用了吧
[onezeroone@ ex-5]$ cat CMakeLists.txt
PROJECT(EX-5)
#ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(src bin)
该用的src了。
[onezeroone@ src]$ cat CMakeLists.txt
ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(/usr/include/hello)
TARGET_LINK_LIBRARIES(main hello)
这里我们只用了简单的hello,看看他能连到那个库,呵呵,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-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)
呵呵,是动态库,那么静态库怎么办呢?我们直接改成libhello.a啊,呵呵。
8:优化
前面我们都是指定路径的,那多麻烦啊,谁能记住呢,那么多库,那么多头文件,哎。
我们系统环境变量,我们export库文件和头文件路径不就OK了吗,好
[onezeroone@ build]$ export
CMAKE_INCLUDE_PATH=/usr/include/hello
[onezeroone@ build]$ export
CMAKE_LIBRARY_PATH=/usr/lib
修改src/CMakeLists.txt文件:
[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)
在export上面2个环境变量后,我们使用FIND_PATH指令,将从指定的目录中查找,懒的我们费事了。不过你也可能看到了,IF/ELSE/ENDIF有点那个,每个后面都要跟变量,那个烦啊。
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
可以看到main先找到静态库,所以先使用的是静态库。Done!
转载 http://blog.chinaunix.net/uid-25696269-id-1564981.html
如有版权问题,请联系QQ: 858668791
1:概述
前面我们已经介绍了共享库和静态的库的构建,下面我们让动态库和静态库和平共处,这时候有人就说了,我们可以分别构建啊,说是这样说,但是这样难免会麻烦,何必呢,这时候有人又说了,我们可以将指令弄到一起,这样不就都构建了,一山不容二虎,我们还是要修改修改下的,所谓和谐嘛。
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)
我们先不考虑INSTALL问题,根据前面的约定,我们生成SHARED和STATIC库。
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
警告我们现在可以无视,我们看到生成了静态库,但是没有想要的动态库,因为target名字不可能一样啊,我们分开来再试试。
[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)
我们清掉前面的结果,再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_s.a
[100%] Built target hello_s
OK,两个库都有了,但是这显然不是我们想要的,我们要的是一个名字2种库文件。
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)
相当于给我们的hello_s起了个别名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
呵呵,娘啊,终于看到了,而且名字相同,老版本的cmake可以会出现只有一个库文件的现象,这是因为构建新的target的时候,老版本的cmake可以会清理掉相同名字的库,没关系,我们可以让他不做清理操作。
为每个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)
不多说了,只不过2个弄到一起而已。
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
前面安装过了,所以现在是update。
[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
OK了,看到了吧。
7:该用了吧
[onezeroone@ ex-5]$ cat CMakeLists.txt
PROJECT(EX-5)
#ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(src bin)
该用的src了。
[onezeroone@ src]$ cat CMakeLists.txt
ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(/usr/include/hello)
TARGET_LINK_LIBRARIES(main hello)
这里我们只用了简单的hello,看看他能连到那个库,呵呵,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-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)
呵呵,是动态库,那么静态库怎么办呢?我们直接改成libhello.a啊,呵呵。
8:优化
前面我们都是指定路径的,那多麻烦啊,谁能记住呢,那么多库,那么多头文件,哎。
我们系统环境变量,我们export库文件和头文件路径不就OK了吗,好
[onezeroone@ build]$ export
CMAKE_INCLUDE_PATH=/usr/include/hello
[onezeroone@ build]$ export
CMAKE_LIBRARY_PATH=/usr/lib
修改src/CMakeLists.txt文件:
[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)
在export上面2个环境变量后,我们使用FIND_PATH指令,将从指定的目录中查找,懒的我们费事了。不过你也可能看到了,IF/ELSE/ENDIF有点那个,每个后面都要跟变量,那个烦啊。
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
可以看到main先找到静态库,所以先使用的是静态库。Done!
相关文章推荐
- CMake 实例学习(四) 构建静态库
- CMake 实例学习(3)构建静态库
- 第一个cmake,实例学习,项目管理
- CMake 实例学习(二) 内外之分
- CMake 实例学习(三) 构建共享库
- CMake 实例学习
- cmake学习(一)静态库与动态库构建
- CmakeList.txt学习 简单说明(PCL OpenCV 实例)
- cmake学习实例
- CMake学习笔记(3)——编译静态库和动态库
- cmake学习(一)静态库与动态库构建
- 【WIN32】CMake的学习总结 2——静态库
- cmake学习(一)静态库与动态库构建
- CMake入门学习+实战<三> 静态库与动态库构建
- CMake入门学习+实战<四> 使用外部共享库和头文件+实战:使用CLion调用zlib静态库实现pdf flatedecode解码
- CMake 实例学习(一) 开始
- linux学习——动态库和静态库实例分析
- opencv学习系列:实例练习(含多个实例)
- 5.Intent学习 实例
- .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧