[Error] undefined reference to 'boost:... ', undefinedboost
Many new users refer to the Boost library for programming. Sometimes the following error occurs during ubuntu Compilation:
Test04.cpp :(. text + 0x2c): undefined reference to 'boost: program_options: options_description: m_default_line_length'
Test04.cpp :(. text + 0x37): undefined reference to 'boost: program_options: options_description: m_default_line_length'
Test04.cpp :(. text + 0x7c): undefined reference to 'boost: program_options: options_description (std ::__ cxx11: basic_string <char, std: char_traits <char>, std: allocator <char> const &, unsigned int, unsigned int )'
This problem occurs because the required libraries are not linked to the Link when the C ++ project file is compiled using g ++. There are two solutions:
(1) Add configuration options during file Compilation
For example, the test. cpp statement is:
G ++-std = c ++ 11-o test. elf test. cpp
When the Boost library is used, add "-lboost _ specific database name" to the end to change the compilation statement:
G ++-std = c ++ 11-o test. elf test. cpp-lboost _Program_options
If the error is "undefined reference to 'boost: system:... '", you can add-lboost _System
Note: when using the C ++ 11 standard library, you must add "-std = c ++ 11" to the compilation configuration ", in half the case, the new Boost version will be mixed with C ++ 11 content, so it is best to add this configuration item.
(2) modify the configuration in Makefile
For example, open Makefile to find the rule.
$ (LINK) $ (LFLAGS)-o $ (TARGET) $ (OBJECTS) $ (OBJCOMP) $ (LIBS)
To:
$ (LINK) $ (LFLAGS)-o $ (TARGET) $ (OBJECTS) $ (OBJCOMP) $ (LIBS)-lboost _System-Lboost _Thread
Note that the format of Makefile/tab is different from that of space. If you use QTCreator to open Makefile, the/tab will be replaced by several spaces when saving the Makefile, which will cause the subsequent make tasks to fail, therefore, you must use an editor such as Vim to open the changes.
(3) about # include <>
Use Boost to add the following:
# Include <boost/program_options/parsers. hpp>
# Include <boost/program_options/variables_map.hpp>
# Include <boost/program_options/options_description.hpp>
# Include ...........................
And use 1:
# Include <boost/program_options.hpp>
The function is the same. If you can accurately know which file the class belongs to, you 'd better use the first # include method. If you don't know, you can use the second method.