GCC共享库问题 [英] GCC Shared Library Problems

查看:158
本文介绍了GCC共享库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ubuntu上使用gcc创建一个共享库

我只有一个简单的类(shared.h和shared.cpp)和一个客户端使用它(main.cpp)< br>
这是我的makefile,我还是无法让程序编译。

I'm trying to create a shared library on ubuntu using gcc
I just have one simple class(shared.h and shared.cpp) and one client to use it (main.cpp)
This is my makefile and I'm still not able to to get the program to compile.

all:
    #compile object(fPIC: creates position independent code)
    gcc -fPIC -Wall -g -c shared.cpp

    #compile shared library
    gcc -shared -Wl,-soname,libshared.so.1 -o libshared.so.1.0.1 shared.o -lc

    #link shared library
    gcc -g -o main main.cpp -L. -lshared




  • 我相信第一行是正确的

  • 我不确定-lc是什么。我认为它传递的东西给链接器?

  • 我不想安装库,我只是想从当前目录中链接它。我试过: export LD_LIBRARY_PATH =。

    但它似乎没有什么区别。一切都在当前目录中。

    • I'm confident the first line is correct
    • I am unsure what "-lc" does. I think it passes something to the linker?
    • I don't want to install the library, I just want to be able to link it from the current directory. I have tried: export LD_LIBRARY_PATH=.
      but it does not seem to make a difference. Everything is in the current directory.


      错误:/ usr / bin / ld:找不到-lshared

      ERROR: /usr/bin/ld: cannot find -lshared


    • 如何让编译器检查我的库的当前目录?

      how do I get the compiler to check the current directory for my library?

      推荐答案

      问题不是它不在目录中,问题是你命名的库libshared.so.1.0.1。当您使用-lshared时,它会在库搜索路径中查找名为libshared.so或libshared.a的文件。

      The problem is not that it's not looking in the directory, the problem is that you've named the library "libshared.so.1.0.1". When you use -lshared, it's looking for a file named 'libshared.so' or 'libshared.a' in the library search path.

      大多数时候,使用版本化的系统库,即使您已经安装了libshared.so.1或libshared.so.1.0.1,也将提供指向libshared.so的最新版本的链接。

      Most of the time, when using versioned system libraries, you'll provide a link to the latest one as 'libshared.so', even if you have installed 'libshared.so.1' or 'libshared.so.1.0.1'.

      在您的情况下,如果您继续离开名为libshared.so.1.0.1的文件,您将需要创建2个符号链接:

      In your case, if you continue to leave the file named 'libshared.so.1.0.1', you'll want to create 2 symbolic links:


      1. libshared.so - 使用ld可以找到库

      2. libshared.so.1 - 您在构建时将SO名称声明为libshared.so.1,您需要提供此链接,否则可执行文件将无法在运行时找到正确的共享库。

      这篇关于GCC共享库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆