【问题标题】:Translate Makefile into CMakeLists; Cannot load shared library将 Makefile 翻译成 CMakeLists;无法加载共享库
【发布时间】:2018-04-09 14:10:26
【问题描述】:

我有自己的 C++ 库,我正在更改它的构建方式(从基本 Makefile 转换为 CMake)。

我还有一些使用这个库的 C++ 示例。

如果我使用 Makefile 构建库,示例运行得很好。 如果我使用 CMake 生成的 Makefile 构建,该库似乎构建得很好,但示例将失败并显示 error while loading shared libraries: libfpthread.so: cannot open shared object file: No such file or directory

使用 CMake 构建

$ cmake . -G 'Unix Makefiles'
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joaomlneto/Documents/GitHub/fpthreads

$ make
/usr/bin/cmake -H/home/joaomlneto/Documents/GitHub/fpthreads -B/home/joaomlneto/Documents/GitHub/fpthreads --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/joaomlneto/Documents/GitHub/fpthreads/CMakeFiles /home/joaomlneto/Documents/GitHub/fpthreads/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/joaomlneto/Documents/GitHub/fpthreads'
make -f CMakeFiles/fpthread.dir/build.make CMakeFiles/fpthread.dir/depend
make[2]: Entering directory '/home/joaomlneto/Documents/GitHub/fpthreads'
cd /home/joaomlneto/Documents/GitHub/fpthreads && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/joaomlneto/Documents/GitHub/fpthreads /home/joaomlneto/Documents/GitHub/fpthreads /home/joaomlneto/Documents/GitHub/fpthreads /home/joaomlneto/Documents/GitHub/fpthreads /home/joaomlneto/Documents/GitHub/fpthreads/CMakeFiles/fpthread.dir/DependInfo.cmake --color=
Scanning dependencies of target fpthread
make[2]: Leaving directory '/home/joaomlneto/Documents/GitHub/fpthreads'
make -f CMakeFiles/fpthread.dir/build.make CMakeFiles/fpthread.dir/build
make[2]: Entering directory '/home/joaomlneto/Documents/GitHub/fpthreads'
[ 11%] Building CXX object CMakeFiles/fpthread.dir/src/nvrecovery.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/nvrecovery.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/nvrecovery.cpp
[ 22%] Building CXX object CMakeFiles/fpthread.dir/src/xthread.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/xthread.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/xthread.cpp
[ 33%] Building CXX object CMakeFiles/fpthread.dir/src/libdthread.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/libdthread.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/libdthread.cpp
[ 44%] Building CXX object CMakeFiles/fpthread.dir/src/logger.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/logger.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/logger.cpp
[ 55%] Building CXX object CMakeFiles/fpthread.dir/src/real.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/real.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/real.cpp
[ 66%] Building CXX object CMakeFiles/fpthread.dir/src/xmemory.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/xmemory.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/xmemory.cpp
[ 77%] Building CXX object CMakeFiles/fpthread.dir/src/xrun.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/xrun.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/xrun.cpp
[ 88%] Building CXX object CMakeFiles/fpthread.dir/src/prof.cpp.o
/usr/bin/c++   -DDETERM_MEMORY_ALLOC -DENABLE_PROFILING -DLOCK_OWNERSHIP -DNDEBUG -DSSE_SUPPORT -Dfpthread_EXPORTS -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util  -fPIC  -g -O0 -msse2 -pthread -std=gnu++11 -o CMakeFiles/fpthread.dir/src/prof.cpp.o -c /home/joaomlneto/Documents/GitHub/fpthreads/src/prof.cpp
[100%] Linking CXX shared library libfpthread.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/fpthread.dir/link.txt --verbose=1
/usr/bin/c++  -fPIC   -shared -Wl,-soname,libfpthread.so -o libfpthread.so CMakeFiles/fpthread.dir/src/nvrecovery.cpp.o CMakeFiles/fpthread.dir/src/xthread.cpp.o CMakeFiles/fpthread.dir/src/libdthread.cpp.o CMakeFiles/fpthread.dir/src/logger.cpp.o CMakeFiles/fpthread.dir/src/real.cpp.o CMakeFiles/fpthread.dir/src/xmemory.cpp.o CMakeFiles/fpthread.dir/src/xrun.cpp.o CMakeFiles/fpthread.dir/src/prof.cpp.o -ldl -pthread 
make[2]: Leaving directory '/home/joaomlneto/Documents/GitHub/fpthreads'
[100%] Built target fpthread
make[1]: Leaving directory '/home/joaomlneto/Documents/GitHub/fpthreads'
/usr/bin/cmake -E cmake_progress_start /home/joaomlneto/Documents/GitHub/fpthreads/CMakeFiles 0

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(fpthread)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED on)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

file(GLOB src relative ${CMAKE_SOURCE_DIR} "src/*.cpp")
add_library(fpthread SHARED ${src})
target_include_directories(fpthread
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/heaplayers
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/heaplayers/util
)
target_link_libraries(fpthread
    Threads::Threads
    ${CMAKE_DL_LIBS}
)

add_definitions(-g)
add_definitions(-O0)
add_definitions(-DNDEBUG)
#add_definitions(-DDEBUG)
add_definitions(-msse2 -DSSE_SUPPORT)
#add_definitions(-DCHECK_SCHEDULE)
add_definitions(-DDETERM_MEMORY_ALLOC)
add_definitions(-DENABLE_PROFILING)
#add_definitions(-DGET_CHARACTERISTICS)
#add_definitions(-DLAZY_COMMIT)
add_definitions(-DLOCK_OWNERSHIP)
#add_definitions(-DPAGE_DENSITY)
#add_definitions(-DTRACING)
#add_definitions(-D'CUSTOM_PREFIX(x)=grace\#\#x')

set(CMAKE_VERBOSE_MAKEFILE on)

message("source files:")
message(${src})
message("")

使用 old Makefile 构建

$ make -f Makefile.old 
g++ -std=gnu++11 -shared -fPIC -g -DNDEBUG  -msse2 -DSSE_SUPPORT  -DDETERM_MEMORY_ALLOC  -DENABLE_PROFILING  -DLOCK_OWNERSHIP  -D'CUSTOM_PREFIX(x)=grace##x'  -m64 -I/home/joaomlneto/Documents/GitHub/fpthreads/include -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers -I/home/joaomlneto/Documents/GitHub/fpthreads/include/heaplayers/util /home/joaomlneto/Documents/GitHub/fpthreads/src/libdthread.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/xthread.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/xrun.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/real.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/prof.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/logger.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/xmemory.cpp /home/joaomlneto/Documents/GitHub/fpthreads/src/nvrecovery.cpp -o libfpthread.so -ldl -pthread

Makefile

CXX = g++ -std=gnu++11
SRCS=$(wildcard $(PWD)/src/*.cpp)
INCLUDE_DIRS = -I$(PWD)/include -I$(PWD)/include/heaplayers -I$(PWD)/include/heaplayers/util

TARGET_X64 = libfpthread.so
TARGET_X86 = libfpthread32.so
TARGETS = $(TARGET_X64) $(TARGET_X86)

CXXFLAGS  = -shared -fPIC -g
CXXFLAGS += -DNDEBUG # -DDEBUG/-DNDEBUG: enable/disable debugging
CXXFLAGS += -msse2 -DSSE_SUPPORT # Enable SS2 Support TODO automatic detection!
#CXXFLAGS += -DCHECK_SCHEDULE # Check deterministic schedule
CXXFLAGS += -DDETERM_MEMORY_ALLOC # TODO document
CXXFLAGS += -DENABLE_PROFILING # Enable execution profiling and print statistics at the end
#CXXFLAGS += -DGET_CHARACTERISTICS # Get some characteristics about running
#CXXFLAGS += -DLAZY_COMMIT # TODO document
CXXFLAGS += -DLOCK_OWNERSHIP # TODO document
#CXXFLAGS += -DPAGE_DENSITY # TODO document
#CXXFLAGS += -DTRACING # TODO document
CXXFLAGS += -D'CUSTOM_PREFIX(x)=grace\#\#x' # TODO document

CXXFLAGS32 = $(CXXFLAGS) -m32 -DX86_32BIT
CXXFLAGS64 = $(CXXFLAGS) -m64

LIBS = -ldl -pthread

.PHONY: default all x64 x86 clean

default: x64

all: x64 x86

x64: $(TARGET_X64)

x86: $(TARGET_X86)

$(TARGET_X64): $(SRCS) Makefile
    $(CXX) $(CXXFLAGS64) $(INCLUDE_DIRS) $(SRCS) -o $@ $(LIBS)

$(TARGET_X86): $(SRCS) Makefile
    $(CXX) $(CXXFLAGS32) $(INCLUDE_DIRS) $(SRCS) -o $@ $(LIBS)

clean:
    rm -f $(TARGETS)

比较生成的对象

尺寸

尺寸略有不同。

$ file fpthread_*
fpthread_cmake.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=69aaa475c964666aad9fb04f9cb278042ef0c367, not stripped
fpthread_make.so:  ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=71178ef39a4e22eb3f8e3cae9210033c18eae3d3, not stripped

$ ls ./fpthread_* -lA
-rwxrwxr-x 1 joaomlneto joaomlneto 1100184 Apr  9 14:39 ./fpthread_cmake.so
-rwxrwxr-x 1 joaomlneto joaomlneto 1100264 Apr  9 14:37 ./fpthread_make.so

ldd 输出

$ ldd fpthread_cmake.so 
    linux-vdso.so.1 =>  (0x00007ffe64f96000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb3604d0000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb36014e000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb35ff38000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb35fd1b000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb35f951000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fb36092e000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb35f648000)
$ ldd fpthread_make.so 
    linux-vdso.so.1 =>  (0x00007ffdd017a000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fcc32b6d000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcc32950000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fcc325ce000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fcc323b8000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcc31fee000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fcc32fcb000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcc31ce5000)

Makefile 示例

FPTHREAD_HOME=../../
CC = g++
CFLAGS = -g -O0
PLIB = -lpthread
FPLIB = $(FPTHREAD_HOME)/libfpthread.so

#nvthread
INC_DIR = $(FPTHREAD_HOME)/include
SRC_DIR = $(FPTHREAD_HOME)/src

FPINCLUDE_DIRS = -I$(INC_DIR)
FPSRCS = $(SRC_DIR)/fprecovery.cpp

TARGETS=read-my-writes

.PHONY: all clean
all: $(TARGETS)

read-my-writes:
    $(CC) $(CFLAGS) $(FPINCLUDE_DIRS) $(FPSRCS) read-my-writes.c -o $@ -rdynamic $(FPLIB) -ldl

clean:
    rm -f *.o $(TARGETS)

reset:
    rm -rf /mnt/ramdisk/fpthreads/*
    rm -f /tmp/fplib.crash

LD_DEBUG 输出

CMake

使用 CMake (cmake . -G 'Unix Makefiles'; make) 构建 libfpthread.so 后:

$ LD_DEBUG=all ./read-my-writes
     64187: 
     64187: file=libfpthread.so [0];  needed by ./read-my-writes [0]
     64187: find library=libfpthread.so [0]; searching
     64187:  search cache=/etc/ld.so.cache
     64187:  search path=/lib/x86_64-linux-gnu/tls/x86_64:/lib/x86_64-linux-gnu/tls:/lib/x86_64-linux-gnu/x86_64:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/tls/x86_64:/usr/lib/x86_64-linux-gnu/tls:/usr/lib/x86_64-linux-gnu/x86_64:/usr/lib/x86_64-linux-gnu:/lib/tls/x86_64:/lib/tls:/lib/x86_64:/lib:/usr/lib/tls/x86_64:/usr/lib/tls:/usr/lib/x86_64:/usr/lib        (system search path)
     64187:   trying file=/lib/x86_64-linux-gnu/tls/x86_64/libfpthread.so
     64187:   trying file=/lib/x86_64-linux-gnu/tls/libfpthread.so
     64187:   trying file=/lib/x86_64-linux-gnu/x86_64/libfpthread.so
     64187:   trying file=/lib/x86_64-linux-gnu/libfpthread.so
     64187:   trying file=/usr/lib/x86_64-linux-gnu/tls/x86_64/libfpthread.so
     64187:   trying file=/usr/lib/x86_64-linux-gnu/tls/libfpthread.so
     64187:   trying file=/usr/lib/x86_64-linux-gnu/x86_64/libfpthread.so
     64187:   trying file=/usr/lib/x86_64-linux-gnu/libfpthread.so
     64187:   trying file=/lib/tls/x86_64/libfpthread.so
     64187:   trying file=/lib/tls/libfpthread.so
     64187:   trying file=/lib/x86_64/libfpthread.so
     64187:   trying file=/lib/libfpthread.so
     64187:   trying file=/usr/lib/tls/x86_64/libfpthread.so
     64187:   trying file=/usr/lib/tls/libfpthread.so
     64187:   trying file=/usr/lib/x86_64/libfpthread.so
     64187:   trying file=/usr/lib/libfpthread.so
     64187: 
./read-my-writes: error while loading shared libraries: libfpthread.so: cannot open shared object file: No such file or directory

生成文件

在使用旧的 Makefile (make) 构建 libfpthread.so 之后:

$ LD_DEBUG=all ./read-my-writes
     63819: 
     63819: file=../..//libfpthread.so [0];  needed by ./read-my-writes [0]
     63819: file=../..//libfpthread.so [0];  generating link map
     63819:   dynamic: 0x00007f13c55f5b60  base: 0x00007f13c53b2000   size: 0x0000000000259c28
     63819:     entry: 0x00007f13c53cd2a0  phdr: 0x00007f13c53b2040  phnum:                  7
     63819: 
     63819: 
     63819: file=libstdc++.so.6 [0];  needed by ./read-my-writes [0]
     63819: find library=libstdc++.so.6 [0]; searching
     63819:  search cache=/etc/ld.so.cache
     63819:   trying file=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
     63819: 
     63819: file=libstdc++.so.6 [0];  generating link map
     63819:   dynamic: 0x00007f13c53aad50  base: 0x00007f13c5030000   size: 0x0000000000381400
     63819:     entry: 0x00007f13c50b9d20  phdr: 0x00007f13c5030040  phnum:                  8
     63819: 
     63819: 
     63819: file=libc.so.6 [0];  needed by ./read-my-writes [0]
     63819: find library=libc.so.6 [0]; searching
     63819:  search cache=/etc/ld.so.cache
     63819:   trying file=/lib/x86_64-linux-gnu/libc.so.6
     63819: 
     63819: file=libc.so.6 [0];  generating link map
     63819:   dynamic: 0x00007f13c5029ba0  base: 0x00007f13c4c66000   size: 0x00000000003c99a0
     63819:     entry: 0x00007f13c4c86950  phdr: 0x00007f13c4c66040  phnum:                 10
     63819: 
     63819: 
     63819: file=libdl.so.2 [0];  needed by ../..//libfpthread.so [0]
     63819: find library=libdl.so.2 [0]; searching
     63819:  search cache=/etc/ld.so.cache
     63819:   trying file=/lib/x86_64-linux-gnu/libdl.so.2
     63819: 
     63819: file=libdl.so.2 [0];  generating link map
     63819:   dynamic: 0x00007f13c4c64d88  base: 0x00007f13c4a62000   size: 0x00000000002030f0
     63819:     entry: 0x00007f13c4a62da0  phdr: 0x00007f13c4a62040  phnum:                  7
     63819: 
     63819: 
     63819: file=libgcc_s.so.1 [0];  needed by ../..//libfpthread.so [0]
     63819: find library=libgcc_s.so.1 [0]; searching
     63819:  search cache=/etc/ld.so.cache
     63819:   trying file=/lib/x86_64-linux-gnu/libgcc_s.so.1
     63819: 
     63819: file=libgcc_s.so.1 [0];  generating link map
     63819:   dynamic: 0x00007f13c4a612b0  base: 0x00007f13c484c000   size: 0x0000000000215910
     63819:     entry: 0x00007f13c484ea70  phdr: 0x00007f13c484c040  phnum:                  6
     63819: 
     63819: 
     63819: file=libpthread.so.0 [0];  needed by ../..//libfpthread.so [0]
     63819: find library=libpthread.so.0 [0]; searching
     63819:  search cache=/etc/ld.so.cache
     63819:   trying file=/lib/x86_64-linux-gnu/libpthread.so.0
     63819: 
     63819: file=libpthread.so.0 [0];  generating link map
     63819:   dynamic: 0x00007f13c4846d50  base: 0x00007f13c462f000   size: 0x000000000021c428
     63819:     entry: 0x00007f13c46350b0  phdr: 0x00007f13c462f040  phnum:                  9
     63819: 
     63819: 
     63819: file=libm.so.6 [0];  needed by /usr/lib/x86_64-linux-gnu/libstdc++.so.6 [0]
     63819: find library=libm.so.6 [0]; searching
     63819:  search cache=/etc/ld.so.cache
     63819:   trying file=/lib/x86_64-linux-gnu/libm.so.6
     63819: 
     63819: file=libm.so.6 [0];  generating link map
     63819:   dynamic: 0x00007f13c462dd88  base: 0x00007f13c4326000   size: 0x00000000003080f8
     63819:     entry: 0x00007f13c432b600  phdr: 0x00007f13c4326040  phnum:                  7
     63819: 
...

请注意:

  • 这是我第一次使用 CMake,而且我对 Makefile 了解不多。即使与问题本身没有直接关系,任何形式的建设性 cmets 都会受到赞赏:-)
  • 对不起这个糟糕的标题;不知道该怎么称呼它!

【问题讨论】:

  • 您显示了创建fpthread 库的代码,但错误消息是关于与其链接的。如何创建fpthread_cmakefpthread_make 库?
  • 我看到的一个区别是您没有使用 -fPIC 进行编译。
  • 在将LD_DEBUG 环境变量设置为all 后,能否再次尝试运行示例? (见:man7.org/linux/man-pages/man8/ld.so.8.html#ENVIRONMENT)?也许您会从输出中获得一些见解?
  • 另外,我可以在您的问题中看到三个文件名用于同一个库 (fpthread{,_cmake,_make}.so)。我假设您在准备帖子时为清楚起见手动重命名了它们,但是如果那里有文件名混淆,也许值得仔细检查?
  • 请删除add_definitions(-lpthread) 行。 pthread 库已添加到target_link_libraries(fpthread Threads::Threads) 调用中。

标签: c++ makefile cmake shared-libraries


【解决方案1】:

我想我了解您的问题,并且有一个潜在的解决方案。尝试将此添加到您的 CMakeLists.txt:

set_property(TARGET fpthread PROPERTY NO_SONAME ON)

然后重建libfpthread.so

出现这种情况的原因是:

当可执行文件与具有 DT_SONAME 字段的共享对象链接时,当可执行文件运行时,动态链接器将尝试加载由 DT_SONAME 字段指定的共享对象,而不是使用链接器提供的文件名.

摘自:https://linux.die.net/man/1/x86_64-linux-gnu-ld.bfd,在-soname 选项文档下。

另见:https://cmake.org/cmake/help/latest/prop_tgt/NO_SONAME.html

【讨论】:

  • 完全正确 - 删除 DT_SONAME 字段可以解决问题。我猜另一种选择是以不同的更标准的方式包含库?只是想知道你会推荐什么:-)
  • Nice :) 如果可能的话,我建议将库和示例集成到单个 CMake 构建树中并使用 target_link_libraries 以便 CMake 可以查看示例 -> 库依赖项并做正确的事事物。只要您首先为库目录调用add_subdirectory,然后从顶级CMakeLists.txt 调用示例目录,就可以保留单独的目录。祝你好运!
  • @joaomlneto:我不太了解您:在your comment 中,您说您使用dlopen运行时 加载库。这个答案意味着您与图书馆链接。什么是真理?
猜你喜欢
  • 2014-10-26
  • 2014-11-06
  • 2021-11-04
  • 2021-10-12
  • 2011-01-24
  • 2020-12-17
  • 2013-11-22
  • 2011-11-13
相关资源
最近更新 更多