PDA

View Full Version : Possible to Import Portions of Libraries in C?



StunnerAlpha
June 6th, 2009, 01:49 AM
Ey guys, I was wondering if it is at all possible to import a portion of <stdlib> for the C language(not C++)? I need to be able to use malloc but when I #include that library I get errors and warnings about redundancies(I am modifying a FreeBSD kernel). Thanks.

Volt9000
June 6th, 2009, 04:39 PM
AFAIK you can't import part of a library in C. The only way to do what you're asking would be to compile a separate version of said library with only the functions you need.

What kind of errors and warnings are you getting?

nvteighen
June 6th, 2009, 04:45 PM
Ey guys, I was wondering if it is at all possible to import a portion of <stdlib> for the C language(not C++)? I need to be able to use malloc but when I #include that library I get errors and warnings about redundancies(I am modifying a FreeBSD kernel). Thanks.
Er... try using #include <malloc.h>. You should have it available.

That doesn't mean you will be "importing" a portion of the library. The C Standard Library is a block you'll be linking to completely, even though you can make your program aware just of the needed parts by using specific header files.

StunnerAlpha
June 6th, 2009, 05:11 PM
Er... try using #include <malloc.h>. You should have it available.

That doesn't mean you will be "importing" a portion of the library. The C Standard Library is a block you'll be linking to completely, even though you can make your program aware just of the needed parts by using specific header files.
Yeah that ended up working, thanks!

leblancmeneses
June 6th, 2009, 07:51 PM
yes it is called static linking.

libraries ending in .a copies code linked during the linking process only.

nvteighen
June 6th, 2009, 08:45 PM
yes it is called static linking.

libraries ending in .a copies code linked during the linking process only.
No, static linking puts the whole library into your executable.

When you have a compiled library, either static or dynamic, you just can't cut it down at your will without recompiling. There are several reasons why you can't. For example: a library has always an index of linkable symbols... trying to cut a portion would imply to modify its index.

No: what you do is to cut the header file to show the compiler only those functions relevant for your program. Then your program will be linked to the whole standard library. Yes, in theory, will have access to all of its functions, but you don't care.