cannot find -lboost_filesystem

I found a code from GitHub whose CMakeLists .txt file looks like this:

cmake_minimum_required(VERSION 3.7)
project(obj_loader)

execute_process(COMMAND python -c "import tensorflow; print(tensorflow.sysconfig.get_include())" OUTPUT_VARIABLE Tensorflow_default_INCLUDE_DIRS)
set(Tensorflow_INCLUDE_DIRS "${Tensorflow_default_INCLUDE_DIRS}" CACHE PATH "Tensorflow include path")
include_directories(${Tensorflow_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES obj_loader.cpp)
add_library(obj_loader SHARED ${SOURCE_FILES})
target_link_libraries(obj_loader boost_filesystem)

I get the following error when I run the mingw32-make command

[ 50%] Building CXX object CMakeFiles/obj_loader.dir/obj_loader.cpp.obj
[100%] Linking CXX shared library libobj_loader.dll
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lboost_filesystem
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\obj_loader.dir\build.make:99: libobj_loader.dll] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/obj_loader.dir/all] Error 2
mingw32-make: *** [Makefile:90: all] Error 2

What is the -lboost_filesystem error related to?
Thank you in advance

Here the code says "link to boost_filesystem", but nothing indicates where this file could exist. I would recommend that the code instead do:

find_package(Boost REQUIRED COMPONENTS filesystem)
target_link_libraries(obj_loader Boost::filesystem)

CMake will end up with an error if Boost cannot be found and it can be cycled at configure rather than build/link time.

Thank you
I used alternative codes, however, despite finding the boost, it gives me the following error:

– The C compiler identification is GNU 10.3.0
– The CXX compiler identification is GNU 10.3.0
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working C compiler: C:/msys64/mingw64/bin/gcc.exe - skipped
– Detecting C compile features
– Detecting C compile features - done
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Check for working CXX compiler: C:/msys64/mingw64/bin/g++.exe - skipped
– Detecting CXX compile features
– Detecting CXX compile features - done
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:438 (message):
The package name passed to find_package_handle_standard_args (Boost) does
not match the name of the calling package (BOOST). This can lead to
problems in calling code that expects find_package result variables
(e.g., _FOUND) to follow a certain pattern.
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.21/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
CMakeLists.txt:5 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.

– Found Boost: C:/Program Files/boost_1_70_0/boost_1_70_0 (found version “1.70.0”)
– Configuring done
CMake Warning:
Value of Tensorflow_INCLUDE_DIRS contained a newline; truncating

CMake Error at CMakeLists.txt:14 (add_library):
Target “obj_loader” links to target “BOOST::filesystem” but the target was
not found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?

– Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.

Enviroment Variables :
BOOST_ROOT : C:\Program Files\boost_1_70_0\boost_1_70_0
BOOST_INCLUDEDIR : C:\Program Files\boost_1_70_0\boost_1_70_0
BOOST_LIBRARYDIR : C:\Program Files\boost_1_70_0\boost_1_70_0\stage\lib

Installed :

  • Windows 10 64 bit
  • cmake version 3.21.3
  • gcc (Rev5, Built by MSYS2 project) 10.3.0
  • Boost_1_70_0

Could you share the find_package() call you actually used?

These are the changes I made to the CMakeList.txt file

cmake_minimum_required(VERSION 3.7)
project(obj_loader)

cmake_policy(SET CMP0074 NEW)
find_package(BOOST REQUIRED COMPONENTS filesystem)

execute_process(COMMAND python -c “import tensorflow; print(tensorflow.sysconfig.get_include())” OUTPUT_VARIABLE Tensorflow_default_INCLUDE_DIRS)
set(Tensorflow_INCLUDE_DIRS “${Tensorflow_default_INCLUDE_DIRS}” CACHE PATH “Tensorflow include path”)
include_directories(${Tensorflow_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES obj_loader.cpp)
add_library(obj_loader SHARED ${SOURCE_FILES})
target_link_libraries(obj_loader BOOST::filesystem)

I reinstall boost 1.70 with the help of Installing boost libraries for GCC (MinGW) on Windows
now my error has changed to :

– The C compiler identification is GNU 10.3.0
– The CXX compiler identification is GNU 10.3.0
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working C compiler: C:/msys64/mingw64/bin/gcc.exe - skipped
– Detecting C compile features
– Detecting C compile features - done
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Check for working CXX compiler: C:/msys64/mingw64/bin/g++.exe - skipped
– Detecting CXX compile features
– Detecting CXX compile features - done
– Found Boost 1.70.0 at C:/Program Files/boost/lib/cmake/Boost-1.70.0
– Requested configuration: QUIET REQUIRED COMPONENTS filesystem
– Found boost_headers 1.70.0 at C:/Program Files/boost/lib/cmake/boost_headers-1.70.0
– Found boost_filesystem 1.70.0 at C:/Program Files/boost/lib/cmake/boost_filesystem-1.70.0
– No suitable boost_filesystem variant has been identified!
– libboost_filesystem-mgw103-mt-d-x32-1_70.a (32 bit, need 64)
– libboost_filesystem-mgw103-mt-d-x64-1_70.a (mgw103, detected mgw10, set Boost_COMPILER to override)
– libboost_filesystem-mgw103-mt-x32-1_70.a (32 bit, need 64)
– libboost_filesystem-mgw103-mt-x64-1_70.a (mgw103, detected mgw10, set Boost_COMPILER to override)
CMake Error at C:/Program Files/boost/lib/cmake/Boost-1.70.0/BoostConfig.cmake:95 (find_package):
Found package configuration file:

C:/Program Files/boost/lib/cmake/boost_filesystem-1.70.0/boost_filesystem-config.cmake

but it set boost_filesystem_FOUND to FALSE so package “boost_filesystem” is
considered to be NOT FOUND. Reason given by package:

No suitable build variant has been found.

Call Stack (most recent call first):
C:/Program Files/boost/lib/cmake/Boost-1.70.0/BoostConfig.cmake:124 (boost_find_dependency)
C:/Program Files/CMake/share/cmake-3.21/Modules/FindBoost.cmake:594 (find_package)
CMakeLists.txt:5 (find_package)

– Configuring incomplete, errors occurred!
See also “D:/project/bmvc-code/obj_loader/cmake-build-release/CMakeFiles/CMakeOutput.log”.

It should be Boost, not BOOST. The find_package works because you’re on Windows and the case-insensitive file name behaviors make it work. However, CMake is case-sensitive and the targets it makes are named Boost::. Using the right casing will also resolve the warning about the case mismatch you received.

Ben I did as you said, but I still have the same problem. Please take a look at the picture

The output is saying that the Boost you have is not suitable. It found debug (the -d libraries) and release libraries. Of those, the 32bit ones are unsuitable (expected) and the x64 ones are built using MSVC which is not compatible with MinGW. You’ll need a Boost built with the MinGW toolchain.

Whoops, they are MinGW, just not the right version. They are MinGW 10.0 when you have 10.3. I think setting Boost_COMPILER to mgw10 might work?

I tested Boost_COMPILER with gcc and mingw / numbers 10 and 103.
Only error :
- libboost_filesystem-mgw103-mt-d-x64-1_70.a (mgw103, detected mgw10, set Boost_COMPILER to override)
converted to
- libboost_filesystem-mgw103-mt-x64-1_70.a (mgw103, Boost_COMPILER = gcc10).

I followed the error. Cannot set __boost_variants and __boost_configs in boost_filesystem-config.cmake. This probably goes back to the same issue you mentioned, but I have gcc (Rev5, Built by MSYS2 project) 10.3.0 installed on the system and I also installed boost 1.70 with the same gcc, so where does this mismatch come from?

Boost_filesystem-config.cmake
# Generated by Boost 1.70.0

if(TARGET Boost::filesystem)
  return()
endif()

message(STATUS "Found boost_filesystem ${boost_filesystem_VERSION} at ${boost_filesystem_DIR}")

# Compute the include and library directories relative to this file.
get_filename_component(_BOOST_CMAKEDIR "${CMAKE_CURRENT_LIST_DIR}/../" ABSOLUTE)
get_filename_component(_BOOST_INCLUDEDIR "${_BOOST_CMAKEDIR}/../../include/boost-1_70/" ABSOLUTE)
get_filename_component(_BOOST_LIBDIR "${_BOOST_CMAKEDIR}/../" ABSOLUTE)

# Create imported target Boost::filesystem
add_library(Boost::filesystem UNKNOWN IMPORTED)

set_target_properties(Boost::filesystem PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_BOOST_INCLUDEDIR}"
  INTERFACE_COMPILE_DEFINITIONS "BOOST_ALL_NO_LIB"
)

include(${CMAKE_CURRENT_LIST_DIR}/../BoostDetectToolset-1.70.0.cmake)

if(Boost_DEBUG)
  message(STATUS "Scanning ${CMAKE_CURRENT_LIST_DIR}/libboost_filesystem-variant*.cmake")
endif()

file(GLOB __boost_variants "${CMAKE_CURRENT_LIST_DIR}/libboost_filesystem-variant*.cmake")

macro(_BOOST_SKIPPED fname reason)
  if(Boost_DEBUG)
    message(STATUS "  ... skipped ${fname} (${reason})")
  endif()
  list(APPEND __boost_skipped "${fname} (${reason})")
endmacro()

foreach(f IN LISTS __boost_variants)
  if(Boost_DEBUG)
    message(STATUS "  Including ${f}")
  endif()
  include(${f})
endforeach()

unset(_BOOST_LIBDIR)
unset(_BOOST_INCLUDEDIR)
unset(_BOOST_CMAKEDIR)

get_target_property(__boost_configs Boost::filesystem IMPORTED_CONFIGURATIONS)

if(__boost_variants AND NOT __boost_configs)
  message(STATUS "No suitable boost_filesystem variant has been identified!")
  if(NOT Boost_DEBUG)
    foreach(s IN LISTS __boost_skipped)
      message(STATUS "  ${s}")
    endforeach()
  endif()
  set(boost_filesystem_FOUND 0)
  set(boost_filesystem_NOT_FOUND_MESSAGE "No suitable build variant has been found.")
  unset(__boost_skipped)
  unset(__boost_configs)
  unset(__boost_variants)
  unset(_BOOST_FILESYSTEM_DEPS)
  return()
endif()

unset(__boost_skipped)
unset(__boost_configs)
unset(__boost_variants)

if(_BOOST_FILESYSTEM_DEPS)
  list(REMOVE_DUPLICATES _BOOST_FILESYSTEM_DEPS)
  message(STATUS "Adding boost_filesystem dependencies: ${_BOOST_FILESYSTEM_DEPS}")
endif()

foreach(dep_boost_filesystem IN LISTS _BOOST_FILESYSTEM_DEPS)
  set(_BOOST_QUIET)
  if(boost_filesystem_FIND_QUIETLY)
    set(_BOOST_QUIET QUIET)
  endif()
  set(_BOOST_REQUIRED)
  if(boost_filesystem_FIND_REQUIRED)
    set(_BOOST_REQUIRED REQUIRED)
  endif()
  get_filename_component(_BOOST_CMAKEDIR "${CMAKE_CURRENT_LIST_DIR}/../" ABSOLUTE)
  find_package(boost_${dep_boost_filesystem} 1.70.0 EXACT CONFIG ${_BOOST_REQUIRED} ${_BOOST_QUIET} HINTS ${_BOOST_CMAKEDIR})
  set_property(TARGET Boost::filesystem APPEND PROPERTY INTERFACE_LINK_LIBRARIES Boost::${dep_boost_filesystem})
  unset(_BOOST_QUIET)
  unset(_BOOST_REQUIRED)
  unset(_BOOST_CMAKEDIR)
  if(NOT boost_${dep_boost_filesystem}_FOUND)
    set(boost_filesystem_FOUND 0)
    set(boost_filesystem_NOT_FOUND_MESSAGE "A required dependency, boost_${dep_boost_filesystem}, has not been found.")
    unset(_BOOST_FILESYSTEM_DEPS)
    return()
  endif()
endforeach()

unset(_BOOST_FILESYSTEM_DEPS)

I’m sorry, this is out of my Boost knowledge. I recommend asking the boost-cmake team what might be up here.

@shayan_Hojati and you have exact same error message when setting Boost_COMPILER to mgw103?

Thank you so much for following up :wink:

Yes . I had to reinstall Boost, but this time I changed both the boost version and the gcc version!
As my impression, the problem may have been with MSYS2, however, for those who refer to this topic later, I used Boost 1.77 and gcc 8.1.0, and it worked for me.

But now I have another problem!!!

In the CMakeList .txt file where I put the line :

execute_process (COMMAND python -c “import tensorflow; print (tensorflow.sysconfig.get_include ())” OUTPUT_VARIABLE Tensorflow_default_INCLUDE_DIRS)

Returns the path of the tensorflow, but when I want to use op.h,OpKernel in the objectLoder.cpp file, I find the following error :

CMakeFiles\obj_loader.dir/objects.a(obj_loader.cpp.obj):obj_loader.cpp:(.text$_ZN11ObjLoaderOpD1Ev[_ZN11ObjLoaderOpD1Ev]+0x46): undefined reference to `tensorflow::OpKernel::~OpKernel()

And my addressing type in, objectloader.cpp is as follows :

#include “tensorflow/core/framework/op.h”
#include “tensorflow/core/framework/op_kernel.h”
#include “tensorflow/core/framework/shape_inference.h”

Where do you think the problem comes from?

That gets the include directories. You also need to link to TensorFlow to get its symbols at link time.

Sorry, I did not understand what you meant

Just because you can include headers does not mean that you’ve done everything that is needed. I don’t know TensorFlow, so you’ll have to look there for what exactly is needed, but you need to ask for the libraries that you need to link against (probably similar mechanisms to getting its include directories).