`
uuhorse
  • 浏览: 64851 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

Beginning Linux Programming(Get Start)

阅读更多

1、Hello World

/*** hello.c  **/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf(“Hello World!\n”);
    exit(0);
}

 保存代码到hello.c,使用gcc编译:

$ gcc -o hello hello.c

在当前目录产生一个可执行的hello文件,输入$ ./hello 执行输出  Hello World!

 

2、引入头文件

#include <stdio.h> 表示引入头文件,另外还可以使用命令行参数引入外部(目录/usr/openwin/include中)的头文件,(-I是大写的i,include的首字母)

$ gcc -I/usr/openwin/include fred.c

#include <stdio.h>表示从系统路径中(PATH变量指定目录)查找头文件,一般是引入标准库中的头文件;

#include "fred.h" 表示优先从当前目录开始查找头文件,一般是用户自定义的头文件或者第三方库的引入;

 

3、Library文件

一般静态库文件以.a为后缀,.so为共享库文件(类似windows中的lib文件和dll文件)

通常使用 -l 参数(小写的L)引入库,如要引入math相关的库,有两种方式:

① $ gcc -o fred fred.c /usr/lib/libm.a (直接在命令最后跟上库文件)

② $ gcc -o fred fred.c -lm (-lm是libm.a的速记法,注意lm之间没有空格)

如需要引入libX11

$ gcc -o x11fred -L/usr/openwin/lib x11fred.c -lX11

此处可以注意到使用了-L参数,因为libX11的库文件不在标准库的位置(/lib,/usr/lib),需要使用-L参数指明位置;

 

3.1、静态库文件

3.1.1、编译生成OBJ文件

如下两个c源文件,我们创建相应的库

 

/*** fred.c  ****/
#include <stdio.h>
void fred(int arg)
{
    printf(“fred: we passed %d\n”, arg);
}

 

/*** bill.c ***/
#include <stdio.h>
void bill(char *arg)
{
    printf(“bill: we passed %s\n”, arg);
}

使用-c参数编译生成相应的obj文件(编译产生的中间文件,非最终的可执行文件):
    $ gcc -c bill.c fred.c

在当前目录下,使用ls命令查看刚生成的obj文件:
    $ ls *.o
    结果: bill.o fred.o

要在程序中引入该编译过的对象,

① 首先创建对应的.h头文件,声明库中的函数

 

/* lib.h */

void bill(char *);

void fred(int);

 

② 在程序中引入lib.h头文件

/* program.c */

#include <stdlib.h>
#include “lib.h”

int main()
{
    bill(“Hello World”);
    exit(0);
}

 编译执行上述代码:

   

$ gcc -c program.c
$ gcc -o program program.o bill.o
$ ./program
   bill: we passed Hello World
$

 

3.1.2 生成库文件(ar命令)

$ ar crv libfoo.a bill.o fred.o
   a - bill.o
   a - fred.o

使用ar命令将bill.o和fred.o加到库文件libfoo.a中

需要注意的是,在一些系统中,尤其是衍生自Berkeley UNIX的系统,需要使用ranlib命令创建库文件对应的内容表:

$ ranlib libfoo.a

在linux中,此步骤不是必须的,但是一般不会出错。

 

下面,我们就可以直接使用libfoo.a库了:

$ gcc -o program program.o libfoo.a
$ ./program
   bill: we passed Hello World
$

同样的,我们可以使用 -l 参数链接该库文件,但是由于不是标准库文件,需要使用-L 指定目录:

$ gcc –o program program.o –L. –lfoo

 

通过下表,我们可以更清晰的理解各种文件之间的关系

Item UNIX Windows
object module func.o func.obj
static library lib.a

lib.lib

shared library lib.so

lib.dll

program program program.exe

 

 3.2 共享库文件

生成so

 $gcc bill.c fred.c -fPIC -shared -o libfoo.so

使用so编译(-lfoo即引入libfoo.so文件)

$ gcc -g program.c -o program -L. -lfoo

查看program执行时需要链接的库

   

$ ldd program
        linux-vdso.so.1 =>  (0x00007fff785fe000)
        libfoo.so => not found
        libc.so.6 => /lib64/libc.so.6 (0x00007f23e21d2000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f23e2574000)

执行program

命令行输入$./program 回车后报错:
./program: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory

原因是libfoo.so不是标准的库,不在PATH环境下,需要额外指明lib路径,如下:

$ export LD_LIBRARY_PATH=./
$ ./program
    bill: we passed Hello World!

 

 

 

详细的操作步骤如下:

$ mkdir lib
$ cd lib

$
$ vi fred.c
$ vi bill.c
$ ls
bill.c  fred.c
$ gcc -c bill.c fred.c
$ ls
bill.c  bill.o  fred.c  fred.o

$
$ vi lib.h
$ vi program.c
$ ls
bill.c  bill.o  fred.c  fred.o  lib.h  program.c
$ gcc -c program.c
$ ls
bill.c  bill.o  fred.c  fred.o  lib.h  program.c  program.o
$ gcc -o program program.o bill.o
$ ls
bill.c  bill.o  fred.c  fred.o  lib.h  program  program.c  program.o
$ ./program
bill: we passed Hello World!

$
$ ar crv libfoo.a bill.o fred.o
a - bill.o
a - fred.o
$ ranlib libfoo.a
$ ls
bill.c  bill.o  fred.c  fred.o  libfoo.a  lib.h  program  program.c  program.o

$
$ rm program
$ ls
bill.c  bill.o  fred.c  fred.o  libfoo.a  lib.h  program.c  program.o
$ gcc -o program program.o libfoo.a
$ ls
bill.c  bill.o  fred.c  fred.o  libfoo.a  lib.h  program  program.c  program.o
$ ./program
bill: we passed Hello World!

$
$ rm program
$ ls
bill.c  fred.c  libfoo.a   lib.h      program.o
bill.o  fred.o  libfoo.so  program.c
$ gcc -g program.c -o program -L. -lfoo
$ ls
bill.c  fred.c  libfoo.a   lib.h    program.c
bill.o  fred.o  libfoo.so  program  program.o
$ ldd program
    linux-vdso.so.1 =>  (0x00007fff785fe000)
    libfoo.so => not found
    libc.so.6 => /lib64/libc.so.6 (0x00007f23e21d2000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f23e2574000)
$ program
-bash: program: command not found
$ ./program
./program: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
$ export LD_LIBRARY_PATH=./
$ ./program
bill: we passed Hello World!
$

 

 
分享到:
评论

相关推荐

    [Linux.Bible][2005.Edition]

    you to start off at the very beginning with Linux, but still grow to the point where you can get going with some powerful server and programming features, if you care to. Part I assumes that someone ...

    Beginning Android Games 3rd Edition.pdf

    You’ll start with game design fundamentals and Android programming basics, and then progress toward creating your own basic game engine and playable game apps that work on Android smartphones and ...

    Beginning Node.js

    You will see how using Node.js can be a fun and rewarding experience - start today with Beginning Node.js. What you’ll learn • Learn how JavaScript can help you be highly productive as a full-...

    Beginning Python: From Novice to Professional 第二版

    The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ...

    Beginning C# 2008 Databases From Novice to Professional

    The Beginning series from Apress is the right choice to get the information you need to land that crucial entry-level job. These books will teach you a standard and important technology from the ...

    Beginning Android Games [3rd Edition,2016]

    You'll start with game design fundamentals and Android programming basics, and then progress toward creating your own basic game engine and playable game apps that work on Android smartphones and ...

    Beginning iPhone Development with Swift 5, 5th Edition.pdf

    Beginning iPhone Development with Swift 5 covers the basic information you need to get up and running quickly to turn your great ideas into working iOS apps. Once you’re ready, move on to Pro iPhone ...

    Think Julia

    Get a clear definition of each programming concept Learn about values, variables, statements, functions, and data structures in a logical progression Discover how to work with files and databases ...

    Learning [removed] JavaScript programming Vol 1: The language core

    all you need to get going is a web browser. So just start! Step by step, you will learn the core of the JavaScript language and how to apply it in everyday programming. You will learn how to write ...

    一本android的好书beginning android 2 和 源码

    Start at the Beginning Setting the Table Makin’ Data What Goes Around Comes Around Raw Queries Regular Queries Building with Builders Using Cursors Data, Data, Everywhere ■Chapter 23: Accessing ...

Global site tag (gtag.js) - Google Analytics