Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw11 finish #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# 为什么不指定 cmake_minimum_required 会导致下面在 project 处出错?
#cmake_minimum_required(VERSION 3.10)
# CMake 3.0 引入的兼容性问题CMP0048
cmake_minimum_required(VERSION 3.10)

project(hellocmake VERSION 3.1.4 LANGUAGES CXX)
project(hellocmake VERSION 3.1.4 LANGUAGES C CXX)

# 如何让构建类型默认为 Release?
#set(CMAKE_BUILD_TYPE Release)
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)

# (可选)使用 ccache 加速编译
find_program(CCACHE_PROGRAM ccache)
Expand All @@ -17,10 +21,12 @@ add_subdirectory(legacy)
add_subdirectory(mylib)

# 这样需要一个个写出所有文件很麻烦,请改成自动批量添加源文件
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_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 作为伪目标方便命令行调用
add_custom_target(run COMMAND $<TARGET_FILE:main>)
13 changes: 11 additions & 2 deletions mylib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# 请改用自动批量查找所有 .cpp 和 .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 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_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 .)