Search code examples
c++c++11boostapple-m1

How to fix undefined symbols for arm64 when using boost::filesystem on m1 macbook?


I'm trying to work with file paths in c++11. The overwhelming advice on the internet is to use the boost library where I should be able to do something like this.

I installed boost(version 1.81.0_1) with brew and here is my code:

#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
    
int main(int argc, char* argv[]){
   if (argc < 2){
     std::cout << "Usage: tut1 path\n";
     return 1;
   }
   std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
   return 0;
}

I also added following lines to my ~.zshrc:

export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib

When I run:

clang main.cpp

It prints:

Undefined symbols for architecture arm64:
"boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)"
...
ld: symbol(s) not found for architecture arm64

According to this post I also need to give some -l flags and I've tried out setting different ones but no luck yet.


Solution

  • You need to link the boost filesystem library, for example

    $ clang++ -I/opt/homebrew/include -L/opt/homebrew/lib -lboost_filesystem main.cpp -o main
    

    -I adds the path for searching include files

    -L adds the path to look for library files

    -l indicates de library name