由于添加了/ Fo,因此无法使用Scons为ARM进行交叉编译

时间:2019-04-25 11:42:25

标签: arm cross-compiling scons

我正在使用Scons 3.0.0在装有VC ++的Windows上使用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并按照本文Using scons to compile C++ code under windows, scons adds "/Fo" as compile option的说明进行尝试后,问题仍然存在。

这是我的cons脚本的代码

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)

有人知道吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您已允许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)