如何停止CMake附加C编译器标志 [英] How to stop CMake appending C compiler flags

查看:100
本文介绍了如何停止CMake附加C编译器标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个旧样式的makefile转换为CMake CMakeLists.txt文件,以便可以将项目加载到JetBrain的新CLion IDE中.

I converted an old style makefile to a CMake CMakeLists.txt file so that I can load a project into JetBrain's new CLion IDE.

我认为这很容易,但是我被CMake困在向编译命令添加一些自定义编译器标志的位置,这会导致编译错误.我对CMake的了解不足,无法解决此问题.

I thought it would be easy, but I'm stuck at the point of CMake appending some custom compiler flags to the compilation command which cause a compilation error. I don't have enough knowledge of CMake to solve this issue.

这是原始的makefile.

Here is the original makefile.

# makefile

# Main Filename to be compiled
MAINFILE = TestProgram

# Paths
DRIVE           := C:
COMPILERROOT    := $(DRIVE)/GNUHC11
COMPILERPATH    := $(COMPILERROOT)/bin
GELROOT         := $(DRIVE)/library/gel-hc1x-1.6.1
GELINCLUDESDIR  := $(GELROOT)/include

# Compiler, Linker, Object Copy, and Object Dump path
CC      := $(COMPILERPATH)/m6811-elf-gcc        # compiler
OC      := $(COMPILERPATH)/m6811-elf-objcopy    # object copy
OD      := $(COMPILERPATH)/m6811-elf-objdump    # object dump

# Includes
GELINCLUDES     += -I$(GELINCLUDESDIR) -I$(GELINCLUDESDIR)/asm-m68hc11/arch-32k

# Compiler Flags
CFLAGS  += -Os                  # turn on optimizer
CFLAGS  += -mshort              # consider type int to be 16 bits
CFLAGS  += -Wl,-m,m68hc11elfb   # build for elf file and use memory.x for memory map
CFLAGS  += -I. $(GELINCLUDES)   # Add current dir and gel library for includes
CFLAGS  += -Dmc6811             # Add define to define the processor architecture for gel includes

# C Source codes to be compiled
SRC1 = $(MAINFILE).c
SRC2 = Interrupts.c
SRC3 = Utilities.c

# C Header files dependencies
HDR1 = $(MAINFILE).h
HDR2 = Interrupts.h
HDR3 = Utilities.h

SRCS = $(SRC1) $(SRC2) $(SRC3)

HDRS = $(HDR1) $(HDR2) $(HDR3)

# Elf file to be generated
ELF1 = $(SRC1:.c=.elf)

# Generate Bin file for programming & Assembly dump
$(MAINFILE).bin : $(ELF1)
    $(OC) -O binary $(ELF1) $(MAINFILE).bin
    $(OD) -xDC --section=.text --section=.vectors $(ELF1) >$(MAINFILE).dump

# Full compile and link
$(ELF1) : $(SRCS) $(HDRS)
    $(CC) $(CFLAGS) -o $(ELF1) $(SRCS)

clean ::
    del *.dump
    del *.elf
    del *.bin

这是我对CMakeLists.txt文件的尝试.

And here is my attempt at the CMakeLists.txt file.

cmake_minimum_required(VERSION 2.8.4)

# program names
set(HC11C    m6811-elf-gcc.exe)
set(OBJCOPY  m6811-elf-objcopy.exe)
set(OBJDUMP  m6811-elf-objdump.exe)

# Important project paths
set(LIB_INC_PATH "C:/library/gel-hc1x-1.6.1/include"
                    "C:/library/gel-hc1x-1.6.1/include/asm-m68hc11/arch-32k")
set(HC11C_PATH   "C:/GNUHC11/bin")

# Sets the compiler
# Needs to come before the project function
set(CMAKE_SYSTEM_NAME  Generic)
set(CMAKE_C_COMPILER   "${HC11C_PATH}/${HC11C}")

set(MAIN_FILE "TestProgram")                
project(${MAIN_FILE})

# Files to be compiled
set(BASE_PATH    "${${PROJECT_NAME}_SOURCE_DIR}")
set(INC_PATH     "${BASE_PATH}")
set(SRC_PATH     "${BASE_PATH}")

set(SRC_FILES   "${SRC_PATH}/${MAIN_FILE}.c"
                "${SRC_PATH}/Interrupts.c"
                "${SRC_PATH}/Utilities.c")

# Attempt to clear the other spurious compiler flags that I don't want, 
# and which cause a compiler arguments error.
# This doesn't seem to work - the defaults still appear.
set(CMAKE_C_FLAGS_DEBUG             "")
set(CMAKE_C_FLAGS_RELEASE           "")
set(CMAKE_C_FLAGS_RELWITHDEBINFO    "")
set(CMAKE_C_FLAGS_MINSIZEREL        "")

# Compiler flags
set(CWARN     "-Wl,-m,m68hc11elfb")         # build for elf file and use memory.x for memory map
set(CTUNING   "-mshort")                    # consider type int to be 16 bits
set(COPT      "-Os")                        # turn on optimizer
set(CDEFS     "-Dmc6811")                   # Add define to define the processor architecture for gel includes
set(CFILES    "${MAIN_FILE}.c Interrupts.c Utilities.c")

set(CFLAGS   "${CDEFS} ${COPT} ${CWARN} ${CTUNING} ${CFILES}")

set(CMAKE_C_FLAGS   "${CFLAGS}")

# Project setup
include_directories(${INC_PATH} ${LIB_INC_PATH})
add_executable(${MAIN_FILE} ${SRC_FILES})
set_target_properties(${MAIN_FILE} PROPERTIES OUTPUT_NAME "${MAIN_FILE}.elf")

# Compiling targets
add_custom_target(main ALL ${OBJCOPY} -O binary "${MAIN_FILE}.elf" "${MAIN_FILE}.bin"  DEPENDS ${MAIN_FILE})
add_custom_target(dump ALL ${OBJDUMP} -xDC --section=.text --section=.vectors "${MAIN_FILE}.elf" > "${MAIN_FILE}.dump" DEPENDS main)

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${MAIN_FILE}.dump;${MAIN_FILE}.elf;${MAIN_FILE}.bin")

# Config logging
message("* ")
message("* Project Name:\t${PROJECT_NAME}")
message("* Project Source:\t${SRC_PATH}")
message("* Project Include:\t${INC_PATH}")
message("* Library Include:\t${LIB_INC_PATH}")
message("* ")
message("* Project Source Files:\t${SRC_FILES}")
message("* MAIN_FILE variable:\t${MAIN_FILE}")
message("* ")
message("* C Flags:\t${CMAKE_C_FLAGS}")
message("* ")

这是生成的编译命令:

C:\GNUHC11\bin\m6811-elf-gcc.exe "-xc" "-Dmc6811" "-Os" "-Wl,-m,m68hc11elfb" "-mshort" "TestProgram.c" "Interrupts.c" "Utilities.c" "-IC:\\DEVELO~1\\source" "-IC:\\library\\gel-hc1x-1.6.1\\include" "-IC:\\library\\gel-hc1x-1.6.1\\include\\asm-m68hc11\\arch-32k" "-v" "-dD" "-E" "-D___CIDR_IGNORE_DEFINITIONS_START"

它可以工作,但是对于不需要的结尾自动附加的"-E"编译器标志.其他附加标志"-v",-dD"和"-D ___ CIDR ..."也是不需要的,但不会像"-E"那样引起编译错误.如何关闭这些附加标志?

It would work but for the auto appended "-E" compiler flag at the end which I don't want. The other appended flags "-v" "-dD" and '-D___CIDR..." are also unwanted but do not cause a compilation error like "-E" does. How can I turn these appended flags off?

在此先感谢您的帮助.

推荐答案

似乎您正在交叉编译,因此首选的cmake配置与正常配置有所不同.

It seems you are cross-compiling, so the preferred cmake configuration is a bit different than normal.

有关详情,请参见 http://www.vtk.org/Wiki/CMake_Cross_Compiling 注意工具链文件".

See http://www.vtk.org/Wiki/CMake_Cross_Compiling for details, pay attention to the "toolchain file".

我对自动附加标志一无所知.

I have no idea regarding the auto-appended flags.

您还可以使用CLion本身的任何编辑器或cmake-gui来查看生成的CMakeCache.txt文件.

You can also have a look at the generated CMakeCache.txt file, either with any editor, from CLion itself, or with cmake-gui.

请记住,如果您想手动查看CMakeCache.txt,请记住,CLion将您的CMakeLists.txt复制到一个时髦的临时目录中,并在临时目录下运行cmake.

Remember that CLion copies your CMakeLists.txt in a funky temporary directory and runs cmake off the temporary directory, if you want to look at CMakeCache.txt by hand.

我建议在调试CMakeLists.txt时将CLion放在一旁,而直接从外壳中直接使用cmake或cmake-gui.

What I suggest is to put aside CLion while you are debugging the CMakeLists.txt, and just use cmake or cmake-gui directly from the shell.

别放弃,cmake和CLion都是两个非常不错的程序,恕我直言:-)

Don't give up, both cmake and CLion are two very good programs IMHO :-)

这篇关于如何停止CMake附加C编译器标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆