使用 GNU make、嵌入式 C 进行本机和交叉编译
Native and cross compile using GNU make, embedded C
我正在编写一个 Makefile 以使我能够进行本机和交叉编译。选择是否为主机 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
我在 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__ */
首先回答PLATFORM
和HOST
相同的情况:
$ make main.o PLATFORM=HOST
I get this error, they are 2 different errors and I can't understand the reason behind this.
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:CPU
、ARCH
和 SPECS
仅在以下情况下设置
PLATFORM
是 MSP432
所以行 CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0 -std=c99
被评估为 CFLAGS = -mcpu= -m --specs= -Wall -Werror -g -O0 -std=c99
当以 CFLAGS
作为参数调用 gcc
时,这是不正确的。
要更正此问题,您可以在 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
编辑
在评论中,您为您的问题添加了这个问题:
that solved it the errors are consistent now, and here it is
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
文件,必须定义宏MSP432
或HOST
才能运行。
要定义这样的宏,必须将 -D
选项传递给 gcc
。
所以想法是在 makefile 中添加一些行以在必要时定义 MSP432
或 HOST
:
...
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
...
我正在编写一个 Makefile 以使我能够进行本机和交叉编译。选择是否为主机 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
我在 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__ */
首先回答PLATFORM
和HOST
相同的情况:
$ make main.o PLATFORM=HOST
I get this error, they are 2 different errors and I can't understand the reason behind this.
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:CPU
、ARCH
和 SPECS
仅在以下情况下设置
PLATFORM
是 MSP432
所以行 CFLAGS = -mcpu=$(CPU) -m$(ARCH) --specs=$(SPECS) -Wall -Werror -g -O0 -std=c99
被评估为 CFLAGS = -mcpu= -m --specs= -Wall -Werror -g -O0 -std=c99
当以 CFLAGS
作为参数调用 gcc
时,这是不正确的。
要更正此问题,您可以在 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
编辑
在评论中,您为您的问题添加了这个问题:
that solved it the errors are consistent now, and here it is
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
文件,必须定义宏MSP432
或HOST
才能运行。
要定义这样的宏,必须将 -D
选项传递给 gcc
。
所以想法是在 makefile 中添加一些行以在必要时定义 MSP432
或 HOST
:
...
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
...