Reputation: 69
I have a project that was written and developed in C for an ARM device using the arm-none-eabi-gcc toolchain. It uses a custom makefile that was working for C only compiling. We are attempting to transition to a primarily C++ project while keeping the C source files but I'm having issues with updating the makefile. I know this has been asked a couple times, but none of them were able to completely resolve our issue. How do I include C source and header files for compilation with g++ and cpp files?
I really appreciate anyone who is willing and able to review our makefile to help resolve the compile issues. The makefile is long so here's a somewhat abridged version that should include everything critical.
Keep in mind, I have only included the makefile here, not platform_id.mk or $(PLATFORM_MCU).mk but I'm pretty confident in those files though.
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)
Afer running make -e PLATFORM=test
the errors are numerous and primarily stating that the various functions in the c source code are not defined. Right now main.cpp isn't much more than int main()
and and include to a header file that includes most of the other C header files.
Here's an example of one of the compiler errors:
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);
Here is main.cpp
#include "headers.h"
int main(){
while(1)
{
}
return 0;
}
Again, thank you for any help you can provide.
Upvotes: 4
Views: 1465
Reputation: 69
Thank you for the help, I think I was able to resolve the issue and want to post it here in case it helps anyone in the future. The errors in regards to all the C files came from my mistake in not properly transferring the defines from the CFLAGS to the CXX flags. Once I added the -DBOARD=TEST related flags, my header files were able to recognize the board I was using which allowed it to carry through the chain of "includes".
Upvotes: 1