1. ホーム
  2. メイヴン

cmakeの使用に関する注意事項

2022-02-26 05:56:10

I.

 ウェブ上にはたくさんのcmakeがあります。しかし、自分で使おうとすると、どれも自分が欲しいものではないことに気がつきます。

自分でノートを作る、使い方はすべて基本。


II.

 そこにコメントを書いて説明し、直接投稿しています。


1.

#cmake 

#1.
#1.
cmake_minimum_required(VERSION 2.8.2 FATAL_ERROR)
# Solution name
project(real_ex)

# Set macro definitions, there is ssss_STATIC predefined in VS's property page - "Configuration Properties - "C/C++ - > Preprocessor Items - "Preprocessor Definitions.
add_definitions(-Dsss_STATIC)

# Set solution binary generation paths, i.e. debug and release paths
set(REAL_ex_EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
# Set the include path
set(REAL_ex_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/include)
# This also includes the source code.
set(REAL_ex_SOURCE_PATH ${PROJECT_SOURCE_DIR}/src)

# Add subdirectories in the current level directory
add_subdirectory(src)



2.

#2.
# Macro definition for including multiple folder paths, used by: rw_include_module(file_a file_b)
macro(rw_include_module)
	foreach(module ${ARGN})
		include_directories(${REAL_ex_INCLUDE_PATH}/rw_modules/${module})
	endforeach()
endmacro(rw_include_module)

# Macro definition containing header files and linked lib libraries.
macro(rw_include_runtime_library)
	foreach(library ${ARGN})
		include_directories(${REAL_ex_INCLUDE_PATH}/${library})
		target_link_libraries(${PROJECT_NAME} ${library})
	endforeach()
endmacro(rw_include_runtime_library)

3.

#Add all source files, which can define the file format.
#Explain: GLOB_RECURSE
#file(GLOB_RECURSE variable [RELATIVE path]
# [FOLLOW_SYMLINKS] [globbingexpressions]...)
#GLOB_RECURSE is similar to GLOB, except that it will iterate through all files in the matching directory and files below the subdirectory.
#GLOB_RECURSE is similar to GLOB in that it will iterate over all files in the matching directory and files under the subdirectories. For subdirectories that are symbolic links, it will only iterate over these directories if FOLLOW_SYMLINKS specifies one or if cmake policy CMP0009 is not set to NEW.
#
# Usage of GLOB.
# file(GLOB variable [RELATIVE path] [globbingexpressions]...)
#
#GLOB produces a list of all files matching the globbing expressions and saves them to the variable.
The #Globbing expression is similar to a regular expression, but simpler. If the RELATIVE tag is specified, the
#returns a list of paths relative to the specified path. (The GLOB command is usually not recommended to collect a list of source files from the source tree.
#The reason is: if the CMakeLists.txt file has not changed.
# Even if files are added or removed from that source tree, the resulting build system will not know when to ask CMake to re-generate the build file.
macro(rw_module_source_all)
	file(GLOB_RECURSE rw_module_source_list . "*.cpp" "*.c" "*.h" "*.hpp" "*.inl")
	aux_source_directory(. rw_module_source_list)
endmacro(rw_module_source_all)



4.

# The following one didn't work, it says that you can set the project on vs to add the folders you want under the project (that is, the filters added in the VS project) #set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER ${REAL_ex_MODULES_PROJECT_FOLDER}) # Project generate dynamic link library. add_library(${PROJECT_NAME} SHARED ${rw_module_source_list}) #compile to generate static link library. add_library(${PROJECT_NAME} STATIC ${rw_module_source_list})

5.

#file copy, this took a long time to find, couldn't use it at first, tried install and configure_file.
#Configure file copy: configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${filename} ${CMAKE_CURRENT_BINARY_DIR})
# Found that the file is either 0K or 1K after configure_file copy, which is relatively small. Can't use it myself.
# File copy: FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/glut32Copy.lib DESTINATION ${CMAKE_BINARY_DIR}/Debug)
#
# Extract a description.
# configure_file: Copies a copy of a file to another location and modifies its contents.
# configure_file(<input> <output>
                #[_COPYONLY] [ESCAPE_QUOTES] [@ONLY])
#Copy the file <input> to <output> then replace the values of the variables referenced in the file contents.
#If <input> is a relative path, the base path it is evaluated against is the current source path. <input> must be a file, not a path.
#If <output> is a relative path, the base path it is evaluated against is the current binary file path.
# If <output> is an existing path, then the input file will be placed under that path with its original name.
# This command replaces any variables referenced in the input file in ${VAR} format or @VAR@ format, as if their values were determined by CMake.
# If a variable is not yet defined, it will be replaced with null. If the COPYONLY option is specified, then the variable is not expanded.
#If the ESCAPE_QUOTES option is specified, then all replaced variables will be escaped according to C rules. The file will be configured with the current value of the CMake variable.
#If the @ONLY option is specified, only variables in @VAR@ format will be replaced and variables in ${VAR} format will be ignored.
#This is more useful for configuring script files that use the ${VAR} format.
#Any definition statement like #cmakedefine VAR will be replaced with #define VAR or /* #undef VAR */, depending on the setting of the VAR variable in CMake.
#Any definition statement similar to #cmakedefine01 VAR will be replaced with #define VAR 1 or #define VAR 0, depending on whether the VAR is evaluated as TRUE or FALSE.

#(The purpose of configure_file is to make the variables in CMake available to normal files. --translation)

macro(rw_copy_file_to_build_project)
    foreach(filename ${ARGN})
		FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${filename} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
	endforeach()
endmacro(rw_copy_file_to_build_project)




6.

# Collect all CMakeLists.txt from all subfolders, into the specified variable.
file(GLOB_RECURSE rw_module_cmake_list . "CMakeLists.txt")

# Iterate through the source code for adding subfolders
foreach(rw_module_cmake ${rw_module_cmake_list})
	string(LENGTH ${PROJECT_SOURCE_DIR} module_prefix_length)
	string(SUBSTRING ${rw_module_cmake} ${module_prefix_length} -1 rw_module_name)
	string(SUBSTRING ${rw_module_name} 1 -1 rw_module_name)
	string(FIND ${rw_module_name} "/" rw_module_name_length)

	if(NOT (${rw_module_name_length} STREQUAL -1))
		string(SUBSTRING ${rw_module_name} 0 ${rw_module_name_length} rw_module_name)
		set(rw_current_module_name ${rw_module_name})
		add_subdirectory(${rw_module_name})
	endif()
endforeach()


















今回のプロジェクトで使用されているものは、比較的ベーシックなものもあります。不足な点があれば、遠慮なくお申し付けください。



オーバー!