使用GNU make,嵌入式C进行本机和交叉编译

问题描述 投票:3回答:1

我正在使用Makefile来使我能够进行本机和交叉编译。选择wether来编译主机linux或ti MSP432应该从命令行完成:

$ make build PLATFORM=MSP432
$ make build PLATFORM=HOST

这是我尝试在其中执行的Makefile:

include sources.mk

ifeq ($(PLATFORM),MSP432)
# Platform Overrides

# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs

# Compiler Flags and Defines

CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif


ifeq ($(PLATFORM),HOST)

CC = gcc

endif


TARGET = c1m1
LDFLAGS = -Wl,-Map=$(TARGET).map -T $(LINKER_FILE)
CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0  
-std=c99
CPPFLAGs =


.PHONY: build
build: $(TARGET).out


.PHONY: clean
clean:  
    rm -f $(OBJS) $(TARGET).out $(TARGET).map

%.o : %.c
$(CC) -c $< $(CFLAGS) -o $@
    OBJS = $(SOURCES:.c=.o)

$(TARGET).out: $(OBJS)
    $(CC) $(OBJS) $(CFLAGS) $(LDFLAGS) -o $@

这是正确的方法吗?

当我使用以下编译时,还会发生另一个奇怪的错误:

$ make main.o PLATFORM=MSP432

我收到此错误:

arm-none-eabi-gcc -c main.c -mcpu=cortex-m4 -mthumb -- 
specs=nosys.specs -Wall -Werror -g -O0  -std=c99 -o main.o
main.c:23:22: fatal error: platform.h: No such file or directory 
 #include "platform.h"
                  ^
compilation terminated.
Makefile:64: recipe for target 'main.o' failed
make: *** [main.o] Error 1

当我使用这个编译时:

$ make main.o PLATFORM=HOST

我得到这个错误,它们是2个不同的错误,我无法理解这背后的原因。

gcc -c main.c -mcpu= -m --specs= -Wall -Werror -g -O0  -std=c99 -o 
main.o
gcc: warning: ‘-mcpu=’ is deprecated; use ‘-mtune=’ or ‘-march=’ 
instead
gcc: error: missing argument to ‘-mcpu=’
gcc: error: missing argument to ‘--specs=’
gcc: error: unrecognized command line option ‘-m’
Makefile:64: recipe for target 'main.o' failed
make: *** [main.o] Error 1

我在一个问题中发布了那些明显不同的问题,因为我认为它们正在影响彼此。

这也是另一个名为platform.h的头文件,它有一些条件包含一些指令,在我认为可能需要编译时切换的答案之后

#ifndef __PLATFORM_H__
#define __PLATFORM_H__

#if defined (MSP432)
#include "msp432p401r.h"
#define PRINTF(...)




#elif defined (HOST)
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)



#else
#error "Platform provided is not supported in this Build System"
#endif

#endif /* __PLATFORM_H__ */
c compiler-errors embedded cross-compiling
1个回答
1
投票

首先,我将回答PLATFORMHOST相同的情况:

$ make main.o PLATFORM=HOST

我得到这个错误,它们是2个不同的错误,我无法理解这背后的原因。

gcc -c main.c -mcpu= -m --specs= -Wall -Werror -g -O0  -std=c99 -o 
main.o
gcc: warning: ‘-mcpu=’ is deprecated; use ‘-mtune=’ or ‘-march=’ 
instead
gcc: error: missing argument to ‘-mcpu=’
gcc: error: missing argument to ‘--specs=’
gcc: error: unrecognized command line option ‘-m’
Makefile:64: recipe for target 'main.o' failed
make: *** [main.o] Error 1

这是由于你的makefile:CPUARCHSPECS仅在PLATFORMMSP432时设置

因此,CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0 -std=c99线被评估为CFLAGS = -mcpu= -m --specs= -Wall -Werror -g -O0 -std=c99

当用gcc作为参数调用CFLAGS时,这是不正确的。

要更正此问题,您可以在makefile中进行少量更改:

include sources.mk

ifeq ($(PLATFORM),MSP432)
# Platform Overrides

# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs

LDFLAGS_ARCH = -T $(LINKER_FILE)
CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS)

# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif

ifeq ($(PLATFORM),HOST)

CC = gcc

endif


TARGET = c1m1
LDFLAGS = -Wl,-Map=$(TARGET).map $(LDFLAGS_ARCH)
CFLAGS = $(CFLAGS_ARCH) -Wall -Werror -g -O0  
-std=c99
CPPFLAGs =

现在,对于main.c:23:22: fatal error: platform.h: No such file or directory您必须找到此文件所在的位置,并最终将其添加为gcc选项。

例如,如果文件platform.h/some/directory中,您可以将此选项添加到gcc以帮助它找到它:

-I/some/directory

所以在makefile中,你可以拥有这一行:

CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -I/some/directory

编辑

在评论中,您为问题添加了此问题:

这解决了它现在的错误是一致的,在这里它是

 In file included from main.c:23:0: ./include/common/platform.h:30:2: error:
 #error "Platform provided is not supported in this Build System" #error "... *** [main.o] Error 1 

关于platform.h文件,必须定义宏MSP432HOST才能运行。

要定义这样的宏,必须将-D选项传递给gcc

所以我的想法是在makefile中添加一些行来在必要时定义MSP432HOST

...
ifeq ($(PLATFORM),MSP432)
# Platform Overrides

# Architectures Specific Flags
LINKER_FILE = msp432p401r.lds
CPU = cortex-m4
ARCH = thumb
SPECS = nosys.specs

LDFLAGS_ARCH = -T $(LINKER_FILE)
CFLAGS_ARCH = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -DMSP432 

# Compiler Flags and Defines
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
endif

ifeq ($(PLATFORM),HOST)

CFLAGS_ARCH = -DHOST 

CC = gcc

endif
...
© www.soinside.com 2019 - 2024. All rights reserved.