【问题标题】:How do you link a C++ library from the command line如何从命令行链接 C++ 库
【发布时间】:2020-05-16 13:48:25
【问题描述】:

我正在制作具有多个头文件的 C++ 库(所有头文件都有一个 .h、.cpp 和 .o 文件),我如何使库可链接,以便用户可以像这样编译它:g++ main.cpp -o binaries -l libraryName?

【问题讨论】:

  • 我发现最好的方法是花几个月时间学习 GNU 工具链:如何使用 autoconf、automake 和 libtool。 libtool 将负责此功能,但为了有效地使用它,您还需要 autoconf 和 automake。您始终可以使用适当的选项运行 ld 并自己构建共享库,但是 libtool 会处理许多其他细节,这会让您大吃一惊,直到您做对为止。了解如何使用 libtool。祝你好运。

标签: c++ command-line compilation linker


【解决方案1】:

如何使库可链接

像这样:

ar ruv libraryName.a foo.o bar.o baz.o

这最好通过编写Makefile 来实现,它将为您自动执行构建过程。像这样的:

all: libraryName.a
clean:
        rm -f *.o  # important: use TAB, not spaces for indentation here.

SRCS = foo.cpp bar.cpp baz.cpp
OBJS = ${SRCS:.cpp=.o}

libraryName.a: ${OBJS}

附:要使用libraryName.a,请执行以下操作:

g++ -o exename main.cpp -lbraryName

而不是这个(需要名为 liblibraryName 的库):

g++ -o exename main.cpp -l libraryName

【讨论】:

  • 感谢@EmployedRussian。 “ruv”中的“u”是什么意思?因为当我尝试时它说:'u' modifier ignored since `D' is the default (see `U')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
相关资源
最近更新 更多