CMake在vs中创建文件夹组织目标工程


a

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC -g) # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options( mylib PUBLIC -DUSEXX) # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX) # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX) # Bad
add_definitions(-DUSEXX) # OK

使用cmake生成表达式

In you case you can use:

1
2
3
target_compile_options(${TARGET} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)

or about compilers:

1
2
3
4
target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
$<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
$<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)

相关参考

Does set_target_properties in CMake override CMAKE_CXX_FLAGS?

It is impossible to reliably use target_compile_options() on MSVC.

Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake

cmake 编译器id

Does set_target_properties in CMake override CMAKE_CXX_FLAGS?

Compile with /MT instead of /MD using CMake

Modern way to set compiler flags in cross-platform cmake project

What is the modern method for setting general compile flags in CMake?

Difference between add_compile_options and SET(CMAKE_CXX_FLAGS…)

Blog

CMake – Properties and Options

CMake 基本用法介绍

Build system internals

cmake 论坛

target_compile_options de-duplicates flags but some repeats matter

cmake运行时库的方法

Changing CMAKE_CXX_FLAGS in project

CMake - remove a compile flag for a single translation unit

Does set_target_properties in CMake override CMAKE_CXX_FLAGS?

Not possible to set "/MT" or "/MTd" for Visual Studio 15 2017 generator

Compile with /MT instead of /MD using CMake

csdn

cmake:msvc分别对不同的target使用不同的运行库选项(/MT或/MD)

cmake:设置编译选项的讲究(add_compile_options和CMAKE_CXX_FLAGS的区别)