ld:找不到-lhello

时间:2018-09-26 14:55:50

标签: c++ ld

下面是我的问题的简化示例。 src/hello.cc中有一个C ++源文件,一个运行g ++来编译共享库的Makefile。 ld找不到文件,但文件确实存在。

src / hello.cc:

// 'Hello World!' program 

#include <iostream>

int hello()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Makefile:

build:
    cd src; rm -rf build && mkdir build; cd build; echo `pwd`; g++ -c -fPIC ../hello.cc; g++ hello.o -shared -o libhello.so.0.1; cd ../..
    mkdir -p lib
    cp src/build/libhello.so.0.1 lib
    ln -s lib/libhello.so.0.1 lib/libhello.so
clean:
    rm -rf src/build
    rm -rf lib

运行make后,ld无法找到生成的.so文件:

> make
cd src; rm -rf build && mkdir build; cd build; echo `pwd`; g++ -c -fPIC ../hello.cc; g++ hello.o -shared -o libhello.so.0.1; cd ../..
mkdir -p lib
cp src/build/libhello.so.0.1 lib
ln -s lib/libhello.so.0.1 lib/libhello.so

> ld -L`pwd`/lib -lhello --verbose
GNU ld (GNU Binutils for Ubuntu) 2.30
  Supported emulations:
   elf_x86_64
   elf32_x86_64
   elf_i386
   elf_iamcu
   i386linux
   elf_l1om
   elf_k1om
   i386pep
   i386pe
using internal linker script:
==================================================
/* Script for -z combreloc: combine and sort reloc sections */
...
==================================================
attempt to open /home/../lib/libhello.so failed
attempt to open /home/../lib/libhello.a failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libhello.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libhello.a failed
attempt to open //lib/x86_64-linux-gnu/libhello.so failed
attempt to open //lib/x86_64-linux-gnu/libhello.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libhello.so failed
attempt to open //usr/lib/x86_64-linux-gnu/libhello.a failed
attempt to open //usr/lib/x86_64-linux-gnu64/libhello.so failed
attempt to open //usr/lib/x86_64-linux-gnu64/libhello.a failed
attempt to open //usr/local/lib64/libhello.so failed
attempt to open //usr/local/lib64/libhello.a failed
attempt to open //lib64/libhello.so failed
attempt to open //lib64/libhello.a failed
attempt to open //usr/lib64/libhello.so failed
attempt to open //usr/lib64/libhello.a failed
attempt to open //usr/local/lib/libhello.so failed
attempt to open //usr/local/lib/libhello.a failed
attempt to open //lib/libhello.so failed
attempt to open //lib/libhello.a failed
attempt to open //usr/lib/libhello.so failed
attempt to open //usr/lib/libhello.a failed
attempt to open //usr/x86_64-linux-gnu/lib64/libhello.so failed
attempt to open //usr/x86_64-linux-gnu/lib64/libhello.a failed
attempt to open //usr/x86_64-linux-gnu/lib/libhello.so failed
attempt to open //usr/x86_64-linux-gnu/lib/libhello.a failed
ld: cannot find -lhello

但是文件确实存在:

> ls -l /home/../lib/libhello.so
lrwxrwxrwx 1 user user 19 Sep 26 16:17 /home/../lib/libhello.so -> lib/libhello.so.0.1

0 个答案:

没有答案