CMake 使用小记
杨波 2008-5-23
创建库文件
使用CMake构建工程,创建一个共享库文件。
工程目录结构
/tliba/
|--
|-- bin
|-- build
`-- src
|--
|-- hello.cpp
`-- hello.hpp
规范说明
bin 生成的目标文件存放目录;
build cmake生成的工程编译文件存放目录;
src 源代码文件存放目录;
<
#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
#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/
|--
|-- bin
|-- build
|-- include
| `-- hello.hpp
|-- lib
| `-- libhelloa.so
`-- src
|--
`-- main.cpp
规范说明
include 第三方头文件存放目录
lib 第三方库文件存放目录
<
#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
#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
cmake如何使用回到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]#
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论