看一下最终执行结果:
root@txp-virtual-machine:/home/txp/testmy/bin# ./main
i like the cmake
a=8
TXP嵌入式
二、动态库和静态库的学习:
这个话题就回到了我们最开始那位网友提出的需求了;不过我们还是从简单的开始来学习,然后慢慢深入。有的时候我们只需要编译出动态库、静态库,然后给其他程序来调用,那么在cmake里面如何实现呢?具体如下:
注我这用到的源文件内容(test1.c test2.c test1.h test2.h main.c都是一样的来测试
1、为了清晰明了,这里我重新创建了一个目录工程来演示:
root@txp-virtual-machine:/home/txp# mkdir testcmake
root@txp-virtual-machine:/home/txp/testcmake# mkdir build lib lib_test
root@txp-virtual-machine:/home/txp/testcmake# ls
build lib lib_test
然后在lib_test目录下放我们的源文件test1.c test1.h,并同时在lib_test目录创建一个CMakeLists.txt;在testcmake目录也创建一个CMakeLists.txt:
root@txp-virtual-machine:/home/txp/testcmake/lib_test# ls
CMakeLists.txt test1.c test1.h
/*lib_test目录下的CMakeLists.txt的内容:
aux_source_directory(. SRC_LIST)
add_library(test1_shared SHARED ${SRC_LIST})
add_library(test1_static STATIC ${SRC_LIST})
set_target_properties(test1_shared PROPERTIES OUTPUT_NAME "test1")
set_target_properties(test1_static PROPERTIES OUTPUT_NAME "test1")
set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
解释说明:
add_library: 生成动态库或静态库(第1个参数指定库的名字;第2个参数决定是动态还是静态,如果没有就默认静态;第3个参数指定生成库的源文件)。
set_target_properties: 设置输出的名称,还有其它功能,如设置库的版本号等等。
LIBRARY_OUTPUT_PATH: 库文件的默认输出路径,这里设置为工程目录下的lib目录。
testcmake目录下的CMakeLists.txt内容:
cmake_minimum_required(VERSION 2.8)
project(main)
add_subdirectory(lib_test)
最后架构如下:
root@txp-virtual-machine:/home/txp/testcmake# ls
build CMakeLists.txt lib lib_test
root@txp-virtual-machine:/home/txp/testcmake# tree
.
├── build
├── CMakeLists.txt
├── lib
└── lib_test
├── CMakeLists.txt
├── test1.c
└── test1.h
3 directories, 4 files
2、编译结果:
root@txp-virtual-machine:/home/txp/testcmake/build# cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/txp/testcmake/build
root@txp-virtual-machine:/home/txp/testcmake/build# make
Scanning dependencies of target test1_shared
[ 50%] Building C object lib_test/CMakeFiles/test1_shared.dir/test1.c.o
Linking C shared library ../../lib/libtest1.so
[ 50%] Built target test1_shared
Scanning dependencies of target test1_static
[100%] Building C object lib_test/CMakeFiles/test1_static.dir/test1.c.o
Linking C static library ../../lib/libtest1.a
[100%] Built target test1_static
root@txp-virtual-machine:/home/txp/testcmake/build# cd ../lib
root@txp-virtual-machine:/home/txp/testcmake/lib# ls
libtest1.a libtest1.so
注意编译规则上面讲过,这里不再重复
从lib目录下我们可以看到生成了生成了静态库和动态库:libtest1.a libtest1.so
2、对库进行链接:
现在我们使用刚才生成的库了。把build刚才的配置文件清空,并同时在testcmake目录下创建bin和src目录(跟刚才上面讲的差不多):
root@txp-virtual-machine:/home/txp/testcmake# mkdir src bin
root@txp-virtual-machine:/home/txp/testcmake# ls
bin build CMakeLists.txt lib lib_test src
root@txp-virtual-machine:/home/txp/testcmake# cd src
root@txp-virtual-machine:/home/txp/testcmake/src# vim main.c
并在testcmake目录下的CMakeLists.txt内容改成:
cmake_minimum_required(VERSION 2.8)
project(main)
add_subdirectory(lib_test)
add_subdirectory(src)
src目录下要创建一个CMakeLists.txt:
aux_source_directory(.SRC_LIST)
include_directories(../lib_test)
link_directories(${PROJECT_SOURCE_DIR}/lib)
add_executable(main ${SRC_LIST})
target_link_libraries(main test1)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
解释说明:
--link_directories: 添加非标准的共享库搜索路径。
--target_link_libraries: 把目标文件与库文件进行链接。