Compling Cpp files with C file

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Compling Cpp files with C file

410 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by amlwwalker on Sun Dec 01 07:35:54 MST 2013
Hi,
Everything works nicely at the moment with everything as a .c and a .h file.

I want to write some classes to handle some of my code. For instance uart control. I have been learning about editing makefiles, and I think I have edited my makefile reasonably correctly, see below:

#
#       !!!! Do NOT edit this makefile with an editor which replace tabs by spaces !!!!    
#
##############################################################################################
# 
# On command line:
#
# make all = Create project
#
# make clean = Clean project files.
#
# To rebuild project do "make clean" and "make all".
#

##############################################################################################
# Start of default section
#

TRGT = arm-none-eabi-
CC   = $(TRGT)gcc
CPP   = $(TRGT)g++
CP   = $(TRGT)objcopy
AS   = $(TRGT)gcc -x assembler-with-cpp
HEX  = $(CP) -O ihex
BIN  = $(CP) -O binary

MCU  = cortex-m3

# List all default C defines here, like -D_DEBUG=1
DDEFS =

# List all default ASM defines here, like -D_DEBUG=1
DADEFS = 

# List all default directories to look for include files here
DINCDIR = 

# List the default directory to look for the libraries here
DLIBDIR =

# List all default libraries here
DLIBS = 

#
# End of default section
##############################################################################################

##############################################################################################
# Start of user section
#

# 
# Define project name and Ram/Flash mode here
PROJECT        = Keyboard
RUN_FROM_FLASH = 1

#
# Define linker script file here
#
ifeq ($(RUN_FROM_FLASH), 0)
LDSCRIPT = ./prj/lpc1788_ram.ld
FULL_PRJ = $(PROJECT)_ram
else
LDSCRIPT = ./prj/lpc1788_flash.ld
FULL_PRJ = $(PROJECT)
endif

# List all user C define here, like -D_DEBUG=1
UDEFS = 

# Define ASM defines here
UADEFS = 

# List C source files here
SRC  = ./cmsis/core/core_cm3.c \
       ./cmsis/device/NXP/LPC177x_8x/system_LPC177x_8x.c \
       $(wildcard ./src/*.c) \
       $(wildcard ./src/*.cpp) \
       $(wildcard ./drivers/source/*.c) 
       # ./src/crt.c \
       #./src/SDRAM.c \
       #./src/GLCD.c \
       #./src/stubs.c \
       #./src/util.c \
       #./src/vectors_lpc1768.c \
       #./src/main.c \
       #./src/peripherals.c \
       
       

# List ASM source files here
Asrc=# List all user directories here
UINCDIR = ./inc \
          ./hardware\
          ./drivers/include \
          ./cmsis/core \
          ./cmsis/device/NXP/LPC177x_8x \

# List the user directory to look for the libraries here
ULIBDIR =

# List all user libraries here
ULIBS = 

# Define optimisation level here
OPT = -O0

#
# End of user defines
##############################################################################################


INCDIR  = $(patsubst %,-I%,$(DINCDIR) $(UINCDIR))
LIBDIR  = $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))

ifeq ($(RUN_FROM_FLASH), 0)
DEFS    = $(DDEFS) $(UDEFS) -DRUN_FROM_FLASH=0 -D__RAM_MODE__
else
DEFS    = $(DDEFS) $(UDEFS) -DRUN_FROM_FLASH=1
endif

ADEFS   = $(DADEFS) $(UADEFS)
OBJS    = $(ASRC:.s=.o) $(SRC:.c=.o) $(SRC:.cpp=.o)
LIBS    = $(DLIBS) $(ULIBS)
MCFLAGS = -mcpu=$(MCU)

ASFLAGS = $(MCFLAGS) -g -gdwarf-2 -Wa,-amhls=$(<:.s=.lst) $(ADEFS)
CPFLAGS = $(MCFLAGS) $(OPT) -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=$(<:.c=.lst) $(DEFS)
CPPFLAGS = $(MCFLAGS) $(OPT) -gdwarf-2 -mthumb -fomit-frame-pointer -Wall -Wstrict-prototypes -fverbose-asm -Wa,-ahlms=$(<:.cpp=.lst) $(DEFS)
LDFLAGS = $(MCFLAGS) -mthumb -nostartfiles -T$(LDSCRIPT) -Wl,-Map=$(FULL_PRJ).map,--cref,--no-warn-mismatch $(LIBDIR)

# Generate dependency information
CPFLAGS += -MD -MP -MF .dep/$(@F).d
CPPFLAGS += -MD -MP -MF .dep/$(@F).d
#
# makefile rules
#

ifeq ($(RUN_FROM_FLASH), 0)
all: $(OBJS) $(FULL_PRJ).elf $(FULL_PRJ).hex
 $(MAKE) post-build
else
all: $(OBJS) $(FULL_PRJ).elf $(FULL_PRJ).hex $(FULL_PRJ).bin
 $(MAKE) post-build
endif


%.o : %.c
$(CC) -c $(CPFLAGS) -I . $(INCDIR) $< -o $@

%.o : %.cpp
$(CPP) -c $(CPPFLAGS) -I . $(INCDIR) $< -o $@

%.o : %.s
$(AS) -c $(ASFLAGS) $< -o $@

%elf: $(OBJS)
$(CC) $(CPP) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  
%hex: %elf
$(HEX) $< $@

%bin: %elf
$(BIN) $< $@

clean:
-rm -f $(OBJS)
-rm -f $(FULL_PRJ).elf
-rm -f $(FULL_PRJ).map
-rm -f $(FULL_PRJ).hex
-rm -f $(FULL_PRJ).bin
-rm -f $(SRC:.c=.c.bak)
-rm -f $(SRC:.c=.lst)
-rm -f $(ASRC:.s=.s.bak)
-rm -f $(ASRC:.s=.lst)
-rm -fR .dep

post-build:
-@echo 'ARM Size statistics follow.'
-arm-none-eabi-size $(FULL_PRJ).elf
-@echo ' '

# 
# Include the dependency files, should be the last of the makefile
#
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

# *** EOF ***



The only place I haven't successfully put the cpp control is in the "clean". As it was causing some problems. Beside, I haven't got it compiling yet.

When I build I get the error:


Quote:

arm-none-eabi-gcc: error: arm-none-eabi-g++: No such file or directory
make: *** [Keyboard.elf] Error 1



It seems interesting to me that this is a arm-none-eabi-gcc issue which suggests I actaully havent set up for C++ files correctly.

Once I have done that I will need to make main.c a main.cpp file.
Labels (1)
0 Kudos
1 Reply

324 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Mon Dec 02 03:03:05 MST 2013
There is (at least one) bug in your makefile:

%elf: $(OBJS)
$(CC) $(CPP) $(OBJS) $(LDFLAGS) $(LIBS) -o $@


This is running the C compiler with the C++ compiler as the first parameter...
0 Kudos