无法使用 Scons 为 ARM 交叉编译,因为添加了 /Fo

Unable to cross compile for ARM with Scons because /Fo is added

我在安装了 VC++ 的 Windows 上使用 Scons 3.0.0 与 arm-none-eabi-gcc 交叉编译 ARM。 然后 scons 添加 /Fo 作为编译选项,如下所示:

arm-none-eabi-gcc /Fobuild\main.o /c source\main.c ...

命令行内容如下:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
arm-none-eabi-gcc /Fobuild\main.o /c source\main.c -DBOARD_PCA10040 -DBSP_UART_SUPPORT -DCONFIG_GPIO_AS_PINRESET -DFLOAT_ABI_HARD -DNRF52 -DNRF52832_XXAA -DNRF52_PAN_74 -DSWI_DISABLE0 -mcpu=cortex-m4 -mthumb -mabi=aapcs -Wall -Werror -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O3 -g3
arm-none-eabi-gcc: error: /Fobuild\main.o: No such file or directory
arm-none-eabi-gcc: error: /c: No such file or directory
scons: *** [build\main.o] Error 1
scons: building terminated because of errors.

阅读用户指南后https://scons.org/doc/production/HTML/scons-user.html and tried as described in this post 问题仍然存在。

这是我的 scons 脚本的代码

import os

cross_compiler = "arm-none-eabi-"

#don't use the default environment
env = Environment(ENV = os.environ)
# env = Environment(**env_options)

env.Replace(AR = cross_compiler + 'ar')
env.Replace(AS = cross_compiler + 'as')
env.Replace(CC    = cross_compiler + "gcc")
env.Replace(CXX   = cross_compiler + "g++")
env.Replace(LD    = cross_compiler + "g++")
env.Replace(LINK  = cross_compiler + 'gcc')
env.Replace(STRIP = cross_compiler + "strip")
env.Replace(RANLIB = cross_compiler + 'ranlib')
env.Replace(OBJCOPY = cross_compiler + 'objcopy')

env.Replace(PROGSUFFIX = ".out")
env.Replace(OBJSUFFIX  = ".o")

env.Replace(TARGET_ARCH = "arm")

env.Replace(LINKFLAGS = [])

env.Replace(TOOLS = [])
# env.Replace(TOOLS = [cross_compiler + 'gcc'])
env.Append(ENV = {'PATH' : os.environ['PATH']})

# Optimization flags
opt_flags = ['-O3', '-g3']
# Uncomment the line below to enable link time optimization
#opt_flags.append('-flto')

# C flags common to all targets
c_flags = [
            '-DBOARD_PCA10040',
            '-DBSP_UART_SUPPORT',
            '-DCONFIG_GPIO_AS_PINRESET',
            '-DFLOAT_ABI_HARD',
            '-DNRF52',
            '-DNRF52832_XXAA',
            '-DNRF52_PAN_74',
            '-DSWI_DISABLE0',
            '-mcpu=cortex-m4',
            '-mthumb',
            '-mabi=aapcs',
            '-Wall',
            '-Werror',
            '-mfloat-abi=hard',
            '-mfpu=fpv4-sp-d16'
           ]

c_flags += opt_flags

env.Replace(CCFLAGS = c_flags)

libs_paths = []

objs = []

libs = []

src_dir = 'source'
build_dir = 'build'

#Set specifics c files
c_files_list=[
                'main'
                ]

obj_extension='.o'


with open('env_dump.txt', 'w') as file:
    env_dumped = env.Dump()
    for key in env_dumped:
        file.write(key)

#Add specifics c and objs
for elt in c_files_list:
   object = build_dir + os.sep + elt + obj_extension
   env.Object(target = object, source = src_dir + os.sep + elt +'.c')
   objs.append(object)

env.Program('Test', objs, LIBS = libs, LIBPATH = libs_paths)

有人有想法吗?

感谢您的帮助

您已允许 SCons 将其默认工具加载到 Environment() 中。

这将首先尝试 MSVC.. 因此是您不需要的标志。

见下文。我只是明确加载我认为您需要的工具。

还稍微简化了代码并强制 DefaultEnvironment 不加载任何工具。 这应该会略微加快启动时间,因为它不会尝试查找和配置可能有点慢的 MSVC。

试试这个:

import os
cross_compiler = "arm-none-eabi-"

#don't use the default environment
DefaultEnvironment(tools=[])

# Only specify the tools we're using
env = Environment(ENV = os.environ, tools=['gcc','g++','gnulink','ar'])

# overwrite tool names with our cross compiler tools
for (tool, name) in [
    ('AR','ar'),
    ('AS','as'),
    ('CC','gcc'),
    ('CXX','g++'),
    ('LD','g++'),
    ('LINK','gcc'),
    ('STRIP','strip'),
    ('RANLIB','ranlib'),
    ('OBJCOPY','objcopy')
]:
    env[tool] = cross_compiler + name

env['PROGSUFFIX']  = ".out"
env['OBJSUFFIX']   = ".o"

env['TARGET_ARCH'] = "arm"

env['LINKFLAGS'] = []

env.Append(ENV = {'PATH' : os.environ['PATH']})

# Optimization flags
opt_flags = ['-O3', '-g3']
# Uncomment the line below to enable link time optimization
#opt_flags.append('-flto')

# C flags common to all targets
c_flags = [
            '-DBOARD_PCA10040',
            '-DBSP_UART_SUPPORT',
            '-DCONFIG_GPIO_AS_PINRESET',
            '-DFLOAT_ABI_HARD',
            '-DNRF52',
            '-DNRF52832_XXAA',
            '-DNRF52_PAN_74',
            '-DSWI_DISABLE0',
            '-mcpu=cortex-m4',
            '-mthumb',
            '-mabi=aapcs',
            '-Wall',
            '-Werror',
            '-mfloat-abi=hard',
            '-mfpu=fpv4-sp-d16'
           ]

c_flags += opt_flags

env['CCFLAGS'] = c_flags

libs_paths = []

objs = []

libs = []

src_dir = 'source'
build_dir = 'build'

#Set specifics c files
c_files_list=[
                'main'
                ]

obj_extension='.o'


with open('env_dump.txt', 'w') as file:
    env_dumped = env.Dump()
    for key in env_dumped:
        file.write(key)

#Add specifics c and objs
for elt in c_files_list:
   object = build_dir + os.sep + elt + obj_extension
   env.Object(target = object, source = src_dir + os.sep + elt +'.c')
   objs.append(object)

env.Program('Test', objs, LIBS = libs, LIBPATH = libs_paths)