6

存在以下库文件:

cls /usr/local/Cellar/boost/1.51.0/lib $ ls libboost_program*
libboost_program_options-mt.a       libboost_program_options-mt.dylib

我包含以下标题#include <boost/program_options.hpp>

cls /usr/local/Cellar/boost/1.51.0/include $ ls boost/program_options.hpp
boost/program_options.hpp

我尝试将库与

-lboost_program_options-mt
-L/usr/local/Cellar/boost/1.51.0/lib

(我假设库的名称是从文件名减去“lib”和文件结尾得出的。如果我尝试名称boost_program_options(不带-mt),我得到ld: library not found for -lboost_program_options

这是我使用库的主要功能:

#include <boost/program_options.hpp>
#include <iostream

int main(int argc, char **argv) {

    namespace opts = boost::program_options;

    opts::options_description desc("EnsembleClustering options");
    desc.add_options()
            ("hello", "produce greeting");

    opts::variables_map vmap;
    opts::store(opts::parse_command_line(argc, argv, desc), vmap);
    opts::notify(vmap);

    if (vmap.count("hello")) {
        std::cout << "Hello World" << std::endl;
    }
}

为什么这个设置会给我一个symbol(s) not found错误?

make all 
Building target: EnsembleClustering-DPar
Invoking: MacOS X C++ Linker
/usr/local/bin/g++-4.7 -L"/Users/cls/workspace/STINGER/OpenMP Debug" -L"/Users/cls/workspace/gtest/lib" -L/usr/local/Cellar/log4cxx/0.10.0/lib -L/usr/local/Cellar/boost/1.51.0/lib -fopenmp -std=c++11 -o "EnsembleClustering-DPar"  ./src/scoring/EdgeScoring.o ./src/scoring/ModularityScoring.o  ./src/overlap/test/OverlapGTest.o  ./src/overlap/Overlapper.o ./src/overlap/RegionGrowingOverlapper.o  ./src/matching/Matcher.o ./src/matching/Matching.o ./src/matching/ParallelMatcher.o  ./src/io/test/InputGTest.o  ./src/io/CSVWriter.o ./src/io/GraphFromAdjacencies.o ./src/io/GraphIO.o ./src/io/GraphReader.o ./src/io/METISGraphReader.o ./src/io/METISParser.o ./src/io/METISToGraph.o  ./src/graph/test/GraphGTest.o ./src/graph/test/STINGERGTest.o  ./src/graph/Graph.o ./src/graph/GraphGenerator.o  ./src/ensemble/test/EnsembleGTest.o  ./src/ensemble/EnsembleClusterer.o  ./src/coarsening/test/CoarseningGTest.o  ./src/coarsening/ClusterContracter.o ./src/coarsening/ClusteringProjector.o ./src/coarsening/Contracter.o ./src/coarsening/GraphContraction.o ./src/coarsening/MatchingContracter.o  ./src/clustering/test/ClusteringGTest.o  ./src/clustering/base/Clustering.o ./src/clustering/base/ClusteringGenerator.o ./src/clustering/base/Modularity.o ./src/clustering/base/QualityMeasure.o  ./src/clustering/algo/test/ClusteringAlgoGTest.o  ./src/clustering/algo/Clusterer.o ./src/clustering/algo/LabelPropagation.o ./src/clustering/algo/ParallelAgglomerativeClusterer.o  ./src/aux/test/AuxGTest.o  ./src/aux/Noise.o ./src/aux/RandomInteger.o ./src/aux/RandomProbability.o ./src/aux/Timer.o  ./src/EnsembleClustering.o   -lSTINGER -lgtest -llog4cxx -lboost_program_options-mt
Undefined symbols for architecture x86_64:
  "boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)", referenced from:
      boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*) in EnsembleClustering.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [EnsembleClustering-DPar] Error 1

编辑:检查库文件lipo

cls /usr/local/Cellar/boost/1.51.0/lib $ lipo -detailed_info libboost_program_options-mt.a libboost_program_options-mt.dylib 
input file libboost_program_options-mt.a is not a fat file
input file libboost_program_options-mt.dylib is not a fat file
Non-fat file: libboost_program_options-mt.a is architecture: x86_64
Non-fat file: libboost_program_options-mt.dylib is architecture: x86_64

我想链接静态库。

4

2 回答 2

4

阅读错误信息

Undefined symbols for architecture x86_64:
  "boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)", referenced from:
      boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*) in EnsembleClustering.o

它非常清楚地说,缺少符号std::__debug::vector,请注意std::__debug命名空间是这里的关键。我怀疑您已经使用迭代器调试构建了源代码,-D_GLIBCXX_DEBUG但正在尝试链接一个没有的库,例如 boost 程序选项。您没有向我们展示您是如何构建翻译单元的,所以这里有一个sscce

$ g++-4.8 -std=c++11 -O2 -pthread main.cpp -D_GLIBCXX_DEBUG -lboost_program_options && ./a.out
/tmp/cca1jwUx.o: In function `boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)':
main.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0x319): undefined reference to `boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::string, std::allocator<std::string> > const&)'
collect2: error: ld returned 1 exit status

有几种方法可以解决这个问题

  • 从包括程序选项标题的翻译单元中删除-DGLIBCXX_DEBUG标志。
  • 请咨询您的 boost 包维护者,看看他们是否提供使用-D_GLIBCXX_DEBUG. Linux 上的某些变体可以,您可能会询问 MacPorts 或 brew 用户邮件列表。
于 2013-03-27T03:49:14.487 回答
2

由于您要指定包含、库的位置并提供适当.dylib的 ,我能想到的唯一问题是您要链接到的库中没有 x86_64 拱门。

尝试使用file或检查库lipo。如果它没有显示 x86_64,那就是问题所在。

于 2013-01-18T11:13:54.933 回答