C++ 将 .so 和 .a 库添加到 Makefile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12054858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add .so and .a libraries to Makefile
提问by rajat
I have a makefile which looks like this .
我有一个看起来像这样的 makefile。
DEFINES=-std=c++0x
INCS_GTK=-I/usr/include/gtk-2.0 -I/usr/include/glib-2.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gtk-2.0/gdk -I/usr/include/pango-1.0 -I/usr/lib/gtk-2.0/include -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include
INCS=-I/usr/include/freetype2 -I/usr/include/mysql -Iframeworks ${INCS_GTK}
LDLIBS=-lconfig++ -lcxcore -lcv -lGL -lGLU -lglut -lhighgui -lSDL -lftgl -lueye_api -lboost_filesystem -lboost_system -lann -lpthread -lflycapture -lglog -lmysqlpp -lmysqlclient -lunittest++
DEBUG=-g -pg
WARNINGS=-Wall -Wextra -pedantic -Wno-long-long #-O3 -Weffc++
BUILDDIR=build
BINDIR=dist
MAINCXX=${shell find -name '*.cxx'}
TARGETS=${MAINCXX:%.cxx=%}
CXXFLAGS=${DEBUG} ${WARNINGS} ${DEFINES} ${INCS}
LDFLAGS=${DEBUG} ${WARNINGS} ${DEFINES}
include IDEconfigs/Makefile/generic.mk
I want to add the following paths of static libraries to the makefile .
我想将静态库的以下路径添加到 makefile 。
/usr/local/lib/libYARP_OS.a /usr/local/lib/libYARP_sig.a /usr/local/lib/libYARP_math.a /usr/local/lib/libYARP_dev.a /usr/local/lib/libYARP_name.a /usr/local/lib/libYARP_init.a
how do i go about doing this .
我该怎么做呢。
回答by Swair
Lets consider your /usr/local/lib/libYARP_OS.a
.
让我们考虑一下您的/usr/local/lib/libYARP_OS.a
.
What you can do is, have -L/usr/local/lib/
in your makefile as one of the variables. And then you can have -lYARP_OS
appended to the LDLIBS.
您可以做的是,将-L/usr/local/lib/
您的 makefile 作为变量之一。然后你可以-lYARP_OS
附加到 LDLIBS。
-L is for path to the lib and -l is the lib name here libYARP_OS.a
will be passed as -lYARP_OS
.
-L 是 lib 的路径,-l 是这里的 lib 名称,libYARP_OS.a
将作为-lYARP_OS
.
On the command line you would do something like: gcc -o main main.c -L/usr/local/lib/ -lYARP_OS
. This should give you an idea.
在命令行中,你会做这样的事情:gcc -o main main.c -L/usr/local/lib/ -lYARP_OS
。这应该给你一个想法。
回答by Tim
You can either use an -L<path>
flag to tell GCC about the location of any library, and then include it with -l<libname>
. For example this would be
您可以使用-L<path>
标志告诉 GCC 任何库的位置,然后将其包含在-l<libname>
. 例如,这将是
$ gcc -o main main.c -L/usr/local/lib/ -lYARP_SO
as noted by swair.
正如斯维尔所指出的。
Alternatively, you can also supply the full path of the static library and compile directly, like
或者,您也可以提供静态库的完整路径并直接编译,例如
$ gcc -o main main.c /usr/local/lib/libYARP_OS.a
See 'Shared libraries and static libraries'for details.
有关详细信息,请参阅“共享库和静态库”。
In your specific case I would add them to the LDLIBS=
line.
在您的特定情况下,我会将它们添加到LDLIBS=
行中。
NB: Be careful about linking order, this is relevant when linking programs together. See 'Link order of libraries'for details. For example:
注意:注意链接顺序,这在将程序链接在一起时很重要。有关详细信息,请参阅“库的链接顺序”。例如:
$ gcc -Wall calc.c -lm -o calc (correct order)
works
作品
$ cc -Wall -lm calc.c -o calc (incorrect order)
main.o: In function `main':
main.o(.text+0xf): undefined reference to `sqrt'
Also see this similar question: How to link to a static library in C?
另请参阅这个类似的问题:How to link to a static library in C?
回答by Vincenzo Pii
Append -lYARP_OS -lYARP_sig -lYARP_math -lYARP_dev -lYARP_name -lYARP_init
to LDLIBS
.
附加-lYARP_OS -lYARP_sig -lYARP_math -lYARP_dev -lYARP_name -lYARP_init
到LDLIBS
.
Warning: the linking order may matter.
警告:链接顺序可能很重要。
Also, be sure that the linker knows that /usr/local/lib
is a place where to look for libraries, otherwise instruct it with -L/usr/local/lib
(you could add another makefile variable, e.g. LIBPATHS
or something similar, to contain the libraries paths).
此外,请确保链接器知道这/usr/local/lib
是一个查找库的地方,否则指示它-L/usr/local/lib
(您可以添加另一个 makefile 变量,例如LIBPATHS
或类似的东西,以包含库路径)。
As a general synopsis, if you have a library libMyLib.a
in folder /my/path
, the gcc
(or g++
) can be invoked with the following parameters:
作为一般概要,如果您libMyLib.a
在文件夹中有一个库/my/path
,则可以使用以下参数调用gcc
(或g++
):
gcc -L/my/path -lMyLib [...]
-L
is used to include paths where the linker will look for libraries-l
is used to link a library, which must be passed without thelib
prefix and the extension
-L
用于包含链接器将在其中查找库的路径-l
用于链接库,必须不带lib
前缀和扩展名传递
This question may be useful for a general understanding of libraries usage in C and C++: How to use Libraries
这个问题可能有助于大致了解 C 和 C++ 中的库用法:How to use Libraries
回答by pauljames840
In Makefile , add like this
在 Makefile 中,像这样添加
USER_LIBS = -lYARP_OS -lYARP_sig -lYARP_math -lYARP_dev -lYARP_name -lYARP_init
USER_LIBS = -lYARP_OS -lYARP_sig -lYARP_math -lYARP_dev -lYARP_name -lYARP_init
This will link the libraries you required
这将链接您需要的库