سؤال

I am running QtCreator on Mac... I want to start working on boost libraries ... So, I installed boost libraries using

brew install boost

After that I created a small boost hallo world program and made the changes in .pro file as follows

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

unix:INCLUDEPATH += "/usr/local/Cellar/boost/1.55.0_1/include/"
unix:LIBPATH += "-L/usr/local/Cellar/boost/1.55.0_1/lib/"

SOURCES += main.cpp

LIBS += \
-lboost_date_time \
-lboost_filesystem \
-lboost_program_options \
-lboost_regex \
-lboost_signals \
-lboost_system

I am still unable to build... What could be the reason? Please suggest me what could be the possible mistake...

The errors are

library not found for -lboost_data_time
linker command failed with exit code 1 (use -v to see invocation)
هل كانت مفيدة؟

المحلول

This is taking a bit from Uflex's answer, as he missed something. So keep the same code:

//make sure that there is a boost folder in your boost include directory
#include <boost/chrono.hpp>
#include <cmath>

int main()
{
auto start = boost::chrono::system_clock::now();

    for ( long i = 0; i < 10000000; ++i )
        std::sqrt( 123.456L ); // burn some time

    auto sec = boost::chrono::system_clock::now() - start;
    std::cout << "took " << sec.count() << " seconds" << std::endl;

    return 0;
}

But lets change his .pro a bit:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

macx {
    QMAKE_CXXFLAGS += -std=c++11

    _BOOST_PATH = /usr/local/Cellar/boost/1.55.0_1
    INCLUDEPATH += "$${_BOOST_PATH}/include/"
    LIBS += -L$${_BOOST_PATH}/lib
    ## Use only one of these:
    LIBS += -lboost_chrono-mt -lboost_system # using dynamic lib (not sure if you need that "-mt" at the end or not)
    #LIBS += $${_BOOST_PATH}/lib/libboost_chrono-mt.a # using static lib
}

The only thing I have added to this was the boost system( -lboost_system ) That should solve the issue with his original version causing the undefined symbols, and allow you to add your other libraries.

Such as -lboost_date_time, which for me worked perfectly with the brew install.

Granted, my path is actually: /usr/local/Cellar/boost/1.55.0_2

نصائح أخرى

Boost libraries are modularized, you just need to link against the libraries that you are using. Some libraries are header only, so you don't need to do anything, having boost reachable in your path is enough.

You can try to compile this:

//make sure that there is a boost folder in your boost include directory
#include <boost/chrono.hpp>
#include <cmath>

int main()
{
    auto start = boost::chrono::system_clock::now();

    for ( long i = 0; i < 10000000; ++i )
        std::sqrt( 123.456L ); // burn some time

    auto sec = boost::chrono::system_clock::now() - start;
    std::cout << "took " << sec.count() << " seconds" << std::endl;

    return 0;
}

And in the .pro file:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

macx {
    QMAKE_CXXFLAGS += -std=c++11

    _BOOST_PATH = /usr/local/Cellar/boost/1.55.0_1
    INCLUDEPATH += "$${_BOOST_PATH}/include/"
    LIBS += -L$${_BOOST_PATH}/lib
    ## Use only one of these:
    LIBS += -lboost_chrono-mt # using dynamic lib (not sure if you need that "-mt" at the end or not)
    #LIBS += $${_BOOST_PATH}/lib/libboost_chrono-mt.a # using static lib
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top