无法在带有M1芯片的MacBook Air上编译带有Boost头文件的C++

jdg4fx2g  于 4个月前  发布在  Mac
关注(0)|答案(2)|浏览(93)

我试图运行一个C程序与Boost头.我工作在Macbook Air与M1芯片,并已安装最新版本的Boost与brew install boost.对于M1芯片的默认包含文件夹是/opt/homebrew/include,这是一个符号链接到/opt/homebrew/Cellar/boost/1.76.0.由于某种原因,当我编译与gcc(和g)我一直得到消息fatal error: 'boost/thread.hpp' file not found.
我的代码和控制台命令如下:

#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>

void wait(int seconds)
{
  boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}

void thread()
{
  using boost::this_thread::get_id;
  for (int i = 0; i < 5; ++i)
  {
    wait(1);
    std::cout << "Thread: " << i << std::endl;
  }
}

int main()
{
  boost::thread t1{thread};
  t1.join();
}

字符串
我使用的命令是gcc mythread.cpp -o mythread -l /opt/homebrew/include -l boost_thread-mt
我得到的错误消息是:

mythread.cpp:1:10: fatal error: 'boost/thread.hpp' file not found
#include <boost/thread.hpp>
         ^~~~~~~~~~~~~~~~~~
1 error generated.


我不明白为什么编译器看不到boost/thread. hpp。

pkwftd7m

pkwftd7m1#

对于M1芯片,默认包含文件夹是/opt/homebrew/include,这是/opt/homebrew/Cellar/boost/1.76.0的符号链接。
以上信息有误,请使用

-I/opt/homebrew/Cellar/boost/1.76.0/include

字符串
或者,如果有这样的参考,

-I/opt/homebrew/include

oprakyz7

oprakyz72#

g++ -L/opt/homebrew/Cellar/boost/1.82.0_1/lib -I/opt/homebrew/Cellar/boost/1.82.0_1/include/ -lboost_thread-mt --std=c++11 Main.cpp -o Main

字符串

  • -L连接boost c++中线程库的静态库
  • -I连接boost的头文件
  • -l启用线程管理类型

相关问题