3

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()
int ermedi_8
  • 171
  • 1
  • 14
  • When search header files, compiler looks only those paths **which you specify** for it (exception is system-wide headers, but it is definitely not your case). So, just do not include unneded directories. BTW, why you set *Boost_INCLUDE_DIRS* if `find_packge(Boost)` should set it for you? – Tsyvarev May 22 '17 at 21:53
  • I commented out the included directories but nothing changed. I added a "REQUIRED" to my find_package statement, to get a "nice error message" in case like suggested [here](https://stackoverflow.com/questions/1065011/why-is-this-boost-header-file-not-included?rq=1). The error message it throws me is: `Unable to find the requested Boost libraries. Unable to find the Boost header files. Please set BOOST_ROOT to the root directory containing Boost or BOOST_INCLUDEDIR to the directory containing Boost's headers` I tried setting those directories, but wasn't successful... – int ermedi_8 May 23 '17 at 06:56
  • Well, so **actual problem** is "not finding Boost", but not a failure to include its header as you stated in the question. `I tried setting those directories, but wasn't successful.` - Show what have you tried. And do not replace actual Boost header name with "RequiredHeader". – Tsyvarev May 23 '17 at 07:21
  • Thank you for your help so far. I don't thought it isn't clear what i tried, sorry for that ==> so now i updated my question, the title and added the new CMake Boost part – int ermedi_8 May 23 '17 at 07:48
  • Read error message carefully: it talks about `BOOST_INCLUDEDIR` variable, not `BOOST_INCLUDE_DIR`. By setting `Boost_DEBUG` variable to 1 (or any non-false value) you may get additional information about searching Boost. – Tsyvarev May 23 '17 at 08:10
  • The Boost_DEBUG was a good hint. I uploaded the output of my last gradle sync [here](https://pastebin.ca/3815851). Since it doesn't really helped me in finding any solution, i hope it will you :) – int ermedi_8 May 23 '17 at 09:01
  • Boost include directory should be one which contains `boost/config.hpp` file (that is, *include directory* should have `boost` subdirectory, which in turn should contain file named `config.hpp`). Check that `BOOST_INCLUDEDIR` is exactly that directory. – Tsyvarev May 23 '17 at 09:16
  • Thank you very much Tsyvarev! Though i resolved my issue in an other way than using find_package now, it was the hint which helped me finding an answer – int ermedi_8 May 29 '17 at 11:11
  • Why I get error in the line of find_package(..). Android studio may be not have the word "find_package" – Slim_user71169 Dec 23 '17 at 06:55

1 Answers1

2

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!

Community
  • 1
  • 1
int ermedi_8
  • 171
  • 1
  • 14
  • Typically you shouldn't need `target_include_directories` as the headers will be automatically included by `target_link_libraries`. For the header-only library, use `target_link_directories( PUBLIC/PRIVATE Boost::boost)` – Chris Watts Nov 29 '17 at 09:36
  • I can not use your lines. AS can't recognize the word "find_package" – Slim_user71169 Dec 25 '17 at 02:47
  • @Slim_user71169 I am using CMake 3.6 with Android Studio 3.0 and the build is working flawlessly. Try to check your CMake version (since find_package has nothing to do with AS). Can you provide the exact error message you are getting? – int ermedi_8 Jan 08 '18 at 13:36