构建共享库时类方法的多重定义

时间:2014-05-04 15:40:07

标签: c++ linker scons

我正在使用SConstruct构建一个C ++库。如果我将库构建为静态库,一切都按预期工作。

但是,如果我尝试将库构建为共享库,我会在链接时获得一个巨大的“多重定义”错误列表。

我不明白为什么会抛出这些错误 - 据我所知,我没有多次定义这些方法。看起来这些方法似乎是virtual,这可能是也可能不是因素。

相关文件:
ISceneManager.hpp
BasicSceneManager.hpp(继承自ISceneManager
BasicSceneManager.cpp

错误摘录:

build/BasicSceneManager.os: In function `glr::BasicSceneManager::drawAll()':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:81: multiple definition of `glr::BasicSceneManager::drawAll()'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:81: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::setDefaultShaderProgram(glr::shaders::IShaderProgram*)':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:118: multiple definition of `glr::BasicSceneManager::setDefaultShaderProgram(glr::shaders::IShaderProgram*)'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:118: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getModelMatrix() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:128: multiple definition of `glr::BasicSceneManager::getModelMatrix() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:128: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getShaderProgramManager() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:133: multiple definition of `glr::BasicSceneManager::getShaderProgramManager() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:133: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getCamera() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:162: multiple definition of `glr::BasicSceneManager::getCamera() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:162: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::destroyCamera()':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:236: multiple definition of `glr::BasicSceneManager::destroyCamera()'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:236: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getBillboardManager() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:276: multiple definition of `glr::BasicSceneManager::getBillboardManager() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:276: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getModelManager() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:281: multiple definition of `glr::BasicSceneManager::getModelManager() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:281: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getNumSceneNodes() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:311: multiple definition of `glr::BasicSceneManager::getNumSceneNodes() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:311: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getNumLights() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:316: multiple definition of `glr::BasicSceneManager::getNumLights() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:316: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getEnvironmentManager()':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:299: multiple definition of `glr::BasicSceneManager::getEnvironmentManager()'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:299: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getTerrainManager(glr::terrain::IFieldFunction*)':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:287: multiple definition of `glr::BasicSceneManager::getTerrainManager(glr::terrain::IFieldFunction*)'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:287: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getSceneNode(glr::Id) const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:138: multiple definition of `glr::BasicSceneManager::getSceneNode(glr::Id) const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:138: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getLight(glr::Id) const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:167: multiple definition of `glr::BasicSceneManager::getLight(glr::Id) const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:167: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getSceneNode(std::string const&) const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:150: multiple definition of `glr::BasicSceneManager::getSceneNode(std::string const&) const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:150: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getLight(std::string const&) const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:179: multiple definition of `glr::BasicSceneManager::getLight(std::string const&) const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:179: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::getDefaultShaderProgram() const':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:123: multiple definition of `glr::BasicSceneManager::getDefaultShaderProgram() const'
build/BasicSceneManager.os:/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:123: first defined here
build/BasicSceneManager.os: In function `glr::BasicSceneManager::destroySceneNode(glr::ISceneNode*)':
/home/jarrett/projects/icebreakers/glr/src/BasicSceneManager.cpp:221: multiple definition of `glr::BasicSceneManager::destroySceneNode(glr::ISceneNode*)'

有谁知道为什么我会收到这些错误?

1 个答案:

答案 0 :(得分:1)

事实证明问题出在SConstruct

我使用Glob命令收集我的源文件,如下所示:

source_files = Glob('build/*.cpp')
source_files = source_files + Glob('build/exceptions/*.cpp')
source_files = source_files + Glob('build/gui/*.cpp')
source_files = source_files + Glob('build/gui/cef/*.cpp')
source_files = source_files + Glob('build/models/*.cpp')
source_files = source_files + Glob('build/*.cpp')
source_files = source_files + Glob('build/environment/*.cpp')
source_files = source_files + Glob('build/terrain/*.cpp')
source_files = source_files + Glob('build/terrain/marching_cubes/*.cpp')
source_files = source_files + Glob('build/serialize/*.cpp')
...

env.SharedLibrary('build/glr', source_files, LIBS = libraries, LIBPATH = library_paths)

问题是我有一个重复的source_files = source_files + Glob('build/*.cpp')行,导致某些源文件在链接器中被包含两次。

删除该重复行,它工作得很好。

这只是SharedLibrary构建器的一个问题 - 使用StaticLibrary构建器时没有发生这种情况。此问题已记录在案here

感谢kec帮助诊断此问题。