Danijel
Danijel

Reputation: 8620

Eclipse CDT MinGW toolchain: how to link with a library that does not begin with lib prefix?

I have problems with linking to a library called xyz.a in my Eclipse CDT MinGW project. Eclipse projects cannot find this library unless I rename it to libxyz.a.

I have added:

Some info I found online:

How do I specify the libraries to be searched by the linker?

    MinGW supports libraries named according to the "<name>.lib" and "<name>.dll" conventions, in addition to the normal "lib<name>.a" convention common on *nix systems. To include libraries named according to any of these conventions, simply add an associated "-l<name>" specification to the compiler command, ensuring it is placed after the name of the module in which the reference appears.
    Note that, if the library is not found in any of the default library search paths, you may also need to insert an appropriate "-L<dir>" switch to specify its location; (it is recommended that you place the "-L<dir>" switch before the "-l<name>" specification which requires it).
    Also note that the library names "lib<name>.a" and "lib<name>.lib" are not equivalent; if you have a library named according to the aberrant "lib<name>.lib" convention, it will not be found by an "-l<name>" specification -- if you cannot rename the library, you must use the form "-llib<name>" instead.

Upvotes: 1

Views: 1371

Answers (2)

Mike Kinghan
Mike Kinghan

Reputation: 61610

You can link a library called oddname - where oddname includes any file-extension - in Eclipse CDT like this:

  • Navigate in the project Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries.

  • In Libraries(-l) panel add :oddname

  • If necessary, in the Library search path(-L) panel add the path to oddname

  • OK out

This setting will add the option -l:oddname to the generated GCC link command. -l:filename is the form of the -l option signifying that the conventional lib prefix and the {.so|.a} extension are not implied and that filename is the actual filename of the library to be linked. Here is the documentation

Upvotes: 4

Jonah Graham
Jonah Graham

Reputation: 7980

The simplest option is to just add the library to the command line of GCC. To do this with CDT add the library (whatever name it is) to the Other object (under Project settings -> C/C++ Build -> GCC C Linker -> Miscellaneous)

Here is a screenshot where I added a library with the file name badname.a to the command line of GCC.

enter image description here

When the build runs, this is the resultant command line:

Invoking: GCC C Linker
gcc  -o "SO"  ./src/SO.o  /home/me/workspace/SO/badname.a 

Note: the disadvantage of the above solution is the whole library is included, not just the objects within it that are referenced. You can alternatively add any linker flags you want by adding them to the Linker flags box in the same dialog.

Upvotes: 1

Related Questions