diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..38f7d0a --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "compilerPath": "C:\\Program Files\\mingw64\\bin\\gcc.exe", + "cStandard": "c23", + "cppStandard": "c++17", + "intelliSenseMode": "gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6848ac3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,77 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "optional": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "cfenv": "cpp", + "map": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "ctime": "cpp", + "ratio": "cpp", + "iomanip": "cpp", + "bitset": "cpp", + "variant": "cpp", + "typeindex": "cpp", + "charconv": "cpp", + "cinttypes": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "csetjmp": "cpp", + "csignal": "cpp", + "cstring": "cpp", + "cuchar": "cpp", + "forward_list": "cpp", + "list": "cpp", + "unordered_set": "cpp", + "functional": "cpp", + "iterator": "cpp", + "numeric": "cpp", + "random": "cpp", + "regex": "cpp", + "set": "cpp", + "future": "cpp", + "mutex": "cpp", + "scoped_allocator": "cpp", + "shared_mutex": "cpp", + "thread": "cpp", + "valarray": "cpp" + }, + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6db2949 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++.exe build active file", + "command": "C:\\Program Files\\mingw64\\bin\\g++.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe", + "-std=c++2a" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "compiler: \"C:\\Program Files\\mingw64\\bin\\g++.exe\"" + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index cdec6a4..a1cf20c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,26 +1,42 @@ # 为什么不指定 cmake_minimum_required 会导致下面在 project 处出错? -#cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.10) -project(hellocmake VERSION 3.1.4 LANGUAGES CXX) +project(hellocmake VERSION 3.1.4 LANGUAGES CXX C) # 如何让构建类型默认为 Release? #set(CMAKE_BUILD_TYPE Release) -# 这样设置 C++14 的方式对吗?请改正 -set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++14") +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() +# 这样设置 C++14 的方式对吗?请改正 +# set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++14") +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS ON) # (可选)使用 ccache 加速编译 find_program(CCACHE_PROGRAM ccache) +if (CCACHE_PROGRAM) + message(STATUS "Found CCache: ${CCACHE_PROGRAM}") + set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM}) + set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_PROGRAM}) +else() + message("CCache not found") +endif() # legacy/CMakeLists.txt 和 mylib/CMakeLists.txt 里还有问题哦! add_subdirectory(legacy) add_subdirectory(mylib) # 这样需要一个个写出所有文件很麻烦,请改成自动批量添加源文件 -set(main_sources "src/main.cpp" "src/other.cpp" "src/dummy.cpp" "src/veryusefulfile.cpp") +# set(main_sources "src/main.cpp" "src/other.cpp" "src/dummy.cpp" "src/veryusefulfile.cpp") +file(GLOB_RECURSE main_sources CONFIGURE_DEPENDS src/*.cpp ) add_executable(main ${main_sources}) -# 请改为项目的正确版本(用变量来获取) -target_compile_definitions(main PRIVATE HELLOCMAKE_VERSION="233.33.33") +target_link_libraries(main PRIVATE mylib) +# 请改为项目的正确版本(用变量来获取) +# target_compile_definitions(main PRIVATE HELLOCMAKE_VERSION="233.33.33") +target_compile_definitions(main PRIVATE HELLOCMAKE_VERSION=${PROJECT_VERSION}) # (可选)添加 run 作为伪目标方便命令行调用 diff --git a/mylib/CMakeLists.txt b/mylib/CMakeLists.txt index f25aeef..5f31294 100644 --- a/mylib/CMakeLists.txt +++ b/mylib/CMakeLists.txt @@ -1,9 +1,21 @@ # 请改用自动批量查找所有 .cpp 和 .h 文件: -set(mylib_sources "mylib/mylib.cpp" "mylib/mylib.h") +# set(mylib_sources "mylib/mylib.cpp" "mylib/mylib.h") + +file(GLOB_RECURSE mylib_sources CONFIGURE_DEPENDS mylib/*.cpp mylib/*.h) # 使用 SHARED 在 Windows 上会遇到什么困难?请尝试修复! add_library(mylib SHARED ${mylib_sources}) +set_property(TARGET mylib PROPERTY RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}) +set_property(TARGET mylib PROPERTY LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}) + target_compile_definitions(mylib PRIVATE MYLIB_EXPORT) # 这里应该用 PRIVATE 还是 PUBLIC? -target_include_directories(mylib PRIVATE .) +target_include_directories(mylib PUBLIC .)