更改以使CMake项目具有分层文件夹管理后,source_group()似乎不再正常工作。 CMake只是将所有内容转储到默认过滤器中。
我尝试了各种正则表达式来获取每个源文件的父文件的相对文件路径,甚至硬编码父CMakeLists.txt中的源文件只是为了查看是否是问题。在这些变化之后,我也尝试过多次重新生成VS项目。
以下是您的观看文件示例文件:
家长CMakeLists.txt
cmake_minimum_required (VERSION 3.3)
set(SRCS)
add_subdirectory(PlatformDetection)
include_directories (.)
add_library(SystemAbstraction STATIC ${SRCS})
set_property(TARGET SystemAbstraction PROPERTY FOLDER "Engine")
set_target_properties(SystemAbstraction PROPERTIES LINKER_LANGUAGE CXX)
install (TARGETS SystemAbstraction
ARCHIVE DESTINATION lib)
儿童CMakeLists.txt:
cmake_minimum_required (VERSION 3.3)
add_subdirectory(Compilers)
set(SRCS ${SRCS} PARENT_SCOPE)
和
cmake_minimum_required (VERSION 3.3)
set (COMPILER_DETECTION_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/CompilerDetection.h)
set (COMPILER_DETECTION_SRC ${CMAKE_CURRENT_SOURCE_DIR}/CompilerDetection.cpp)
source_group("Header Files\\Platform Detection\\Compilers" FILES ${COMPILER_DETECTION_HEADERS})
source_group("Source Files\\Platform Detection\\Compilers" FILES ${COMPILER_DETECTION_SRC})
set(SRCS ${SRCS} ${COMPILER_DETECTION_HEADERS} ${COMPILER_DETECTION_SRC} PARENT_SCOPE)
答案 0 :(得分:8)
我通过编写一个foreach循环修复了我的问题,该循环遍历源文件并按照它们的相对路径对它们进行分组,如果它们是头文件或源文件。然后在您定义库/可执行文件的每个位置将其作为宏运行。
foreach(FILE ${SRCS})
# Get the directory of the source file
get_filename_component(PARENT_DIR "${FILE}" DIRECTORY)
# Remove common directory prefix to make the group
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" GROUP "${PARENT_DIR}")
# Make sure we are using windows slashes
string(REPLACE "/" "\\" GROUP "${GROUP}")
# Group into "Source Files" and "Header Files"
if ("${FILE}" MATCHES ".*\\.cpp")
set(GROUP "Source Files${GROUP}")
elseif("${FILE}" MATCHES ".*\\.h")
set(GROUP "Header Files${GROUP}")
endif()
source_group("${GROUP}" FILES "${FILE}")
endforeach()
答案 1 :(得分:5)
我认为您需要添加set_property(GLOBAL PROPERTY USE_FOLDERS ON)
。
答案 2 :(得分:1)
这是@BlamKiwi答案的直接副本,但添加了一个绝对路径来修复仅使用相对路径的问题(并放入宏中) 我会评论他,但我没有声誉。
MACRO(GROUP_SRC SRCS)
foreach(FILE ${SRCS})
#convert source file to absolute
get_filename_component(ABSOLUTE_PATH "${FILE}" ABSOLUTE)
# Get the directory of the absolute source file
get_filename_component(PARENT_DIR "${ABSOLUTE_PATH}" DIRECTORY)
# Remove common directory prefix to make the group
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" GROUP "${PARENT_DIR}")
# Make sure we are using windows slashes
string(REPLACE "/" "\\" GROUP "${GROUP}")
# Group into "Source Files" and "Header Files"
if ("${FILE}" MATCHES ".*\\.cpp")
set(GROUP "Source Files${GROUP}")
elseif("${FILE}" MATCHES ".*\\.h")
set(GROUP "Header Files${GROUP}")
endif()
source_group("${GROUP}" FILES "${FILE}")
endforeach()
ENDMACRO(GROUP_SRC)