Last active
May 28, 2021 15:55
-
-
Save jeffguorg/74a5a12e1cfbb91de9d0c2877bb9f948 to your computer and use it in GitHub Desktop.
CMakeLists.txt for lazybones like me
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.3.0) | |
project(blablabla VERSION 0.1.0) | |
set(CMAKE_CXX_STANDARD 20) | |
#include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) | |
#conan_basic_setup() | |
#include_directories(${CONAN_INCLUDE_DIRS}) | |
#link_directories(${CONAN_LIB_DIRS}) | |
#link_libraries(${CONAN_LIBS} ${CONAN_SYSTEM_LIBS}) | |
message("-- Search files in ${CMAKE_CURRENT_SOURCE_DIR}") | |
set(accepted_extension .cc .cpp .cxx) | |
file(GLOB entries RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*") | |
foreach(filepath IN LISTS entries) | |
set(filepath "${CMAKE_CURRENT_SOURCE_DIR}/${filepath}") | |
if(IS_DIRECTORY "${filepath}" AND EXISTS "${filepath}/CMakeLists.txt" AND NOT IS_DIRECTORY "${filepath}/CMakeLists.txt") | |
message("-- including subdir ${filepath}") | |
add_subdirectory(${filepath}) | |
else() | |
get_filename_component(extension ${filepath} LAST_EXT) | |
if(extension IN_LIST accepted_extension) | |
get_filename_component(target ${filepath} NAME_WE) | |
message(" -- build ${filepath} -> ${target}") | |
add_executable(${target} ${filepath}) | |
endif() | |
endif() | |
endforeach() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message("-- Search files in ${CMAKE_CURRENT_SOURCE_DIR}") | |
set(accepted_extension .cc .cpp .cxx) | |
file(GLOB entries RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*") | |
foreach(filepath IN LISTS entries) | |
set(filepath "${CMAKE_CURRENT_SOURCE_DIR}/${filepath}") | |
if(IS_DIRECTORY "${filepath}" AND EXISTS "${filepath}/CMakeLists.txt" AND NOT IS_DIRECTORY "${filepath}/CMakeLists.txt") | |
message("-- including subdir ${filepath}") | |
add_subdirectory(${filepath}) | |
else() | |
get_filename_component(extension ${filepath} LAST_EXT) | |
if(extension IN_LIST accepted_extension) | |
get_filename_component(target ${filepath} NAME_WE) | |
message(" -- build ${filepath} -> ${target}") | |
add_executable(${target} ${filepath}) | |
endif() | |
endif() | |
endforeach() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
最近重新学C++,重新写C++,一个个文件创建也没有多麻烦……但是就是懒……就写了简单的一个个目录自己找源文件的逻辑。不需要自己一个个写add_executable,只要复制CMakeLists就够了。万一要改个配置啊什么的,也可以在目录里面的CMakeLists直接改就行。
还有一种写法是直接用file(GLOB_RECURSE ...)获取所有文件,然后每个文件根据路径生成一个target,一个CMakeLists搞定。如果只是打打oj,那这样做完全ok。但是这样有个问题是如果要对单个target做一些修改,比如说想要几个文件生成一个target或者生成lib或者什么的,就会非常麻烦。所以自己决定啦。要用这种更无脑但是比较难改的也很简单,只要把GLOB改成GLOB_RECURSE在CMAKE_SOURCE_DIR中搜索就可以了。