I am trying to build boost from source and I need static linking as I am thinking to move this statically linked boost project to AWS lambda servers.
I have done the following steps:
- I downloaded boost_1_72_0.tar.gz and
tar xzvf boost_1_72_0.tar.gz
inside third_party directory. cd
inside boost_1_72_0.DST_DIR=/my local path/
where I want to install../bootstrap.sh --prefix=${DST_DIR} --includedir=${DST_DIR} --libdir={DST_DIR} --with-libraries=filesystem,system
../b2 link=static --prefix=${DST_DIR} install --
which creates include and lib directories insideDST_DIR
- Inside my build directory I am doing CMake where my CMakeLists.txt is present.
- The CMake command works fine.
- But the
make
command gives an error.
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project(executable LANGUAGES CXX)
set(TARGET "/path to boost directory/boost_1_72_0")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBS OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")
set(Boost_NO_SYSTEM_PATHS True)
message(STATUS "${Boost_NO_SYSTEM_PATHS}")
if (Boost_NO_SYSTEM_PATHS)
set(BOOST_ROOT "${TARGET}")
message(STATUS "${Boost_ROOT}")
set(BOOST_INCLUDE_DIRS "${TARGET}/include/")
set(BOOST_LIBRARY_DIRS "${TARGET}/lib/")
endif (Boost_NO_SYSTEM_PATHS)
message(STATUS "${BOOST_INCLUDE_DIRS}")
message(STATUS "boost library dirs")
message(STATUS "${BOOST_LIBRARY_DIRS}")
find_package(Boost REQUIRED filesystem system)
if(Boost_FOUND)
include_directories(${BOOST_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} "execute_code.cpp")
message(STATUS "boost libraries" "${BOOST_LIBRARIES}")
target_link_libraries(executable ${BOOST_LIBRARIES})
endif()
execute_code.cpp
#include <boost/filesystem.hpp>
#include<iostream>
bool path_exists(string file_path)
{
boost::filesystem::path p{file_path};
return boost::filesystem::exists(p);
}
int main(int argc, char** argv) {
string source_file = argv[1];
if (!path_exists(source)) {
cout << "source file doesn't exist";
return 0;
}
return 0;
}
Error I am getting is:
CMakeFiles/executable.dir/execute_code.cpp.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
execute_code.cpp:(.text._ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x2f): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status
CMakeFiles/executable.dir/build.make:113: recipe for target 'executable' failed
make[2]: *** [executable] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/executable.dir/all' failed
make[1]: *** [CMakeFiles/executable.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I am new to C++ and CMake. Is there anything silly, which I am missing here?
I went through How can I get CMake to find my alternative Boost installation? and other answers but doesn't seem to work in my case.