简体   繁体   中英

compiling c++ linking against static or dynamic library

When you compile a program using the following command, are you linking against a static or dynamic library?

g++ blink.cpp -o blink -lmraa

Secondly, this library was 'installed' from a PPA using these commands

sudo add-apt-repository ppa:mraa/mraa
sudo apt-get update
sudo apt-get install libmraa1 libmraa-dev mraa-tools python-mraa python3-mraa 

How can you tell if the library is static or dynamic?

When you compile a program using the following command, are you linking against a static or dynamic library?

See man ld :

-l namespec

--library=namespec

Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.

On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a ".so" extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.

Linux is an ELF system. So, the linker searches for .so first and then for .a .


How can you tell if the library is static or dynamic?

It can be both, bust most likely .so . You need to see what files comprise those packages.

You can also invoke ldd <executable> on the resulting executable or shared library and see what shared libraries it needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM