gpt4 book ai didi

makefile - 如何停止 CMake 附加 C 编译器标志

转载 作者:行者123 更新时间:2023-12-04 18:05:15 26 4
gpt4 key购买 nike

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

我认为这很容易,但我一直停留在 CMake 将一些自定义编译器标志附加到编译命令的点上,这会导致编译错误。我没有足够的 CMake 知识来解决这个问题。

这是原始的 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 文件的尝试。

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”那样导致编译错误。如何关闭这些附加标志?

在此先感谢您的帮助。

最佳答案

看来你是在交叉编译,所以首选的 cmake 配置与正常配置有点不同。

参见 http://www.vtk.org/Wiki/CMake_Cross_Compiling详情请关注“工具链文件”。

我不知道自动附加标志。

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

请记住,如果您想手动查看 CMakeCache.txt,CLion 会将您的 CMakeLists.txt 复制到一个时髦的临时目录中,然后从该临时目录运行 cmake。

我的建议是在调试 CMakeLists.txt 时将 CLion 放在一边,直接从 shell 中使用 cmake 或 cmake-gui。

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

关于makefile - 如何停止 CMake 附加 C 编译器标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28228336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com