c++ - 无法加载共享对象 : undefined symbol

标签 c++ r boost cmake

我正在尝试使用静态链接 boost 为 R 创建简单的库。

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(TheRPath)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror")

set(Boost_USE_STATIC_LIBS ON)

find_package(Boost 1.50.0 REQUIRED COMPONENTS filesystem)

include_directories(${Boost_INCLUDE_DIRS})
include_directories("/usr/share/R/include")

set(SOURCE_FILES Path.cpp Path.h)

add_library(therpath SHARED ${SOURCE_FILES})

target_link_libraries(therpath ${Boost_LIBRARIES})

当我尝试加载共享对象时,解释器给我一个错误。

> dyn.load("libtherpath.so")
Error in dyn.load("libtherpath.so") : 
  unable to load shared object 'libtherpath.so':
  libtherpath.so: undefined symbol: _ZN5boost6system15system_categoryEv

我有以下代码:

路径.h

#ifndef PATH_H
#define PATH_H

#include <Rinternals.h>

extern "C" SEXP foo(SEXP snapshotPath);

#endif // PATH_H

路径.cpp

#include <string>
#include <boost/filesystem.hpp>
#include "Path.h"

extern "C" SEXP foo(SEXP snapshotPath) {
  std::string path(R_CHAR(STRING_ELT(snapshotPath, 0)));

  std::cerr << path << boost::filesystem::path::preferred_separator << "file.txt" << std::endl;

  return R_NilValue;
}

你能告诉我我做错了什么吗?

UPD 1

make VERBOSE=1 的输出:

/home/user/Soft/clion-latest/bin/cmake/bin/cmake -H/home/user/Workspace/TheRPath -B/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug --check-build-system CMakeFiles/Makefile.cmake 0
/home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_progress_start /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
make -f CMakeFiles/therpath.dir/build.make CMakeFiles/therpath.dir/depend
make[2]: Entering directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
cd /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug && /home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /home/user/Workspace/TheRPath /home/user/Workspace/TheRPath /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles/therpath.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
make -f CMakeFiles/therpath.dir/build.make CMakeFiles/therpath.dir/build
make[2]: Entering directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
[ 50%] Building CXX object CMakeFiles/therpath.dir/Path.cpp.o
/usr/bin/c++   -Dtherpath_EXPORTS -std=c++11 -Wall -Werror -g -fPIC -I/usr/share/R/include    -o CMakeFiles/therpath.dir/Path.cpp.o -c /home/user/Workspace/TheRPath/Path.cpp
[100%] Linking CXX shared library libtherpath.so
/home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/therpath.dir/link.txt --verbose=1
/usr/bin/c++  -fPIC  -std=c++11 -Wall -Werror -g  -shared -Wl,-soname,libtherpath.so -o libtherpath.so CMakeFiles/therpath.dir/Path.cpp.o /usr/lib/x86_64-linux-gnu/libboost_filesystem.a 
make[2]: Leaving directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
[100%] Built target therpath
make[1]: Leaving directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
/home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_progress_start /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles 0

UPD 2

/home/user/Soft/clion-latest/bin/cmake/bin/cmake -H/home/user/Workspace/TheRPath -B/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug --check-build-system CMakeFiles/Makefile.cmake 0
/home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_progress_start /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
make -f CMakeFiles/therpath.dir/build.make CMakeFiles/therpath.dir/depend
make[2]: Entering directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
cd /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug && /home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /home/user/Workspace/TheRPath /home/user/Workspace/TheRPath /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug /home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug/CMakeFiles/therpath.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
make -f CMakeFiles/therpath.dir/build.make CMakeFiles/therpath.dir/build
make[2]: Entering directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
[ 50%] Building CXX object CMakeFiles/therpath.dir/Path.cpp.o
/usr/bin/c++   -Dtherpath_EXPORTS -std=c++11 -Wall -Werror -g -fPIC -I/usr/share/R/include    -o CMakeFiles/therpath.dir/Path.cpp.o -c /home/user/Workspace/TheRPath/Path.cpp
[100%] Linking CXX shared library libtherpath.so
/home/user/Soft/clion-latest/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/therpath.dir/link.txt --verbose=1
/usr/bin/c++  -fPIC  -std=c++11 -Wall -Werror -g  -shared -Wl,-soname,libtherpath.so -o libtherpath.so CMakeFiles/therpath.dir/Path.cpp.o /usr/lib/x86_64-linux-gnu/libboost_system.a /usr/lib/x86_64-linux-gnu/libboost_filesystem.a 
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_system.a(error_code.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/lib/x86_64-linux-gnu/libboost_system.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [libtherpath.so] Error 1
make[2]: Leaving directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
make[1]: *** [CMakeFiles/therpath.dir/all] Error 2
make[1]: Leaving directory `/home/user/.CLion12/system/cmake/generated/a69e8583/a69e8583/Debug'
make: *** [all] Error 2

最佳答案

libboost_filesystem 依赖于 libboost_system。您需要将它添加到您的 CMakeLists.txt:

find_package(Boost REQUIRED COMPONENTS filesystem system)

请注意,我省略了可选的 Boost 版本号,因为您可能真的不需要或不想指定它。但如果你觉得有必要,你可以。

关于c++ - 无法加载共享对象 : undefined symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358415/

相关文章:

c++ - 如何将 vector<uint8_t> 转换为 unsigned char*

r - 如何避免 R 树形图上的文本以及组数限制

r - 在 R 中使用反斜杠转义反斜杠会在字符串中产生 2 个反斜杠,而不是 1

c++ - 为什么boost需要&vector[0]?

c++ - 如何将 time-uuid(存储在 boost uuid 中)转换为自纪元以来的时间戳/时间?

c++ - 在 C++ 中销毁动态分配的内存(数组对象)

c++ - 具有不同消息大小的多播性能

c++ - 在 C++ 中,如何找到直接或间接链接到数组特定元素的数组的所有元素?

包含分类数据和大量注释的反向 x 轴

c++ - boost::program_options - 是否可以强制执行强制标志?