Create and use a library

G

Gattaca

Hello,

1. How can I create a personnal library (with my own functions) ?
2. How can I use it ?
I know that in my source code I will have something like
#include "mylib.h"

But how to make a link between mylib.h and the library compiled ?

Thanks

Note that I read the FAQ first. I didn't find answer to my questions.
 
C

Case -

Gattaca said:
Hello,

1. How can I create a personnal library (with my own functions) ?
2. How can I use it ?
I know that in my source code I will have something like
#include "mylib.h"

But how to make a link between mylib.h and the library compiled ?

a. Place all functions in a file with name "mylib.c".
b. Place all function declarations and other stuff (types,
macro's, #defined constants, ...) in the "mylib.h".
c. As you say, #include "mylib.n" in every source that
uses one or more of these library functions.
d. Compile "mylib.c", "source.c", ...
e. Link the object files into a program.

Step d. and e. can be done with one action/command for example:

cc source.c mylib.c -o myprog

This will invoke the linker as well. When you compile "source.c"
the compiler sees there are external functions (declared in "mylib.h")
and marks calls to them as unresolved. The linker makes a list
of all these unresolved externals and tries to find a match in
all object files it currently is processing. When there's a
match it fill in the right 'address' for each external.

HTH
 
G

Gattaca

Gattaca a écrit :
Sorry but I assume I was not clear enought.
When I said I wanted to create a personnal library It was rather
-> How can I create a personnal compiled library (not a C file)?
In order to keep my source code secret and to do "a public interface"
with a "mylib.h" header file.

Thanks
 
A

Andrew

Gattaca a écrit :
Sorry but I assume I was not clear enought.
When I said I wanted to create a personnal library It was rather
-> How can I create a personnal compiled library (not a C file)?
In order to keep my source code secret and to do "a public interface"
with a "mylib.h" header file.

[snip]

You would be better off asking this question in a vendor-specific
group, as Standard C has no concept of a "personnal library".

To point you in the right direction, you need to check your
implementation's documentation on its library manager (for MSVC this
is "lib.exe", for GNU C this is "ar"). You use the library manager to
collect all the object files generated from your TU's into one big
file. To use it, you need to let your linker know that it can check
out said library for otherwise unresolved references. Again, this is
implementation specific, and is entirely off-topic here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top