`

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 ...

    Echo Quick Start Guide [True PDF]

    After that, you will learn how to test your Go application and use templates. By the end of this book you will be able to build your very own high performance apps using Echo. A Quick Start Guide ...

    LLVM.Essentials.1785280

    We then cover how you can use LLVM library calls to emit intermediate representation (IR) of simple and complex high-level language paradigms. Moving on, we show you how to implement optimizations at...

    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 ...

    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.  ...

    Packt.Building.your.First.Mobile.Game.using.XNA4.2013

    In this book, we will cover drawing 2D and 3D graphics, both static and animations. We will also cover the various ways of handling user input and help set the mood of our game playing both 2D and 3D ...

    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...

    一个跨平台的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:...

    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...

    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'...

    Heilx AAC Decoder optimized for ARM

    - C and assembly code only (C++ not required for codec library) - reentrant, statically linkable - low memory (details in docs/ subdirectory) - option to use Intel Integrated Performance ...

    Professional.MFC.with.VC6

    Document/View - When Not to Use It Avoiding the Document/View Architecture Playing Along with the Document/View Architecture Tricks with Templates Multiple Templates Adding a New Item to your ...

    linux内核 0.11版本源码 带中文注释

    * Yeah, yeah, it's ugly, but I cannot find how to do this correctly * and this seems to work. I anybody has more info on the real-time * clock I'd be interested. Most of this was trial and error...

Global site tag (gtag.js) - Google Analytics