链接器未找到 libsystemd

链接器未找到 libsystemd

我正在尝试编写一个systemd使用看门狗的服务,之后

sudo apt install libsystemd-dev

我检查了一下以确保它可用:

$ ldconfig -p |grep systemd
    libsystemd.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libsystemd.so.0
    libsystemd.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libsystemd.so
    libnss_systemd.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libnss_systemd.so.2

然后检查以确保链接器可以找到它:

$ echo "int main(){}" | gcc -x c++ -Wl,--no-as-needed -llibsystemd - && ldd a.out | grep libsystemd
/usr/bin/ld: cannot find -llibsystemd
collect2: error: ld returned 1 exit status

为何找不到ld

答案1

-llibsystemd不寻找libsystemd- 它寻找liblibsystemd

正如所指出的man g++

  -l library
       The linker searches a standard list of directories for the library,
       which is actually a file named liblibrary.a.  The linker then uses
       this file as if it had been specified precisely by name.

所以你要-lsystemd

echo "int main(){}" | gcc -x c++ -Wl,--no-as-needed -lsystemd - && ldd a.out | grep libsystemd

相关内容