Creating a C library that can be imports as "#include<myLibrary.h>"

342 Views Asked by At

I want to create a C static, so the user can use it as follows.

#include<myLibrary.h>

How can I achieve this ?

2

There are 2 best solutions below

1
On BEST ANSWER

You first need to decide if you library would be a Dynamic Linked Library (Dll) or a Static Linked Library.

if the first (dll), read through Building an Import Library and Export File on how to build import library and export file for include.

if the second (static linked library), you simply provide your .h and .cpp files as part of the package so the users will simply add your files to their project.

0
On

For a dynamic library : ou can do that by create a .so :

cc [files.c] -o lib.so -fPIC -shared

And then, you have to compile with lib.so


For a static library :

gcc -c [files.c]
ar r lib.a [files.o]

And then compile with lib.a

(Work for Linux)