linker could not found object files from different directory with scons

*爱你&永不变心* 提交于 2019-12-25 03:12:16

问题


Currently, I make a project with scons. I compiled source codes and it is time to link them. However, I got an error that ld cannot find object files.

The SConscript is located in src/kernel32, and

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)

and message I got is

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.

I checked the directory, build/kernel32/, and I found main.o file. What is my mistake? Is there an way to change working directory for scons?

Please let me know what I missed.


回答1:


You can try this:

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)

Or

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)



回答2:


This is full scons scripts.

In project root directory,

#SConstruct

build_dir = 'build'

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

# Clean
Clean('.', build_dir)

In src directory

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

In kernel32 directory

#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)

Thank you for your attention.



来源:https://stackoverflow.com/questions/34928921/linker-could-not-found-object-files-from-different-directory-with-scons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!