Thread: Static Library

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Static Library

    I'm creating a personal C++ framework of sorts, just an interface to common stuff I use between projects. It's a static library. It makes use of a fair bit of STL and Boost. In order to use it I have to statically link to a couple Boost static libraries (.lib's).

    Isn't there any way I can have my framework include the Boost dependencies (ie. compile the Boost libraries into my library)? So it doesn't require additional linking for other projects.

    Thanks in advance
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    static linking = compile the libraries into your program/library. So either you're already doing what you want to be doing, or you're not telling us something.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah... That's what I thought.

    I've got it as a static link project in Code::Blocks under Build Options -> Linker Settings -> Link -> "..\boost\libboost_regex-mgw34-mt-d-1_37.lib" etc.

    It compiles Framework.lib, which I link to in a new project, but it throws the standard "undefined reference" to boost::basic_regex if I don't link to those libs in the new project as well.

    :-/
    Last edited by Dae; 03-10-2009 at 11:05 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Without looking at your code, static linking should link in the functions that are used in your framework code. But your framework library doesn't have a function called boost::basic_regex in it (you included the binary for it with the linking, but that doesn't get added to the table of contents of the library). If you call that function from your main program, then you'll need the boost library as well. OTOH, if you only call your framework functions then you shouldn't get any errors.

    (I.e., just because you write a program that uses printf doesn't mean you can link some other program that needs printf to it instead of to the C runtime library.)

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Okay, then the question would be: is it possible to get the framework dependencies (boost static libraries) added to the table of contents of the framework library. Compile multiple library files into one.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I am not as wise in the ways of linkers as I should be, but I do not believe that the functions are necessarily included in a way that they can even be called separately. (They probably are, but I wouldn't be able to guarantee it.)

    If for whatever reason you want to only link your library in, then all the functionality needs to be in your library. If you don't want to use boost's library, but you want to call this regex function, then you're not done writing your library yet until you have a function that does what you want. (It may not do much more than call boost's function, but if you don't want the library to be linked in you can't leave it to boost to do.)

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well, that's gonna be a hassle. Thanks tabstop.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can, if you want to, create a new library from several libraries. There are some interesting challenges in doing so, and I would suggest that you don't, but it is technically possible. The process goes something like this:
    - Extract library 1 and library 2 into current directory.
    - Buld a new library from the extracted files.

    Remember, a static library is simply a collection of object files (a bit like a zip archive, but with no compression), so the content of it is just like the object files you get when you compile your application code.

    One of the problems with splitting and merging libraries is that you need to be careful about dependancies and the order that the library content is introduced into the final application. If library A uses library B functions, then it the order must be libA then libB, as the linker will only search each library once. This means that if libB is linked before libA, the functions in libA that uses libB will be "not found".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    With Visual Studio's LIB.exe it's easy - just add the libs you want to include on the command line when LIB is called.

    gg

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dae View Post
    Okay, then the question would be: is it possible to get the framework dependencies (boost static libraries) added to the table of contents of the framework library. Compile multiple library files into one.
    Yes, you can merge multiple static libraries into a single library. Just link them together to produce a new .lib file (on Windows), or explode them then re-archive (on Linux)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a static library from C++ and C code
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2009, 07:28 PM
  2. Using static libraries
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 02-29-2008, 11:40 PM
  3. Replies: 4
    Last Post: 07-06-2006, 02:53 AM
  4. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM