简体   繁体   English

如何从命令行链接 C++ 库

[英]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.

相关问题 如何将C ++静态库链接到C程序? - How do you link a C++ static library to a C program? 如何从命令行运行Eclipse IDE for C / C ++(我已经安装了它) - How do you run Eclipse IDE for C/C++ from the command line (I already installed it) 如何在C ++程序中从文件名命令行参数确定完整路径? - How do you determine full paths from filename command line arguments in a c++ program? 您如何将用户编译的共享库的共享库链接到Debian上的C ++应用程序? - How do you link a shared library of a user-compiled shared library to a c++ application on Debian? 你能链接C应用程序的C ++库吗? - Can you link a C++ library from a C application? 如何使用命令行参数打开 C++ 中的文本文件,而不使用文件扩展名? - How do you open a text file in C++ using a command line argument, without using the file extension? 您可以在命令行上使用visual-c ++静态链接特定库吗? - Can you statically link a specific library using visual-c++ on the command line? 如何将C ++库与CGO和Swig链接? - How do I link a C++ library with CGO and Swig? 如何从MAC中的/ Library / Framework文件夹包含C ++文件 - How do you include files in C++ from the /Library/Framework folder in MAC 您如何为标准库容器定义 C++ 概念? - How do you define a C++ concept for the standard library containers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM