t1-cmake 的 helloworld
原始目录结构
guan@gvm:~/cmake/CMakeTest/t1$ tree
.
├── build
├── CMakeLists.txt
└── main.c
1 directory, 2 files
文件内容
PROJECT ( HELLO )
SET ( SRC_LIST main. c)
MESSAGE ( STATUS "This is BINARY dir" $ { HELLO_BINARY_DIR } )
MESSAGE ( STATUS "This is SOURCE dir" $ { HELLO_SOURCE_DIR } )
ADD_EXECUTABLE ( hello $ { SRC_LIST } )
#include <stdio.h>
int main ( )
{
printf ( "Hello Wold from t1 Main!\n" ) ;
return 0 ;
}
执行操作(外部构建)
guan@gvm:~/cmake/CMakeTest/t1$ cd build
guan@gvm:~/cmake/CMakeTest/t1/build$ cmake ..
......
guan@gvm:~/cmake/CMakeTest/t1/build$ make
......
guan@gvm:~/cmake/CMakeTest/t1/build$ ./hello
Hello Wold from t1 Main!
结构目录
guan@gvm:~/cmake/CMakeTest/t1/build$ cd ..
guan@gvm:~/cmake/CMakeTest/t1$ tree
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.10.2
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── feature_tests.bin
│ │ ├── feature_tests.c
│ │ ├── feature_tests.cxx
│ │ ├── hello.dir
│ │ │ ├── build.make
│ │ │ ├── C.includecache
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.internal
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── main.c.o
│ │ │ └── progress.make
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ ├── hello
│ └── Makefile
├── CMakeLists.txt
└── main.c
9 directories, 35 files
t2-更好一点的 Hello World
任务
2. 为工程添加一个子目录 src,用来放置工程源代码
3. 添加一个子目录 doc,用来放置这个工程的文档 hello.txt
4. 在工程目录添加文本文件 COPYRIGHT, README
5. 在工程目录添加一个 runhello.sh 脚本,用来调用 hello 二进制
6. 将构建后的目标文件放入构建目录的 bin 子目录
7. 最终安装这些文件:将 hello 二进制与 runhello.sh 安装至/usr/bin,将 doc 目录的内容以及 COPYRIGHT/README 安装到/usr/share/doc/cmake/t2
原始目录结构
guan@gvm:~/cmake/CMakeTest/t2$ tree
.
├── build
├── CMakeLists.txt
├── COPYRIGHT
├── doc
│ └── hello.txt
├── README
├── runhello.sh
└── src
├── CMakeLists.txt
└── main.c
3 directories, 7 files
文件内容
PROJECT ( HELLO )
ADD_SUBDIRECTORY ( src bin)
INSTALL ( FILES COPYRIGHT README DESTINATION share/ doc/ cmake/ t2)
INSTALL ( PROGRAMS runhello. sh DESTINATION bin)
INSTALL ( DIRECTORY doc/ DESTINATION share/ doc/ cmake/ t2)
hello
CMakeLists. txt
#include <stdio.h>
int main ( )
{
printf ( "Hello Wold from t2 Main!\n" ) ;
return 0 ;
}
执行操作
guan@gvm:~/cmake/CMakeTest/t2$ cd build
guan@gvm:~/cmake/CMakeTest/t2/build$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/t2/usr ..
......
guan@gvm:~/cmake/CMakeTest/t2/build$ make
......
guan@gvm:~/cmake/CMakeTest/t2/build$ make install
......
结构目录
guan@gvm:~/cmake/CMakeTest/t2/build$ cd /tmp/t2
guan@gvm:/tmp/t2$ tree
.
└── usr
├── bin
│ └── runhello.sh
└── share
└── doc
└── cmake
└── t2
├── COPYRIGHT
├── hello.txt
└── README
6 directories, 4 files
t3–静态库与动态库构建
任务
1. 建立一个静态库和动态库,提供 HelloFunc 函数供其他程序编程使用,HelloFunc
向终端输出 Hello World 字符串。
2. 安装头文件与共享库。
原始目录结构
guan@gvm:~/cmake/CMakeTest/t3$ tree
.
├── build
├── CMakeLists.txt
└── lib
├── CMakeLists.txt
├── hello.c
└── hello.h
2 directories, 4 files
文件内容
PROJECT ( HELLOLIB )
ADD_SUBDIRECTORY ( lib)
SET ( LIBHELLO_SRC hello. c)
ADD_LIBRARY ( hello SHARED $ { LIBHELLO_SRC } )
ADD_LIBRARY ( hello_static STATIC $ { LIBHELLO_SRC } )
SET_TARGET_PROPERTIES ( hello_static PROPERTIES OUTPUT_NAME "hello" )
SET_TARGET_PROPERTIES ( hello PROPERTIES VERSION 1.2 SOVERSION 1 )
GET_TARGET_PROPERTY ( OUTPUT_VALUE hello_static OUTPUT_NAME )
MESSAGE ( STATUS "This is the hello_static OUTPUT_NAME:" $ { OUTPUT_VALUE } )
INSTALL ( TARGETS hello hello_static
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
INSTALL ( FILES hello. h DESTINATION include / hello)
#ifndef HELLO_H
#define HELLO_H
#include <stdio.h>
void HelloFinc ( ) ;
#endif
#include "hello.h"
void HelloFunc ( )
{
printf ( "Hello World\n" ) ;
}
执行操作
guan@gvm:~/cmake/CMakeTest/t3$ cd build
guan@gvm:~/cmake/CMakeTest/t3/build$ cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
......
guan@gvm:~/cmake/CMakeTest/t3/build$ make
......
guan@gvm:~/cmake/CMakeTest/t3/build$ make install
(报错了,原因是当前用户没有对/usr/local/lib/目录的写权限,所以可以1换个位置安装2使用sudo提高权限)
guan@gvm:~/cmake/CMakeTest/t3/build$ rm -r *
guan@gvm:~/cmake/CMakeTest/t3/build$ cmake -DCMAKE_INSTALL_PREFIX=/tmp/cmake/t3 ..
......
guan@gvm:~/cmake/CMakeTest/t3/build$ make
......
guan@gvm:~/cmake/CMakeTest/t3/build$ make install
......
结构目录
guan@gvm:/tmp/cmake/t3$ tree
.
├── include
│ └── hello
│ └── hello.h
└── lib
├── libhello.a
├── libhello.so -> libhello.so.1
├── libhello.so.1 -> libhello.so.1.2
└── libhello.so.1.2
3 directories, 5 files
t4–使用外部共享库和头文件
任务
编写一个程序使用我们上一节构建的共享库
原始目录结构
guan@gvm:~/cmake/CMakeTest/t4$ tree
.
├── build
├── CMakeLists.txt
└── src
├── CMakeLists.txt
└── main.c
2 directories, 3 files
文件内容
PROJECT ( NEWHELLO )
ADD_SUBDIRECTORY ( src)
ADD_EXECUTABLE ( main main. c)
INCLUDE_DIRECTORIES ( / usr/ local/ include / hello)
TARGET_LINK_LIBRARIES ( main hello)
#include <hello.h>
int main ( )
{
HelloFunc ( ) ;
return 0 ;
}
执行操作
guan@gvm:~/cmake/CMakeTest/t4$ cd build
guan@gvm:~/cmake/CMakeTest/t4/build$ cmake ..
......
guan@gvm:~/cmake/CMakeTest/t4/build$ make
......
guan@gvm:~/cmake/CMakeTest/t4/build$ ldd ./src/main
linux-vdso.so.1 (0x00007ffdfe9db000)
libhello.so.1 => /usr/local/lib/libhello.so.1 (0x00007fa6581b5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa657dc4000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa6585b9000)
guan@gvm:~/cmake/CMakeTest/t4/build$ ./src/main
Hello World