A brief summary of CMake learning __cmake

Source: Internet
Author: User
Tags regex replace win32

Assuming there is Vegagis engineering, the directory structure of the project is as follows:

#--vegagis
# |--src Source Files directory
# |--gui Interface Engineering, Output type: DLL, dependent on Qt Qtcore, Qtgui, Qtxml
# |--pending works in the source file, do not want to join the project
# # # # In order to achieve cross-platform operation, Win32 and Linux directory implementation of the same set of interfaces, Win32 does not compile the Linux directory, Linux does not compile the Win32 directory, but the project contains the two directories
# Implementation under the Win32 of |--win32
# |--linux Linux Implementation
# |--app application, Output type: exe, it relies on GUI
# |--include Installation-time header file output directory
# |--gui
# |--app
# |--bin output path, storing DLL and EXE
# |--debug
# |--plugins
# |--build CMake build path, store generated engineering files, etc.

#////////////////////////////////////////////
#vegagis目录下的CMakeLists. txt
#///////////////////////////////////////////

Cmake_minimum_required (VERSION 2.8)

#工程名
PROJECT (Vegagis)

#工程文件中使用相对路径
SET (cmake_suppress_regeneration 1)
SET (Cmake_use_relative_paths on)

#支持IF (A) ELSE ()
SET (Cmake_allow_loose_loop_constructs on)

#定义工程的顶级路径
SET (Projdir ${cmake_current_source_dir})

#定义源文件目录
SET (Srcdir ${projdir}/src)

#设置输出路径
SET (Executable_output_path ${project_binary_dir}/bin)
SET (Library_output_path ${project_binary_dir}/bin)

#设置安装路径
SET (Cmake_install_prefix ${projdir}/bin)

#定义头文件安装目录
SET (Vgis_include_dir ${cmake_current_source_dir}/headers)

#根据操作系统不同而设置不同的路径
IF (WIN32)
SET (Gdal_hdrs "E:/lib/gdal/include")
ELSE ()
SET (Gdal_hdrs "/home/sunsc/gdal/")
ENDIF ()

#设置头文件的引用路径
Include_directories (
${gdal_hdrs}
)

#设置引用库路径
SET (LIBS ${library_output_path})
Link_directories (${libs})

#设置依赖库Qt4
SET (qt_min_version 4.5.2)
Find_package (Qt4 ${qt_min_version} components qtcore Qtgui qtxml REQUIRED)
INCLUDE (${qt_use_file})

#宏, implementing the Directory Grouping (filter) features of Visio Studio
Macro (Source_group_by_dir source_files)
if (MSVC)
Set (Sgbd_cur_dir ${cmake_current_source_dir})
foreach (Sgbd_file ${${source_files}})
String (REGEX REPLACE ${sgbd_cur_dir}//(. * *)//1 Sgbd_fpath ${sgbd_file})
String (REGEX REPLACE/(. * *)/.* "
1 Sgbd_group_name ${sgbd_fpath})
String (COMPARE EQUAL ${sgbd_fpath} ${sgbd_group_name} sgbd_nogroup)
String (REPLACE/""//"Sgbd_group_name ${sgbd_group_name})
if (Sgbd_nogroup)
Set (sgbd_group_name "//")
endif (Sgbd_nogroup)
Source_group (${sgbd_group_name} FILES ${sgbd_file})
Endforeach (Sgbd_file)
endif (MSVC)
Endmacro (Source_group_by_dir)

#添加子目录
Add_subdirectory (SRC)

#////////////////////////////////////////////
The CMakeLists.txt in the/SRC directory of #vegagis
#///////////////////////////////////////////
Add_subdirectory (GUI)
Add_subdirectory (APP)
#添加依赖关系
Add_dependencies (APP GUI)

#////////////////////////////////////////////
The CMakeLists.txt in the/src/gui directory of #vegagis
#///////////////////////////////////////////

SET (target_name GUI)

#预定义宏,-D option
Add_definitions (-dvmap_gui_lib)

#ui文件
SET (Gui_uis Vmapmainwindow.ui)

#源文件类型为 *.h* and *.c*
FILE (glob_recurse gui_srcs ${cmake_current_source_dir}/*.c* ${cmake_current_source_dir}/*.h*)
#FILE目前还不支持类似EXCLUDE的特性, so the file in pending included in the middle of the previous statement, and we removed it
FILE (GLOB PENDING ${cmake_current_source_dir}/pending/*)
LIST (Remove_item Gui_srcs ${pending}) #新生成的GUI_SRCS就不包括pending目录的源文件了

#参与QT header file for MOC
FILE (glob_recurse Gui_moc_hdrs vmapview.h transformlistener.hpp)

#设置资源qrc文件
SET (Gui_rccs UI.QRC)

QT4_WRAP_UI (Gui_uis_h ${gui_uis})
Qt4_wrap_cpp (Gui_moc_srcs ${gui_moc_hdrs})
Qt4_add_resources (Gui_rcc_srcs ${gui_rccs})

#由于. The UI file output path is in the build directory, so you need to add the ${cmake_current_binary_dir} directory
Include_directories (${srcdir}/gui ${cmake_current_binary_dir})

#设置需要加入到工程中, but you do not need to compile the source files
IF (WIN32)
FILE (glob_recurse platform_src "${cmake_current_source_dir}/linux/*")
ELSE ()
FILE (glob_recurse platform_src "${cmake_current_source_dir}/win32/*")
ENDIF ()
FILE (Glob_recurse exclude_src ${platform_src})
Set_source_files_properties (${exclude_src} PROPERTIES header_file_only true) #设置源文件属性为不参与编译

#进行源代码分组
Source_group_by_dir (GUI_SRCS)

#动态链接库
Add_library (${target_name} SHARED ${gui_srcs} ${gui_uis_h} ${gui_rcc_srcs})

Target_link_libraries (${target_name} ${qt_libraries})

#//////////////////////////////////////
The CMakeLists.txt under the/src/app of the #vegagis
#//////////////////////////////////////
SET (target_name APP)

SET (App_uis Vmapmainwindow.ui)

FILE (glob_recurse app_srcs ${cmake_current_source_dir}/*.c* ${cmake_current_source_dir}/*.h*)

SET (App_moc_hdrs vmapmainwindow.h)

SET (App_rccs VMAPMAINWINDOW.QRC)

QT4_WRAP_UI (App_uis_h ${app_uis})
Qt4_wrap_cpp (App_moc_srcs ${app_moc_hdrs})
Qt4_add_resources (App_rcc_srcs ${app_rccs})

#添加头文件
Include_directories (${cmake_current_binary_dir} ${srcdir}/apps/qt/${target_name})

#源文件分组
Source_group_by_dir (APP_SRCS)

#exe应用程序
Add_executable (${target_name} ${app_srcs} ${app_rcc_srcs} ${app_uis_h})

#TARGET_LINK_LIBRARIES语句必须放到ADD_EXECUTABLE语句之后, or cause a link error
Target_link_libraries (${target_name} ${qt_libraries} GUI)


#其他
Problems with #1 and output paths
#如果要修改输出路径, usually set (Library_output_path XXX), but under WIN32, will automatically be placed in the ${xxx}/debug/release directory, such as a plug-in, Hopefully in the plugins subdirectory of the output directory, but if set to set (Library_output_path xxx/plugins), the file output directory is ${xxx}/plugins/debug, which is obviously not what we want. The practice is to set up PREFIX, such as Set_target_properties (ABC PROPERTIES PREFIX plugins/). Similarly, if the output path is placed, regardless of debug or release, set prefix to. /。

#2, Glob_recurse HEADERS relative ${cmake_current_source_dir} *.h*)
FOREACH (header $) by directory hierarchy when the header file is installed {HEADERS})
 string (. *) [///] "DIR ${header}"
 install (FILES ${header} destination ${vgis_include_ Dir}/${dir})
Endforeach (HEADER ${headers})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.