[Error] undefined reference to 'boost:... ', undefinedboost

Source: Internet
Author: User

[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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.