Mac和boost库上的Qt Creator

Qt Creator on Mac and boost libraries

本文关键字:Qt Creator boost Mac      更新时间:2023-10-16

我正在Mac上运行QtCreator。。。我想开始研究boost库。。。因此,我使用安装了boost库

brew install boost

之后,我创建了一个小的boost hallo世界程序,并在.pro文件中进行了如下的更改

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

我仍然无法构建。。。原因可能是什么?请告诉我可能的错误是什么。。。

错误是

library not found for -lboost_data_time
linker command failed with exit code 1 (use -v to see invocation)

这有点偏离了Uflex的回答,因为他错过了一些东西。所以保持相同的代码:

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

但让我们改变一下他的职业生涯:

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
}

我唯一添加的是助推系统(-lbostrongystem)这应该解决了他的原始版本导致未定义符号的问题,并允许您添加其他库。

比如-lbost_date_time,对我来说,它与brew安装非常配合。

当然,我的路径实际上是:/usr/local/Cellar/boost/1.55.0_2

Boost库是模块化的,您只需要链接到您正在使用的库。有些库只是头,所以您不需要做任何事情,在您的路径中可以访问boost就足够了。

你可以尝试编译这个:

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

在.pro文件中:

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
}