Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How to link a library to library - follow up on another post.(linking libraries )
How to link a library to library - follow up on another post. [message #1820729] Tue, 28 January 2020 00:14 Go to next message
Helen Keller is currently offline Helen KellerFriend
Messages: 173
Registered: June 2019
Senior Member
Please note - I am NOT asking how to link C++ source to library.


I have a library (x.a) which need to reference another library.
It is standard type .a Linux library build using Eclipse and has common source includes built-in.
No problem adding specific "#include " source. It compiles OK.

After looking at GCC option I should be able to just add library file name.
I have added it as "miscellaneous" to the GCC.

The GCC "cannot find it..."
1. Does it have to be full library file name - such as "libX.a" or only X?
2. How / where do I add path to the additional library or do I have to ?


There are too many combinations of what syntax to use, unsuccessfully
tried them,
so I could use some assistance solving this.

AGAIN
Please note - I am NOT asking how to link source to library.
I need to link TWO libraries.

[Updated on: Tue, 28 January 2020 00:15]

Report message to a moderator

Re: How to link a library to library - follow up on another post. [message #1820730 is a reply to message #1820729] Tue, 28 January 2020 00:47 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
You really need to familiarize yourself with GNU documents.
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

-lXXX (small L) is shorthand for libXXX.so and libXXX.a
You can always place the fully named library on the command line instead.
If the library name doesn't follow the assumed naming convention then placing it fully named on the command line is required.

In Eclipse Managed Builds, this is done by adding it as an "Other object".
Understand that Eclipse generated makefiles are for simple builds.
The order of the command link command line is important (see below).
Eclipse doesn't easily allow for such fine control.

UPDATE:
You need a path on things in "Other objects" otherwise Make will only look in the current directory.

Quote:

-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, 'foo.o -lz bar.o' searches library 'z' after file foo.o but before bar.o. If bar.o refers to functions in 'z', those functions may not be loaded.

[Updated on: Tue, 28 January 2020 01:15]

Report message to a moderator

Re: How to link a library to library - follow up on another post. [message #1820772 is a reply to message #1820730] Tue, 28 January 2020 14:55 Go to previous messageGo to next message
Helen Keller is currently offline Helen KellerFriend
Messages: 173
Registered: June 2019
Senior Member
Additional notes :
The Eclipse build type .a library has NO properties -> linker hence there are no " IDE " -l/-L options - only "miscellaneous" options can be used.
The Eclipse build type .s(o) library HAS properties including linker.

The "link options" referenced clearly states that adding a file WITHOUT any specific "extensions" is interpreted as "link to .." option.
Perhaps that is why all the other instructions always state "add -lX" , not add -llibx.a.

Still missing is how to instruct the g++ ( "complier" - there is no linker ) WHERE to find the library being added TO LIBRARY.




Re: How to link a library to library - follow up on another post. [message #1820781 is a reply to message #1820772] Tue, 28 January 2020 17:52 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
You're going to have to be more specific about what you are trying to accomplish.

Are you trying to link an executable with multiple libraries?
Are you trying to merge libraries? If so, why?

Are you creating a library?
If so, then what kind is it: static (.a) or dynamic (.so assuming linux)?
(.a) libraries are built with (ar) https://sourceware.org/binutils/docs/binutils/ar.html

Here's a brief tutorial on creating both:
https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/GCC/create-libraries/index
Google variants of "creating static and dynamic libraries" for more.

The GNU linker's name is ld.
You can execute it directly but gcc and g++ will call it when necessary.
So you can pretend that g++ (or gcc) is synonymous with ld when linking.

A fundamental assumption built into Eclipse is that you understand how to use the tools it calls.
If you do, then setting up Eclipse to use them (setting the options) will become clear.


You really need to familiarize yourself with using GCC.
Using it, besides being off topic in this forum, is far too complicated to explain here.
Start here: https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html#Invoking-GCC
Actually, you should read the whole manual.
https://gcc.gnu.org/onlinedocs/gcc/index.html#SEC_Contents

Quote:
The "link options" referenced clearly states that adding a file WITHOUT any specific "extensions" is interpreted as "link to .." option.
Perhaps that is why all the other instructions always state "add -lX" , not add -llibx.a.


No. Reread the section I quoted again.

    -lXXX (small L) is shorthand for libXXX.so or libXXX.a
    The -l flag is necessary for this to occur
    It is the ONLY time when you can drop the extent.
    dynamic libs (.so) are given priority over static ones (.a) unless the -static option is used

[Updated on: Tue, 28 January 2020 18:48]

Report message to a moderator

Re: How to link a library to library - follow up on another post. [message #1826861 is a reply to message #1820730] Sun, 03 May 2020 00:37 Go to previous messageGo to next message
imb 45 is currently offline imb 45Friend
Messages: 6
Registered: May 2020
Junior Member
How to do the linking command in the right order ? ( lib file before object files) because when the library is added in "other pbject" the command line shows the library as the last element so error undefined reference is shown
please help !!!!
thank you
Re: How to link a library to library - follow up on another post. [message #1826866 is a reply to message #1826861] Sun, 03 May 2020 11:13 Go to previous message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
I'm only guessing at why you are having a problem.
If I'm guessing incorrectly, be more specific.
The linker only uses functions in a library when needed
so placing the libraries before the object files is pointless.

Assume lib X references a function in lib Y and
lib Y references a function in lib X.

If you are using Eclipse to generate the makefile,
you do this by listing libraries multiple times -- as many as needed
e.g., X, Y, X, Y
The order can be changed using the up and down icons in the dialog.
Works for both the libraries and miscellaneous dialogs.

You could also modify the command line pattern for the linker.

Here's the result of adding /usr/lib64/libc.a as a miscellaneous object twice
and library m twice in the libraries dialog
g++ -o "Hello" ./src/Hello.o /usr/lib64/libc.a /usr/lib64/libc.a -lm -lm

If you are writing your own makefile you can cause the linker to loop over a list
see options --start-group and --end-group
in https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html

Writing your own makefile is off-topic for this forum.

[Updated on: Sun, 03 May 2020 11:35]

Report message to a moderator

Previous Topic:Troubles setting up Eclipse/GCC for STM32
Next Topic:debugger not stopping at breakpoints
Goto Forum:
  


Current Time: Fri Apr 26 03:22:13 GMT 2024

Powered by FUDForum. Page generated in 0.03396 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top