仅使用macports GCC为架构x86_64复制符号?

时间:2017-12-07 11:24:07

标签: c++ linux macos gcc

使用gcc从私有存储库编译某些C ++代码时出错。因此,遗憾的是我无法共享代码。错误是duplicate symbols for architecture x86_64

g++ -o obj/manager.o -c src/manager.cc -O3 -w -std=c++11 -Iinclude -MMD
g++ -o obj/interface.o -c src/interface.cc -O3 -w -std=c++11 -Iinclude -MMD
...
g++ -o bin/bnmc ... obj/manager.o obj/interface.o ... -L***/lib64 -Wl,-Bstatic -lbnc -lgsl -ldlib -lbddc -ldlib -Wl,-Bdynamic -lpthread -lreadline -ltermcap -lgmp -lgmpxx -Wl,-R***/lib64 -Wl,-R***/usr/lib64 
duplicate symbol bnmc::ModelCounter<(bnmc::ModelType)0>::Read()    in:
    obj/modelcounter_pwpbdd.o
    obj/modelcounter.o
duplicate symbol bnmc::ModelCounter<(bnmc::ModelType)1>::Traverse(std::vector<bnmc::EvidenceEntry, std::allocator<bnmc::EvidenceEntry> > const&) in:
    obj/modelcounter.o
    obj/modelcounter_wpbdd.o
duplicate symbol bnmc::ModelCounter<(bnmc::ModelType)2>::Traverse(std::vector<bnmc::EvidenceEntry, std::allocator<bnmc::EvidenceEntry> > const&) in:
    obj/modelcounter.o
    obj/modelcounter_obdd.o
...
ld: 5 duplicate symbols for architecture x86_64
collect2: error: ld returned 1 exit status
Makefile:297: recipe for target 'bin/bnmc' failed
make: *** [bin/bnmc] Error 1

我知道这个错误意味着什么,但我不知道为什么它只出现在gcc的Macports版本上:

> gcc --version
gcc (MacPorts gcc5 5.4.0_1) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gcc 4.9.4和6.3.0也失败了。它使用以下命令在我的linux机器上失败:

> gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

有关代码的一些信息。编译器指示重复项是模板类成员函数。它的设置如下。类声明位于头文件中。

// someclass.h
template <typename T>
class someclass {
    public:
        T somefunction();
        T someotherfunction();
        ...
};

定义分为2个源文件。

// someclass.cc
template <typename T>
T someclass<T>::somefunction(){
    // foo
}
template <typename T>
T someclass<T>::someotherfunction(){
    // bar
}
...

template class someclass<int>;
template class someclass<...>;
...

一个单独的专业化文件:

// specialsomeclass.cc
template <>
int someclass<int>::somefuntion(){
    // foo
}

我可以解释为什么会出错,因为somefunction在2个源文件中定义,但我不明白为什么只有gcc的macports版本抱怨这个。有什么我可以或必须做的事情来编译吗?

0 个答案:

没有答案