在库数较多的情况下,GCC -l选项跨越指定的库。

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

也许我遗漏了什么,但是当我用共享库*.so编译我的程序时,我总是要为每一个正在使用的库使用-llib。

现在我有一个项目,链接了40个库(可能),鉴于这个库的 "框架 "是相当大的,要找到每个库的引用是不容易的。

我是否真的要知道在哪个*.so文件里有我要找的东西,还是有一个自动的更快的方法,比如用-L表示完整的路径,然后告诉编译器在这里搜索?

"ldd "命令可能会有帮助,如果我有一个类似的应用程序使用相同的库,但我仍然需要在gccmakefile中至少添加一次。

这是唯一的方法吗,还是有更快更简单的方法?我知道我可能有2个*.so文件有相同的函数名,但这时我会很高兴收到一个 "redefined "的错误。

此外,我如何找出一个*.so或*.dll(甚至是*.a静态lib)文件中可能包含的可用引用?

谢谢。

gcc linker shared-libraries
1个回答
0
投票

我是否真的需要知道在哪个*.so中使用?

是的,这是唯一的办法吗?

这是唯一的方法吗?

嗯,不,你可以自己写解决方案。

还是有更快更简单的方法?

我不相信,即使有,我也不相信会实用。

我怎样才能知道什么是

例如:

$ gcc /tmp/1.c 
/usr/bin/ld: /tmp/cc2lZAPF.o: in function `main':
1.c:(.text+0x19): undefined reference to `pthread_create'

Och no! 我们又一次忘记了与 -pthread. 但不用担心,只是为了好玩,我创造了以下的内容。,ld_find_symbol 脚本。

#!/bin/bash
# SPDX-License-Identifier: MIT AND Beerware

name=$(basename "$0")

usage() {
    cat <<EOF
Usage: $name <symbol>...

Options:
  -d <database>
  -h

Finds the symbol in libraries.

Written by Kamil Cukrowski
Licensed jointly under Beerware License and MIT License.
EOF
}

args=$(getopt -n "$name" -o d:h -- "$@")
eval set -- "$args"
database=/tmp/database.txt
while (($#)); do
    case "$1" in
    -d) database=$2; shift; ;;
    -h) usage; exit; ;;
    --) shift; break; ;;
    esac
    shift
done
if ((!$#)); then usage; exit 1; fi;

if [[ -e "$database" ]]; then
    echo "Using $database"
else
    echo "Creating $database"

    # list all ld search paths
    ld --verbose | grep SEARCH_DIR | tr -s ' ;' '\n' |
    # In a format something we can eat
    sed 's/SEARCH_DIR("\(.*\)")/\1/' |
    # Resolve symlinks, remove duplicates
    # Remove non-existent directories
    xargs -d'\n' -n1 sh -c '
        set -- "$(readlink -f "$1")"
        if [ -e "$1" ]; then
            printf "%s\n" "$1"
        fi
        :
    ' _ |
    sort -u |
    # List all .so and .a files from that dir
    # Calling `file` would be very slow
    xargs -d'\n' -I{} find {} -mindepth 1 -maxdepth 1 -type f \
        '(' -name 'lib*.so.*' -o -name 'lib*.a' ')' |
    # list filename and symbol in each library
    (
        # we list all symbols listed in any elf section
        list_symbols() {
            readelf -Ws "$1" |
            sed '/^ *[^ ]*: *[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* * [0-9]\{1,\} *\([^ ]\{1,\}\)$/!d; s``\1\t'"$1"'`' ||: |
            sort -u
            # log the filename that we are ready
            echo "Indexed: $1" >&3
        }
        export -f list_symbols
        # extra stdbuf -oL so that buffering can't touch this
        # I guess we should be safe till _POSIX_PIPE_BUF
        stdbuf -oL xargs -P$(nproc) -n1 \
            stdbuf -oL bash -c 'list_symbols "$@"' _ 
    ) |
    # sort it
    sort -t $'\t' -u -o "$database"

fi 3>&1

for i in "$@"; do
    look -t $'\t' "$i" "$database" |
    grep -F "$i"$'\t'
done

所以我们可以运行。

$ ,ld_find_symbol pthread_create
...
pthread_create  /usr/lib/libasan.so.5.0.0
pthread_create  /usr/lib/liblsan.so.0.0.0
pthread_create  /usr/lib/libpthread.a
pthread_create  /usr/lib/libtsan.so.0.0.0

Och symbol found. 所以现在我们可以。

$ gcc /tmp/1.c -l:libpthread.a
... errors ....
/usr/bin/ld: (.text+0x202e): undefined reference to `_dl_stack_flags

所以我们可以再一次:

$ ,ld_find_symbol _dl_pagesize
/usr/lib/libc.a

然后修复它

$ gcc /tmp/1.c -l:libpthread.a -l:libc.a

也许经过一些工作,可以用脚本自动神奇地工作,自动检测丢失的符号。不过,这还有很多工作要做。

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