【问题标题】:Undefined symbols for architecture x86_64: "_alloca"架构 x86_64 的未定义符号:“_alloca”
【发布时间】:2012-12-26 16:48:36
【问题描述】:

我正在尝试创建项目polyworld,但编译 qt_clust.o 时出错

g++ -o bin/qt_clust .bld/qt_clust/tools/clustering/qt_clust.o -L/usr/lib -L/usr/local/lib -L/usr/include -lz -lgsl -lgslcblas -lgomp

得到

"_alloca", referenced from:
      __Z38find_valid_neighbors__measureNeighborsP7ClusterRSt6vectorIiSaIiEEP22GeneDistanceDeltaCacheP19PopulationPartition.omp_fn.4 in qt_clust.o
     (maybe you meant: ParsedCluster* std::vector<ParsedCluster, std::allocator<ParsedCluster> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<ParsedCluster const*, std::vector<ParsedCluster, std::allocator<ParsedCluster> > > >(unsigned long, __gnu_cxx::__normal_iterator<ParsedCluster const*, std::vector<ParsedCluster, std::allocator<ParsedCluster> > >, __gnu_cxx::__normal_iterator<ParsedCluster const*, std::vector<ParsedCluster, std::allocator<ParsedCluster> > >))
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

我很确定问题出在这个文件上:https://github.com/JaimieMurdock/polyworld/blob/master/tools/clustering/qt_clust.cpp

我在 OSX Mountain Lion 上。

【问题讨论】:

    标签: c++ macos g++ alloca


    【解决方案1】:

    如果您更改了这些行:

        float dists[clusterNeighborCandidates.size() - (i+1)];
    
        compute_distances( distance_deltaCache,
                           neighborPartition->genomeCache,
                           clusterNeighborCandidates,
                           i, i+1, clusterNeighborCandidates.size(),
                           dists );
    

    到这里:

        ::std::vector<float> dists(clusterNeighborCandidates.size() - (i+1));
    
        compute_distances( distance_deltaCache,
                           neighborPartition->genomeCache,
                           clusterNeighborCandidates,
                           i, i+1, clusterNeighborCandidates.size(),
                           &(dists[0]) );
    

    我打赌这个问题会消失。

    问题是原始代码在堆栈上有一个动态大小的数组。编译器生成的代码调用“alloca”来从堆栈分配内存。不幸的是,这个函数是非标准的,而且总体上有着不为人知的历史。

    动态大小的数组虽然合法 C99,但不合法 C++03 或 C++11。我认为 g++ 和 clang 都支持它们作为扩展。但显然,在 OS X 下,这种支持略有下降。

    ::std::vector 巧妙地回避了这个问题。它不会在堆栈上分配数组。它在堆上分配它。

    【讨论】:

    • 非常感谢!它工作得很好。我听说alloca 没有警告堆栈溢出的问题,但认为因为它在 /usr/include 中,所以它可以编译。可能不会!感谢您的出色而快速的回答。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 2012-07-20
    • 1970-01-01
    • 2015-08-23
    • 2014-05-27
    • 2017-07-06
    • 2016-06-20
    • 2016-06-14
    相关资源
    最近更新 更多