【问题标题】:C : How to link all o file into one fileC:如何将所有o文件链接到一个文件中
【发布时间】:2014-04-11 09:50:23
【问题描述】:

我尝试将 C 库集成到我的项目中。该项目的自定义 makefile 尝试将所有源文件编译成一个目标文件 (*.o)。

我需要将所有这些*.o 文件链接到一个文件中,例如so 文件,以便于使用它。我需要这个吗?如果是,我如何将所有*.o 文件链接到一个库文件中。我将在 Makefile 中添加哪些行?

(注意,这个C库包含很多子目录,一个大的makefile会进入所有子目录,每个目录都使用Makefile来运行)

谢谢:)

【问题讨论】:

  • example,搜索“链接共享库”文本

标签: c gcc linker makefile


【解决方案1】:

看到这是最好的document

让我们看看我有 2 个 .o 文件。 ctest1.o ctest2.o 所以我将按如下方式制作静态库。

ar -cvq libctest.a ctest1.o ctest2.o

对于动态 .so 文件

gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o
mv libctest.so.1.0 /opt/lib
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so

这将创建库 libctest.so.1.0 和指向它的符号链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2016-09-28
    • 2020-06-28
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多