[英]How do you link a C++ library from the command line
I am making C++ library that has multiple headers (all headers having ah, .cpp, and.o file), how do I make the library linkable so that the user can compile it like: g++ main.cpp -o binaries -l libraryName
?我正在制作具有多个标头的 C++ 库(所有标头都有 ah、.cpp 和 .o 文件),如何使库可链接以便用户可以像这样编译它:
g++ main.cpp -o binaries -l libraryName
?
how do I make the library linkable
如何使库可链接
Like so:像这样:
ar ruv libraryName.a foo.o bar.o baz.o
This is best achieved by writing a Makefile
, which will automate the build process for you.这最好通过编写
Makefile
来实现,它将为您自动执行构建过程。 Something like this:是这样的:
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}
PS To use libraryName.a
, do this: PS 要使用
libraryName.a
,请执行以下操作:
g++ -o exename main.cpp -lbraryName
and not this (requires library named liblibraryName
):而不是这个(需要名为
liblibraryName
库):
g++ -o exename main.cpp -l libraryName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.