cmake使用/clr

  • Post author:
  • Post category:build
  • Post comments:0评论

cmake use /clr

There are three options

(1) Collect the affected source files in dedicated directories with a
CMakeLists.txt and remove "-g" from CMAKE_{C,CXX}_FLAGS_DEBUG therein.
However, the sources must go into targets defined in the directory's
CMakeLists.txt; maybe, you need to add particular static libraries
built from these sources, provided your toolchain supports this.

(2) Externalize the affected sources and reintegrate them as an
external project built with the same toolchain but different flags,
i.e. without "-g"; a radical approach with the same drawbacks as (1).

(3) If you get along with Makefile generators, you might use one of
the RULE_LAUNCH_COMPILE properties combined with a shell script:

cmake创建使用到/clr的项目

要删除debug版的/RTC1,有3种方案

  • 1.给单个文件增加/clr并且删除CMAKE_CXX_FLAGS_DEBUG/RTC1标志
# test.cpp增加/clr /EHa属性
set_property(SOURCE test.cpp APPEND PROPERTY COMPILE_FLAGS "/clr /EHa")

# 删除/RTC1标记
if(CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1")
   string(REPLACE "/RTC1" " " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
   message("CMAKE_CXX_FLAGS_DEBUG:${CMAKE_CXX_FLAGS_DEBUG}")
endif()
  • 2.把用到/clr的源文件构建成dll。在dll中删除CMAKE_CXX_FLAGS_DEBUG/RTC1标志

  • 3.使用RULE_LAUNCH_COMPILE与shell脚本结合:

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(EXAMPLE C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/g.c "void g(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/h.c "void h(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c f.c g.c h.c)
SET_TARGET_PROPERTIES(main PROPERTIES RULE_LAUNCH_COMPILE
    "bash ${CMAKE_SOURCE_DIR}/setup <SOURCE> ${CMAKE_BINARY_DIR}/f.c
${CMAKE_BINARY_DIR}/h.c --")

setup

SOURCE="$1"; shift
echo "SOURCE: $SOURCE"
unset FILES
while [ "$1" != "--" ]; do
    FILES[${#FILES[@]}]="$1"; shift
done
shift
echo "FILES[${#FILES[@]}]: ${FILES[@]}"
CMDLINE0=("$1"); CMDLINE1=("$1"); shift
for i in "$@"; do
    CMDLINE0[${#CMDLINE0[@]}]="$1"
    if [ "$1" != "-g" ]; then CMDLINE1[${#CMDLINE1[@]}]="$1"; fi
    shift
done
echo "CMDLINE0: ${CMDLINE0[@]}"
echo "CMDLINE1: ${CMDLINE1[@]}"
for i in "${FILES[@]}"; do
    if [ "$i" == "$SOURCE" ]; then
        echo "Executing ${CMDLINE1[@]}"; exec "${CMDLINE1[@]}"
    fi
done
echo "Executing ${CMDLINE0[@]}"; exec "${CMDLINE0[@]}"

The setup script takes the source to compile, a list of source files to
be compiled without "-g" and the actual command line separated by "--".
It stores the source and the files and constructs two command lines,
one without "-g". Finally, it compares the source with each of the
files, and if there's a match, it executes the shortened command
line without "-g", otherwise the unaltered one.

IMO, (3) is a quite smart approach and should be favored if the project
doesn't need to be built with generators other than the Makefile ones.

/RTC(运行时错误检查)

文章作者: 张拓
文章链接: http://www.xssl.online/cmake%e4%bd%bf%e7%94%a8clr/
版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明来自 张拓的博客
浏览次数: 630

张拓

陕西西安蓝田张拓QQ1070410059。一生所求不过“心安”二字。 然,尘世多纷扰。

发表回复