链接器无法使用scons从不同的目录中找到目标文件

时间:2016-01-21 16:20:44

标签: compilation linker ld scons

目前,我用scons制作了一个项目。 我编译了源代码,是时候链接它们了。 但是,我收到一个错误,ld无法找到目标文件。

SConscript位于src / kernel32和

import os, sys

# Compile CPP
env_gpp_options = {
    'CXX'         : 'x86_64-pc-linux-g++',
    'CXXFLAGS'    : '-std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti',
    'LINK'        : 'x86_64-pc-linux-ld',
    'LINKFLAGS'   : '-melf_i386 -T scripts/elf_i386.x -nostdlib -e main -Ttext 0x10200',
}
env_gpp = Environment(**env_gpp_options)
env_gpp.Append(ENV = {'PATH' : os.environ['PATH']})

object_cpp_list = Glob('*.cpp')

for object_cpp in object_cpp_list:
    env_gpp.Object(object_cpp)

# Find all object file
object_target_list = Glob('*.o')

# Linking
env_link_target = 'kernel32.elf'

env_gpp.Program(env_link_target, object_target_list)

我收到的消息是

x86_64-pc-linux-g++ -o build/kernel32/cpu.o -c -std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti src/kernel32/cpu.cpp
x86_64-pc-linux-g++ -o build/kernel32/main.o -c -std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti src/kernel32/main.cpp
x86_64-pc-linux-g++ -o build/kernel32/memory.o -c -std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti src/kernel32/memory.cpp
x86_64-pc-linux-g++ -o build/kernel32/pageManager.o -c -std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti src/kernel32/pageManager.cpp
x86_64-pc-linux-g++ -o build/kernel32/utils.o -c -std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti src/kernel32/utils.cpp
x86_64-pc-linux-ld -o build/kernel32/kernel32.elf -melf_i386 -T scripts/elf_i386.x -nostdlib -e main -Ttext 0x10200 build/kernel32/asmUtils.o build/kernel32/cpu.o build/kernel32/main.o build/kernel32/memory.o build/kernel32/pageManager.o build/kernel32/utils.o
x86_64-pc-linux-ld: cannot find main.o
scons: *** [build/kernel32/kernel32.elf] Error 1
scons: building terminated because of errors.

我检查了目录,build / kernel32 /,然后找到了main.o文件。 我的错是什么? 有没有办法改变scons的工作目录?

请让我知道我错过了什么。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

import os, sys

# Compile CPP
env_gpp_options = {
    'CXX'         : 'x86_64-pc-linux-g++',
    'CXXFLAGS'    : '-std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti',
    'LINK'        : 'x86_64-pc-linux-ld',
    'LINKFLAGS'   : '-melf_i386 -T scripts/elf_i386.x -nostdlib -e main -Ttext 0x10200',
}
env_gpp = Environment(**env_gpp_options)
env_gpp.Append(ENV = {'PATH' : os.environ['PATH']})

object_cpp_list = Glob('*.cpp')

object_target_list = []
for object_cpp in object_cpp_list:
    object_target_list.extend(env_gpp.Object(object_cpp))

# Linking
env_link_target = 'kernel32.elf'

env_gpp.Program(env_link_target, object_target_list)

或者

import os, sys

# Compile CPP
env_gpp_options = {
    'CXX'         : 'x86_64-pc-linux-g++',
    'CXXFLAGS'    : '-std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti',
    'LINK'        : 'x86_64-pc-linux-ld',
    'LINKFLAGS'   : '-melf_i386 -T scripts/elf_i386.x -nostdlib -e main -Ttext 0x10200',
}
env_gpp = Environment(**env_gpp_options)
env_gpp.Append(ENV = {'PATH' : os.environ['PATH']})

object_cpp_list = Glob('*.cpp')

# Linking
env_link_target = 'kernel32.elf'

env_gpp.Program(env_link_target, object_cpp_list)

答案 1 :(得分:0)

这是完整的scons脚本。

在项目根目录中,

#SConstruct

build_dir = 'build'

# Build
SConscript(['src/SConscript'], variant_dir = build_dir, duplicate = 0)

# Clean
Clean('.', build_dir)

在src目录中

#SConscript for src
SConscript(['bootloader/SConscript',
        'kernel32/SConscript'])

在kernel32目录

#SConscript for kernel32
import os, sys

# Build entry
env_entry = Environment(tools=['default', 'nasm'])

target_entry = 'entry.bin'

object_entry = 'entry.s'

output_entry = env_entry.Object(target_entry, object_entry)

# Compile CPP
env_gpp_options = {
    'CXX'       : 'x86_64-pc-linux-g++',
    'CXXFLAGS'  : '-std=c++11 -g -m32 -ffreestanding -fno-exceptions -fno-rtti',
    'LINK'        : 'x86_64-pc-linux-ld',
    'LINKFLAGS'   : '-melf_i386 -T scripts/elf_i386.x -nostdlib -e main -Ttext 0x10200',
}
env_gpp = Environment(**env_gpp_options)
env_gpp.Append(ENV = {'PATH' : os.environ['PATH']})

object_cpp_list = Glob('*.cpp')

for object_cpp in object_cpp_list:
    env_gpp.Object(object_cpp)

# Compile ASM
env_nasm = Environment(tools=['default', 'nasm'])
env_nasm.Append(ASFLAGS='-f elf32')

object_nasm_list = Glob('*.asm')

for object_nasm in object_nasm_list:
    env_nasm.Object(object_nasm)

# Find all object file
object_target_list = Glob('*.o')
object_target_list.append('entry.bin')

# Linking
env_link_target = 'kernel32.elf'

env_gpp.Program(env_link_target, object_target_list)

感谢您的关注。