Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: CMake is not finding Boost header files

I included a Boost-Header file to my test project using CMakeLists.txt. My some.cpp can include this header without any error, but i'm not able to run since the header file relies obviously on other Boost headers and its not finding the required files. The location of my files is in cpp folder and the boost files are in (C:\boost) a subdirectory:

..\src\main\cpp\boost\RequiredHeader.hpp.

For the include files in the "RequiredHeader" the compiler is looking at:

..\src\main\cpp\boost\boost\AnotherHeader.hpp.

CMakeLists.txt (Boost-part)

# ADD BOOST
message("Import Boost...\n")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_INCLUDE_DIRS C:/boost_1_64_0/boost)
find_package(Boost 1.64.0)

if(Boost_FOUND)
    message("Boost found! Link libraries...\n")
    include_directories(${Boost_INCLUDE_DIRS})
    target_link_libraries(myDependantLib ${Boost_LIBRARIES})
endif()

Your help is highly appreciated!

Updated question:
How to tell CMake where my Boost header files are, since it still is not finding the right location, with BOOST_ROOT set?

Updated CMakeLists.txt

# ADD BOOST
message("Add boost...\n")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_ROOT C:/boost_1_64_0)
set(BOOST_INCLUDE_DIR C:/boost_1_64_0/boost)

FIND_PACKAGE(Boost 1.64.0 COMPONENTS foreach REQUIRED)

if(Boost_FOUND)
    message("Boost found! Link libraries...\n")
    target_link_libraries(calculator LINK_PUBLIC ${Boost_LIBRARIES})
endif()
like image 455
int ermedi_8 Avatar asked Mar 10 '26 21:03

int ermedi_8


1 Answers

This post here helped me in resolving this.

Include Boost-Header files and libs:

set(BOOST_ROOT C:/boost)

The path containing the include headers "boost/*.hpp" and libraries "stage/lib" or any other path where your compiled files have been output.
Then you need to specify the include header and libs paths. In the default case the headers are stored in the same directory as the root (since "boost" folder is searched automatically) and the libs as described in "stage/lib". Otherwise it should be "include" and "lib" of your output directory, while the boost version has to be corresponding to the one specified in version.hpp in the "boost" folder:

set( Boost_INCLUDE_DIR ${BOOST_ROOT}/include )
set( Boost_LIBRARY_DIR ${BOOST_ROOT}/lib )
set( Boost_Version 1_64 )

find_package( Boost ${Boost_Version} COMPONENTS system thread )
if( Boost_FOUND )
    target_include_directories( <your_lib> PUBLIC/PRIVATE ${Boost_INCLUDE_DIR} )  


    # its possible to include boost to system path too:  
    # include_directories( SYSTEM ${Boost_INCLUDE_DIR} )

    link_directories( ${Boost_LIBRARY_DIR} )
    target_link_libraries( <your_lib> ${Boost_LIBRARIES} )
endif()

Then i was able to simply:

#include <boost/random.hpp>
#include <boost/whatever.hpp>

This worked for me on following environment:

  • Android Studio 2.3.1 and 3.0
  • NDK 14.1
  • Boost 1.56.0 and 1.64.0
  • Windows 10

If further explanation is needed, please comment your concerns!

like image 133
int ermedi_8 Avatar answered Mar 13 '26 14:03

int ermedi_8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!