【发布时间】:2020-02-25 13:31:39
【问题描述】:
我正在尝试从源代码构建 boost,我需要静态链接,因为我正在考虑将这个静态链接的 boost 项目移动到 AWS lambda 服务器。
我做了以下步骤:
- 我在第三方目录中下载了 boost_1_72_0.tar.gz 和
tar xzvf boost_1_72_0.tar.gz。 -
cd在 boost_1_72_0 内。 -
DST_DIR=/my local path/我想安装的地方。 -
./bootstrap.sh --prefix=${DST_DIR} --includedir=${DST_DIR} --libdir={DST_DIR} --with-libraries=filesystem,system。 -
./b2 link=static --prefix=${DST_DIR} install --在DST_DIR中创建包含和库目录 - 在我的构建目录中,我在 CMakeLists.txt 所在的位置进行 CMake。
- CMake 命令运行良好。
- 但
make命令出错。
这是我的 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;
}
我得到的错误是:
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
我是 C++ 和 CMake 的新手。有什么愚蠢的,我在这里错过了吗?
我查看了How can I get CMake to find my alternative Boost installation? 和其他答案,但似乎不适用于我的情况。
【问题讨论】:
-
你使用的是什么版本的 CMake?
-
cmake 版本 3.16.3
标签: c++ boost cmake linker-errors