`

How to build static library and how to use it in Linux

    博客分类:
  • C
阅读更多

1. declare the interface of libray:
  max.h
  ------------------------------
  int max(int,int);

2. implement the interface:
  max.c
  ------------------------------
  #include "max.h"
  int max(int a, int b)
  {
    return a > b ? a : b;
  }

3. code the invoker:
  main.c
  ------------------------------
  #include <stdio.h>
  #include "max.h"
  int main(int argc, const char *const *const argv)
  {
    printf("max(3,5)=%d\n", max(3,5));
  }

4. build script:
  main.sh
  ------------------------------
  #!/bin/bash
  gcc -Wall -g -c -o max.o max.c
  ar rcs libmax.a max.o
  gcc -Wall -g -c -o main.o main.c
  gcc -o main main.o -L. -lmax
  ./main

 

5. build shared library script:

------------------------------

#!/bin/bash
gcc -fPIC -Wall -g -c max.c
gcc -g -shared -Wl,-soname,libmax.so.0 -o libmax.so.0.0 max.o
/sbin/ldconfig -n .   <---- generate link libmax.so.0 --> libmax.so.0.0
ln -sf libmax.so.0 libmax.so
gcc -Wall -g -c main.c -o main.o
gcc -g -o main_shared main.o -L. -lmax
LD_LIBRARY_PATH="." ./main_shared

 

6. asranmysql Makefile:

  1 libasranmysql.so: asranmysql.o

  2   gcc -g -shared -Wl,-soname,libasranmysql.so.0 -o libasranmysql.so.0.0 asranmysql.o -lmysqlclient

  3 asranmysql.o: asranmysql.h asranmysql.c

  4   gcc -fPIC -Wall -g -c asranmysql.c

  5 install: libasranmysql.so.0.0

  6   cp asranmysql.h /usr/local/include

  7   cp libasranmysql.so.0.0 /usr/local/lib

  8   ldconfig

  9   ln -s /usr/local/lib/libasranmysql.so.0 /usr/local/lib/libasranmysql.so

 10 uninstall:

 11   rm /usr/local/include/asranmysql.h

 12   rm /usr/local/lib/libasranmysql.*

 13 clean:

 14   rm asranmysql.o libasranmysql.so.0.0


分享到:
评论

相关推荐

    Learning Flask Framework(PACKT,2015)

    It does not presume or force a developer to use a particular tool or library. Flask supports extensions that can add application features as if they were implemented in Flask itself. Flask’s main ...

    QGIS Python Programming Cookbook

    Build a library of reusable scripts with ScriptRunner Create, import, and edit geospatial data on disk or in memory Get to know more about dynamic mapping Create and add features to static maps ...

    C Programming

    - **Making Multiple Decisions**: Explanation of how the `switch` statement works and when it's appropriate to use it instead of nested `if` statements. #### Strings The document explains strings, ...

    Dart in Action

    The book explains how to define and use libraries, as well as the privacy rules that govern access to library members. - **Classes and Interfaces**: Dart's object-oriented features are explored in ...

    OPC UA 客户端 服务器 标准库源码

    How to build and run the console samples on Windows, Linux and iOS This section describes how to run the NetCoreConsoleClient, NetCoreConsolePublisher and NetCoreConsoleServer sample applications. ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    link ▶Use standard order for readability and to avoid hidden dependencies: C library, C++ library, other libraries' .h, your project's .h. All of a project's header files should be listed as ...

    Packt.Mastering.Csharp.and.NET.Programming

    - **Dynamic Programming Features**: Introduces dynamic typing in C# and how it differs from static typing. - **Parallel Extensions**: Explores the Task Parallel Library (TPL) and PLINQ, which ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    - **Variables and Types**: This section explains the different data types available in C# and how to declare and use variables. ### 3. Writing C# Expressions and Statements - **C# Operators**: C# ...

    FFmpeg_ver12430_build

    2)How to use avfilter? a)To vertically flip a video, you would do: ./ffplay -vfilters vflip input_video.avi But the following commands(apply two vflip filter) will result in the orginal picture ...

    Turbo C 2.00[DISK]

    offer, and write for full details on how to receive a free IntroPak containing a $15 credit toward your first month's on- line charges. 2. Check with your local software dealer or users' group. ...

    sqlite3在Visual studio 2012下的编译

    Add the project directory to the include path, here's how to do it in details: Under Project &gt; Properties navigate to the C/C++ folder and choose "General", In the field "Additional Include ...

    微软内部资料-SQL性能优化2

     Give examples of each memory concept and how it applies to SQL Server.  Describe how SQL Server user and manages its memory.  List the primary configuration options that affect memory.  ...

    ElementsBrowserCreator

    It also improves automation efficiency by allowing users to quickly build a library of reusable test scripts. ##### 6. Object-Oriented Automation EBC stands out from other automation tools by ...

    Java邮件开发Fundamentals of the JavaMail API

    your program to use IMAP instead of POP and expect everything in IMAP to be supported. Assuming your mail server supports IMAP, your JavaMail-based program can take Fundamentals of the JavaMail API...

    Tesseract-OCR.rar

    the help.) It might work with your OS if you know how to do that. If you are linking to the libraries, as Ocropus does, please link to libtesseract_api. History ======= The engine was developed at...

    一个跨平台的CString源码

    // the Standard C++ Library basic_string&lt;&gt; template and add to it the // the following conveniences: // - The full MFC CString set of functions (including implicit cast) // - writing to/reading ...

    android-viewflow

    if instead you have a static numbers of views you ought to look at Fragments and the ViewPager in the Compatibility Library instead. Usage In your layout android:id="@+id/viewflow" app:...

    BCGControlBarPro.v12.00

    In general, this mode has been designed and implemented for Vista/Windows 7 Aero, but you can use it in any OSs/modes (see screenshot). The glass (aero) area can be combined with a page header - we'...

Global site tag (gtag.js) - Google Analytics