Like Tree1Likes
  • 1 Post By grumpy

easy way to link to all libraries in a directory?

This is a discussion on easy way to link to all libraries in a directory? within the C++ Programming forums, part of the General Programming Boards category; Instead if specifying each one with -l, is there an easy way for me to link to all libraries in ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    easy way to link to all libraries in a directory?

    Instead if specifying each one with -l, is there an easy way for me to link to all libraries in a specific directory? for example, all of the custom libraries for this API are put in a specific directory.. I need them all when linking to it..

    Right now, I'm just specifying each one individually, but that gets long...


    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    245
    Seems from googling, the only way is to specify all of them?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    *.a doesn't work?

    You don't need to use "-lfoo" to link libfoo.a, you can just say libfoo.a. Which means you can also say *.a

    Code:
    cc -o prog prog.o ../somepath/*.a
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    245
    Works great! didn't know you didn't need -lfoo Thank you!
    Last edited by dayalsoap; 06-16-2011 at 06:48 PM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It doesn't quite mean the same to some linkers, though. If you specify a lib file directly, it will just link it in completely. If you specify it with -l, it will just link in those parts you actually use.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you want, you can use a little bit of magic:
    Code:
    gcc *.c -o program $(ls lib*.a | sed 's/lib/-l/' | sed 's/\.a//')
    That assumes the libraries are in the current directory, if not:
    Code:
    gcc *.c -o program -L/path/to/libraries $(ls /path/to/libraries | grep '\.a$' | sed 's/lib/-l/' | sed 's/\.a//')
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Quote Originally Posted by CornedBee View Post
    It doesn't quite mean the same to some linkers, though. If you specify a lib file directly, it will just link it in completely. If you specify it with -l, it will just link in those parts you actually use.
    It also wouldn't be difficult, in a shell script, to parse a list of files and produce a command line with lib<name>.a converted into -l<name>.

    Linking against all libraries in a directory might get entertaining (a self-inflicted version of DLL hell) as some directories contain multiple versions of libraries, each version exporting functions (or other symbols) with the same name.

    Lists of libraries needed by a program don't normally change with the wind, so I would personally advocate editing a makefile with a specific list of needed libraries over some trick to link against all libraries in a directory.
    tabstop likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And don't forget that Unix linkers usually need libraries to be in dependency order on the command line, i.e. if library A depends on B, then -lA must be before -lB on the command line.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Instead of trying to be smart with wildcards and scripting, I'd rather write a pkg_config file. That way I can be explicit on the exact files and ordering and I can also distribute the file if I distribute the libraries.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to link libraries to MinGW?
    By ahernan17 in forum C Programming
    Replies: 10
    Last Post: 10-23-2010, 01:35 PM
  2. Linking to libraries in the current directory
    By Thiago in forum C Programming
    Replies: 7
    Last Post: 06-09-2010, 08:11 PM
  3. Removing link libraries in static libs
    By 39ster in forum C Programming
    Replies: 1
    Last Post: 06-10-2008, 11:22 AM
  4. Static link libraries on other computers
    By bennyandthejets in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2003, 06:01 PM
  5. Easy on - making a link
    By Hankyaku in forum C++ Programming
    Replies: 16
    Last Post: 09-27-2003, 03:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21