在project目录下的树结构如下:
1 root@u18:~/cp/project# tree 2 . 3 ├── 3rd_lib //存放 第三方库 的目录 4 │ ├── libvirt-lxc.so 5 │ ├── libvirt-qemu.so 6 │ └── libvirt.so 7 ├── build //执行Cmake ..的目录 8 ├── CMakeLists.txt 9 ├── include //头文件目录 10 │ └── libvirt 11 │ ├── libvirt-domain.h 12 │ ├── libvirt-domain-snapshot.h 13 │ ├── libvirt-event.h 14 │ ├── libvirt.h 15 │ ├── libvirt-host.h 16 │ ├── libvirt-interface.h 17 │ ├── libvirt-lxc.h 18 │ ├── libvirt-network.h 19 │ ├── libvirt-nodedev.h 20 │ ├── libvirt-nwfilter.h 21 │ ├── libvirt-qemu.h 22 │ ├── libvirt-secret.h 23 │ ├── libvirt-storage.h 24 │ ├── libvirt-stream.h 25 │ └── virterror.h 26 └── src //代码目录 27 ├── LibvirtConfig.h 28 ├── LibvirtConfig.h.in 29 ├── libvirt.cpp 30 └── Libvirt.h.in 31 32 5 directories, 23 files
CMakeList文件内容如下:
1 cmake_minimum_required (VERSION 2.6) 2 3 #PROJECT(projectname [CXX] [C] [Java]) 编译语言 4 project (Libvirt CXX) 5 6 #The version number 7 set (Libvirt_VERSION_MAJOR 1) 8 set (Libvirt_VERSION_MINOR 0) 9 10 #configure a header file to pass some of the source code 11 configure_file ( 12 "${PROJECT_SOURCE_DIR}/src/LibvirtConfig.h.in" 13 "${PROJECT_BINARY_DIR}/src/LibvirtConfig.h" 14 ) 15 16 # equal to echo in shell 17 MESSAGE(STATUS "This is source dir: " ${PROJECT_SOURCE_DIR}) 18 MESSAGE(STATUS "This is binary dir: " ${PROJECT_BINARY_DIR}) 19 20 #add sub dir and find CMakeList.txt in the sub dir, done it 21 #ADD_SUBDIRECTORY(subdir) 22 23 #将一个文件下的编译用的源文件添加到一个宏列表中 24 #AUX_SOURCE_DIRECTORY(. SRC_LIST) 25 #只将.cc .cpp .c 的文件添加到SRC_LIST中,.h除外 26 27 28 #将.h文件也添加进来,使用 29 INCLUDE_DIRECTORIES("${PROJECT_BINARY_DIR}/src") 30 FILE(GLOB_RECURSE SOURCE_FILE ./src/*.cpp ./src/*.c) 31 FILE(GLOB_RECURSE HEADER_FILE *.h *.hpp) 32 33 MESSAGE(STATUS "This is source dir: " ${SOURCE_FILE}) 34 #MESSAGE(STATUS "This is header dir: " ${HEADER_FILE}) 35 #add the binary tree to the search path for include files so that we will find LibvirtConfig.h 36 #include_directories ("${PROJECT_BINARY_DIR}/src", "${PROJECT_BINARY_DIR}/INCLUDE/libvirt") 37 38 #add the 3rd_lib 39 #link_directories("${PROJECT_BINARY_DIR}/3rd_Lib") 40 FILE(GLOB_RECURSE LIB_FILE libvirt.so*) 41 42 MESSAGE(STATUS "This is 3rd_lib file : " ${LIB_FILE}) 43 44 #add the executable 45 add_executable(Libvirt ${SOURCE_FILE}) 46 47 #link 48 TARGET_LINK_LIBRARIES(Libvirt ${LIB_FILE})
操作步骤:
1、进入build目录,执行cmake .. (因为CMakelist文件在上一级目录中),生成Makefile文件
2、执行make即可
时间: 2024-10-30 23:35:23