与Boost Filesystem的静态链接不起作用

问题描述 投票:1回答:1

[我正在尝试从源代码构建增强功能,并且在考虑将此静态链接的增强功能项目移至AWS Lambda服务器时,我需要静态链接。

我已完成以下步骤:

  1. 我在第三方目录中下载了boost_1_72_0.tar.gz和tar xzvf boost_1_72_0.tar.gz
  2. boost_1_72_0内部的[cd
  3. DST_DIR=/my local path/我要安装的位置。
  4. ./bootstrap.sh --prefix=${DST_DIR} --includedir=${DST_DIR} --libdir={DST_DIR} --with-libraries=filesystem,system
  5. ./b2 link=static --prefix=${DST_DIR} install --DST_DIR内创建包含和lib目录>
  6. 在我的构建目录中,我正在执行CMake,其中存在我的CMakeLists.txt。
  7. CMake命令工作正常。
  8. 但是make命令给出了错误。
  9. 这是我的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?和其他答案,但对于我而言似乎不起作用。

我正在尝试从源代码构建增强功能,并且在考虑将此静态链接的增强功能项目移至AWS Lambda服务器时,我需要静态链接。我已经完成以下步骤:我下载了...

c++ boost cmake linker-errors
1个回答
2
投票

您似乎对Boost使用了旧的CMake变量。从Boost 1.70开始,Boost现在提供CMake支持,使CMake可以更轻松地为您的项目查找和使用Boost(请参阅this页上的文档)。

© www.soinside.com 2019 - 2024. All rights reserved.