你如何从命令行 link C++ 库
How do you link a C++ library from the command line
我正在制作具有多个头文件的 C++ 库(所有头文件都有一个 .h、.cpp 和 .o 文件),如何使该库可链接以便用户可以像这样编译它:g++ main.cpp -o binaries -l libraryName
?
how do I make the library linkable
像这样:
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}
P.S。要使用 libraryName.a
,请执行以下操作:
g++ -o exename main.cpp -lbraryName
而不是这个(需要名为 liblibraryName
的库):
g++ -o exename main.cpp -l libraryName
我正在制作具有多个头文件的 C++ 库(所有头文件都有一个 .h、.cpp 和 .o 文件),如何使该库可链接以便用户可以像这样编译它:g++ main.cpp -o binaries -l libraryName
?
how do I make the library linkable
像这样:
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}
P.S。要使用 libraryName.a
,请执行以下操作:
g++ -o exename main.cpp -lbraryName
而不是这个(需要名为 liblibraryName
的库):
g++ -o exename main.cpp -l libraryName