lib c++

* static libary
** Generate library
*** compile obj
cc -Wall -c ctest1.c ctest2.c 
*** generate lib
 ar -cvq libctest.a ctest1.o ctest2.o
** Use library
*** link with library
cc -o fpxOut1 prog.c libctest.a
cc -o fpxOut2  prog.c -L/path/to/library-directory -lctest
**** Example files:
ctest1.c
void ctest1(int *i)
{
   *i=5;
}

            
ctest2.c
void ctest2(int *i)
{
   *i=100;
}
            
prog.c
#include <stdio.h>
void ctest1(int *);
void ctest2(int *);

int main()
{
   int x;
   ctest1(&x);
   printf("Valx=%d\n",x);

   return 0;
}

* dynamic share library

** create so


gcc -Wall -fPIC -c *.c
gcc -shared -W  -o libctest.so.1.0   *.o
** install so
mv libctest.so.1.0 /opt/lib
    ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
    ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so

** set enviroment in ~/.bashrc
export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH 

** use the so
*** compile
gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog
Compiling for run-time linking with a dynamically linked libctest.so.1.0:
gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog

Use:
    gcc -Wall -L/opt/lib prog.c -lctest -o prog
*** run
./prog
          
 

猜你喜欢

转载自blog.csdn.net/liufengl138/article/details/88945342
lib
今日推荐