Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to boost::program_options does not work properly

The following library files exist:

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

I include the following header with #include <boost/program_options.hpp>:

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

I try to link the libraries with

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

(I assume that the name of the library is derived from the file name minus the "lib" and the file ending. If I try the name boost_program_options (without -mt), I get ld: library not found for -lboost_program_options)

This is my main function using the library:

#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;
    }
}

Why does this setup give me a symbol(s) not found error?

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

EDIT: Checking the library files with lipo gives

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

I'd like to link the static library.

like image 767
clstaudt Avatar asked Jan 18 '13 10:01

clstaudt


1 Answers

Read the error message

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

It very clearly says, missing symbol std::__debug::vector, note that the std::__debug namespace is key here. I suspect you've built your source code using iterator debugging with -D_GLIBCXX_DEBUG but are attempting to link against a library that hasn't, such as boost program options. You didn't show us how you built your translation units, so here is a 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

There are a few ways to solve this

  • remove the -DGLIBCXX_DEBUG flag from translation units including the program options headers.
  • consult your boost package maintainer to see if they offer a version built with -D_GLIBCXX_DEBUG. Some variants on Linux do, you might ask around the MacPorts or brew users mailing lists.
like image 67
Sam Miller Avatar answered Sep 22 '22 13:09

Sam Miller