我有一个使用arm-none-eabi-gcc工具链在C语言中为ARM设备编写和开发的项目。它使用一个自定义的makefile,它只适用于C编译。我们正在尝试转换到主要的C ++项目,同时保留C源文件,但我在更新makefile时遇到问题。我知道这已被问过几次,但没有一个能够彻底解决我们的问题。如何使用g ++和cpp文件包含C源文件和头文件?
我非常感谢愿意并且能够查看我们的makefile以帮助解决编译问题的任何人。 makefile很长,所以这里有一个有点删节的版本,应该包含一切关键的内容。
请记住,我这里只包含了makefile,而不是platform_id.mk或$(PLATFORM_MCU).mk,但我对这些文件非常有信心。
BIN_NAME = $(PLATFORM)_firmware.bin
ifdef PLATFORM
include $(PROJECT_ROOT)build/platform_id.mk
include $(PROJECT_ROOT)hal/$(PLATFORM_MCU)/$(PLATFORM_MCU).mk
include $(PROJECT_ROOT)build/$(PLATFORM_MCU_CORE)/$(PLATFORM_MCU_CORE).mk
else
$(error "Platform not defined. Used the -e flag to define the platform in the environment
endif
$(info $(msg))
# C compiler flags
CFLAGS +=\
-std=c99\
-Og\
-g3\
-Wall\
-fdata-sections\
-ffunction-sections\
-MMD\
-DHAVE_MMAP=0\
-Dmalloc_getpagesize=8\
-DMORECORE_CLEARS=0\
-DDEFAULT_TRIM_THRESHOLD=8
# C++ compiler flags
CXXFLAGS +=\
-Wall\
-D__STDC_LIMIT_MACROS
# C Pre Processor flags
CPPFLAGS +=\
# Undefine WIN32 variable if building on Windows host
ifdef WIN32
CFLAGS += -UWIN32
msg = You're on a windows host.
else
msg = You're not on a windows host.
endif
ifdef TOOLCHAIN
AR = $(TOOLCHAIN)-ar
CC = $(TOOLCHAIN)-gcc
CXX = $(TOOLCHAIN)-g++
endif
# Binary make targets
TARGET = out/$(PLATFORM_MCU)/$(BIN_NAME)
# Binary Sources
VPATH += $(wildcard */src/)
# extract all *.c filenames, don't pull the filepath with it
C_SRCS += $(notdir $(wildcard */src/*.c))
# extract all *.cpp filesnames, don't pull the filepath with it
CXX_SRCS += $(notdir $(wildcard */src/*.cpp))
# extract all *.s filenames, don't pull the filepath with it
S_SRCS += $(notdir $(wildcard */src/*.s))
# Objects
OBJECTS_C += $(patsubst %.c,out/$(PLATFORM_MCU)/obj/%.o,$(C_SRCS))
OBJECTS_CXX += $(patsubst %.cpp,out/$(PLATFORM_MCU)/obj/%.o,$(CXX_SRCS))
# Dependencies
DEP_C := $(OBJECTS_C:.o=.d)
DEP_CXX := $(OBJECTS_CXX:.o=.d)
# Includes
NULL =
SPACE = $(NULL) $(NULL)
INCLUDE_SRC = $(subst $(SPACE),$(SPACE)-I,$(VPATH))
PATH_INC = $(wildcard */inc/)
INCLUDE_INC = $(subst $(SPACE),$(SPACE)-I,$(PATH_INC))
C_INC +=\
-I$(INCLUDE_INC) \
-I$(INCLUDE_SRC)
# Clean and precious
PRECIOUS += out/$(PLATFORM_MCU)/obj/%.o
CLEAN += out/$(PLATFORM_MCU)/
# all
all: $(TARGET)
# Create binary
$(TARGET): $(OBJECTS_C) $(OBJECTS_CXX) $(S_SRCS)
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(C_INC) -T$(LDFILE) $(LFLAGS) -o $@
$(TOOLCHAIN)-objcopy -O binary $@ $(basename $@).bin
# Compile from cpp files. Compilation works when this is removed (and toolchain and files are tweaked to represent that)
out/$(PLATFORM_MCU)/obj/%.o: %.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(C_INC) -o $@ -c $<
-include $(DEP_CXX)
# Create binary from c files
out/$(PLATFORM_MCU)/obj/%.o: %.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(C_INC) -o $@ -c $<
-include $(DEP_C)
.PRECIOUS: $(PRECIOUS)
.PHONY: all clean
clean:
rm -rf $(CLEAN)
运行make -e PLATFORM=test
之后,错误很多,并且主要说明未定义c源代码中的各种功能。现在main.cpp并不比int main()
多得多,并且包含在包含大多数其他C头文件的头文件中。
以下是其中一个编译器错误的示例:
hal/atsamg55/sam/drivers/usart/usart.h:333:47: error: 'p_usart' was not declared in this scope
uint32_t usart_get_writeprotect_status(Usart *p_usart);
这是main.cpp
#include "headers.h"
int main(){
while(1)
{
}
return 0;
}
再次感谢您提供的任何帮助。
答案 0 :(得分:0)
感谢您的帮助,我想我能够解决这个问题,并希望将其发布在此处以防将来帮助任何人。关于所有C文件的错误来自我没有正确地将定义从CFLAGS转移到CXX标志的错误。一旦我添加了-DBOARD = TEST相关的标志,我的头文件就能识别我正在使用的板,它允许它通过“包含”链。