2017-01-25 1 views
0

Ich brauche Hilfe auf, ich habe dies auf dem gleichen Computer getan, es bedeutet, ich habe bereits Boost-Bibliothek installiert und basiert auf dem vorherigen Code, aber dieses Mal gibt es mir Fehler: /tmp/ccpAYzPw.o: In Funktion `Haupt ':Was ist falsch an <boost/program_options/options_description.hpp>?

reading_data.cpp:(.text+0x356): undefined reference to `boost::program_options::options_description::m_default_line_length' 
reading_data.cpp:(.text+0x361): undefined reference to `boost::program_options::options_description::m_default_line_length' 
reading_data.cpp:(.text+0x3a6): undefined reference to `boost::program_options::options_description::options_description(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)' 
reading_data.cpp:(.text+0x3d3): undefined reference to `boost::program_options::options_description::add_options()' 
collect2: error: ld returned 1 exit status 
<builtin>: recipe for target 'reading_data' failed 

Ich verbrachte fast 2 Stunden, um zu sehen, was vor sich geht? aber ich konnte nicht verstehen warum, also brauche ich deine Hilfe.

Hier ist mein Code und ich danke Ihnen sehr.

#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <fstream>  // std::ifstream 
#include <boost/program_options.hpp> 

int main() 
{ 
    boost::program_options::options_description desc("Allowed options"); 

    desc.add_options() 
      ("sign" , program_options::value<string>() -> default_value("gbm") ,"name of the input") 
      ("week" , program_options::value<double>() -> default_value(1930)  ,"number of the week") 
      ("day" , program_options::value<double>() -> default_value(0)  ,"number of the day in within the week") 
      ("hour" , program_options::value<double>() -> default_value(0)  ,"time in hour") 
      ("minute", program_options::value<double>() -> default_value(0)  ,"time in minute") 
      ("second", program_options::value<double>() -> default_value(0)  ,"time in second") 
      ; 

    cout << "Done!" << endl; 
    return 0; 
} 
+3

Es sieht aus wie Sie nicht die Boost program_options Bibliothek verknüpft sind. – roalz

+0

Ich denke, das Semikolon am Ende von 'desc.add_options();' ist ein Problem für später. – Quentin

+3

** Keine Spam-Tags. ** Diese Frage hat nichts mit "c" oder "Informatik" zu tun. –

Antwort

1

Sie versehentlich eine überflüssige Semikolon am Ende der folgenden Zeile hinzugefügt:

desc.add_options(); // war ein Tippfehler im ursprünglichen Beitrag

Sie müssen auch Variablen zum Speichern der Eingabeoptionen deklarieren.

std::string sign; 
double week, day, hour, minute, second; 
desc.add_options() 
     ("sign" , program_options::value<std::string>(&sign) -> default_value("gbm") ,"name of the input") 
     ("week" , program_options::value<double>(&week)  -> default_value(1930)  ,"number of the week") 
     ("day" , program_options::value<double>(&day)  -> default_value(0)  ,"number of the day in within the week") 
     ("hour" , program_options::value<double>(&hour)  -> default_value(0)  ,"time in hour") 
     ("minute", program_options::value<double>(&minute) -> default_value(0)  ,"time in minute") 
     ("second", program_options::value<double>(&second) -> default_value(0)  ,"time in second") 
     ; 
+0

Das ist nicht das Problem: http://melpon.org/wandbox/permlink/bqnnDGUlfxFhFVkc –

+0

@vre: Es tut mir leid für die falsche Eingabe, die ";" sollte nicht da sein, auch wenn ich es ausziehe und mir immer noch die Fehler gibt. –

+0

@VittorioRomeo: Also, was ist das Problem? –

7

Es scheint, dass Sie die Bibliothek nicht verknüpfen. boost::program_optionsnicht header-only, so haben Sie es explizit verlinken:

-lboost_program_options 
+0

Es tut mir leid, ich verstehe nicht, was du meintest? –

+1

Einige Teile von Boost sind als Bibliotheken kompiliert. Dies ist einer von ihnen. Sie müssen Ihren Linker-Einstellungen Einstellungen hinzufügen. Er gab dir die Linker-Option dafür. – drescherjm

+0

@johnnguyen: Um "vorkompilierte" Bibliotheken zu verwenden, müssen Sie sie ** mit Ihrem Projekt verknüpfen. Dies ist eine gute Erklärung: http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c –

1

Boost program_options ist eine kompilierte Bibliothek, kein Kopf nur wie viele andere Boost-Bibliotheken.
Sie müssen die Bibliothek also beim Erstellen mit Ihrem Programm verknüpfen.

Wie Sie das tun, hängt stark von dem Compiler und der Plattform ab, die Sie verwenden.

Wenn Sie nicht wissen, was Verknüpfung dreht sich alles um, können Sie hier lesen: How does the compilation/linking process work?