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]#



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值