CMake使用小计
转自:http://wenku.baidu.com/view/e3d4dad428ea81c758f57854.html
CMake 使用小记
创建库文件
使用 CMake 构建工程,创建一个共享库文件。
工程目录结构
/tliba/
|— CMakeLists.txt
|— bin
|— build
`— src
|— CMakeLists.txt
|— hello.cpp
`— hello.hpp
规范说明
bin 生成的目标文件存放目录;
build cmake 生成的工程编译文件存放目录;
src 源代码文件存放目录;
CMakeLists.txt
#begin
PROJECT(tliba)
SET( EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin )
SET( LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin )
SET( CMAKE_BUILD_TYPE Debug)
ADD_SUBDIRECTORY(src)
#end
src/CMakeLists.txt
#begin
SET( SRCS
hello.cpp
)
ADD_LIBRARY( helloa SHARED ${SRCS} )
#end
编译
先进入build 目录
[root@localhost tliba]# cd ./build
然后执行cmake ..命令
[root@localhost build]# cmake ..
-- Check for working C compiler: /usr/lib/ccache/gcc
-- Check for working C compiler: /usr/lib/ccache/gcc — works
-- Check size of void*
-- Check size of void* - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ — works
-- Configuring done
-- Generating done
-- Build files have been written to: /workspace/cmakepro/tliba/build
[root@localhost build]#
[root@localhost build]#
[root@localhost build]#
最后执行make 命令
[root@localhost build]# make
Scanning dependencies of target helloa
[100%] Building CXX object src/CMakeFiles/helloa.dir/hello.o
Linking CXX shared library ../../bin/libhelloa.so
[100%] Built target helloa
[root@localhost build]#
执行上述命令的结构是在 tliba/bin 目录下生成了共享库文件 libhelloa.so
创建可执行文件
使用 CMake 构建工程,创建一个可执行文件,并使用到共享库 helloa.so;
工程目录结构
/tuliba/
|— CMakeLists.txt
|— bin
|— build
|— include
| `— hello.hpp
|— lib
| `— libhelloa.so
`— src
|— CMakeLists.txt
`— main.cpp
规范说明
include 第三方头文件存放目录
lib 第三方库文件存放目录
CMakeLists.txt
#begin
PROJECT(tuliba)
SET( EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin )
SET( LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin )
SET( CMAKE_BUILD_TYPE Debug)
INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/include )
LINK_DIRECTORIES( ${PROJECT_SOURCE_DIR}/lib )
ADD_SUBDIRECTORY(src)
#end
src/CMakeLists.txt
#begin
SET( SRCS
main.cpp
)
ADD_EXECUTABLE(app ${SRCS})
TARGET_LINK_LIBRARIES( app helloa )
#end
编译
进入build 目录
[root@localhost tuliba]# cd build
执行cmake ..命令
[root@localhost build]# cmake ..
-- Check for working C compiler: /usr/lib/ccache/gcc
-- Check for working C compiler: /usr/lib/ccache/gcc — works
-- Check size of void*
-- Check size of void* - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ — works
-- Configuring done
-- Generating done
-- Build files have been written to: /workspace/cmakepro/tuliba/build
执行make 命令
[root@localhost build]# make
Scanning dependencies of target app
[100%] Building CXX object src/CMakeFiles/app.dir/main.o
Linking CXX executable ../../bin/app
[100%] Built target app
回到tuliba/bin 目录
[root@localhost build]# cd ..
[root@localhost tuliba]# cd bin/
[root@localhost bin]# ls
app
运行可执行文件app
[root@localhost bin]# ./app
Hello World
[root@localhost bin]#
还没有评论,来说两句吧...