gpt4 book ai didi

c - 使用 GNU make、嵌入式 C 进行 native 和交叉编译

转载 作者:行者123 更新时间:2023-12-02 08:19:47 26 4
gpt4 key购买 nike

我正在编写一个 Makefile,以使我能够进行 native 编译和交叉编译。应从命令行选择是针对主机 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

我收到此错误,它们是两个不同的错误,我无法理解其背后的原因。

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__ */

最佳答案

首先回答一下PLATFORMHOST相同的情况:

$ 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:CPUARCHSPECS 仅在以下情况下设置:平台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:没有这样的文件或目录您必须找到该文件的位置并最终将其添加为 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文件,必须定义宏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
...

关于c - 使用 GNU make、嵌入式 C 进行 native 和交叉编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52853496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com