Add .so and .a libraries to Makefile

醉酒当歌 提交于 2019-12-02 19:31:56
Swair

Lets consider your /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 is for path to the lib and -l is the lib name here libYARP_OS.a will be passed as -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.

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

$ 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.

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?

Vincenzo Pii

Append -lYARP_OS -lYARP_sig -lYARP_math -lYARP_dev -lYARP_name -lYARP_init to 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).

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:

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 the lib prefix and the extension

This question may be useful for a general understanding of libraries usage in C and C++: How to use Libraries

In Makefile , add like this

USER_LIBS = -lYARP_OS -lYARP_sig -lYARP_math -lYARP_dev -lYARP_name -lYARP_init

This will link the libraries you required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!