《Beginning Linux Programming》读书笔记(一)

1,第一个程序

#include <stdio.h>
int main()
{
    printf(
"hello,linux\n");
    
return 0;
}

编译运行:

gcc -o hello hello.c
./hello

2,链接目标文件

#include <stdio.h>

void fred(int arg)
{
    prinft(
"fred: you %\n",arg);
}

 

#include <stdio.h>

void bill(char *arg)
{
    printf(
"bill: you passed %s\n", arg);
}

编译:

gcc –c bill.c fred.c

 

/*
    This is lib.h. It declares the functions fred and bill for users
*/

void bill(char *);
void fred(int);

 

#include "lib.h"

int main()
{
bill(
"Hello World");
fred(
100);
    exit(
0);
}

编译运行:

gcc –c program.c
gcc –o program program.o bill.o fred.o
.
/program

3,打包为静态链接库再链接

ar crv libfoo.a bill.o fred.o
gcc 
-o program program.o libfoo.a
.
/program

也可以这样

gcc -o program program.o -L. -lfoo

这里的-L.就指明编译器在当前目录下寻找库文件,-lfoo告诉编译器使用名为libfoo.a的静态库(或名为libfoo.so的共享库)

4,要查看obj文件,库或可执行文件中包含的函数,可以使用nm命令,

nm libfoo.a

5,可以使用ldd来查看程序所需要的共享库

ldd program

 6,一个简单的脚本

#!/bin/bash

for file in *
    
do 
        
if grep -q bash $file
        then
            echo $file
        fi
    done

exit 
0

可以有两种执行方式

/bin/bash first

或者先改变脚本文件的权限,给用户加上可执行权限

chmod u+x first

然后直接执行

first

     如果报错说“找不到命令“,则说明Shell环境变量PATH中没有设置当前目录这一项,可以有两种方式改变,要么输入”PATH=$PATH:. “,再用”export”使之生效,要么编辑.bash_profile文件,将这个命令加入到文件末尾,然后登出再登陆回来。当然另一种暂行的方法是输入”./first”。当然最后一种方式是linux推荐的。

      另外,我们可以将上面这个脚本文件放到别的目录下共享

sudo cp first /usr/local/bin
chmod 
755 /usr/local/bin/first

最后一行是给予组用户和其他用户执行权限

7,在Windows下,查看环境变量的命令是:set,这个命令会输出系统当前的环境变量,那Linux下应该如何查看呢,命令是:

export

如果你想查看某一个名称的环境变量,命令是:echo $环境变量名,比如:

echo $JAVAHOME    

 

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

设置环境变量

如果使用的是bash外壳,则键入如下命令:

JAVA_HOME=/ path/ to/ jdk

export JAVA_HOME

其中/path/to/jdk是安装Java的路径。

如果使用的是tcsh,则键入如下命令:

setenv JAVA_HOME /path/to/jdk

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

删除环境变量

字符模式下设置/删除环境变量

bash

设置:export 变量名=变量值

删除:unset 变量名

csh

设置:setenv 变量名 变量值

删除:unsetenv 变量名

 

posted on 2008-11-24 09:27  Phinecos(洞庭散人)  阅读(1898)  评论(4编辑  收藏  举报

导航