C使用相关笔记

#将c文件编译成动态库
//hello.c

int hello_add(int a, int b)
{
    return a + b;
}

gcc -O -c -fPIC -o hello.o hello.c                    // -fPIC:是指生成的动态库与位置无关。    将hello.c编译成hello.o
gcc -shared -o libhello.so hello.o                    // -shared:是指明生成动态链接库          将hello.o链接成libhello.so动态库

#使用一个动态库
//hello_test.c

#include <stdio.h>

int main()
{
    int a = 3, b = 4, result = 0;
    result = hello_add(a, b);
    printf("%d + %d = %d\n", a, b, hello_add(a,b));
    printf("%d======\n", result);
    return 0;
}

cp libhello.so /usr/lib/                                //将生成的动态库复制到lib目录
gcc -o hello_test -lhello hello_test.c                  //通过-l选项来指定要使用的相关动态库

#关于const常量
const int *a;
int const *a;            //值不可变,地址可以变

int * const a;           //值可变,地址不可变

int const * const a;     //值和地址都不可变


#include <stdio.h>

void test_const(int * const a)
{
    //int c = 5;
    //*a = c;
    (*a)++;
    //a++;//
    printf("0x%x\n", a);
    printf("%d\n", *a);
}

int main(void)
{
    int b = 2;
    
    test_const(&b);
    return 0;
}

#野指针 会导致 段错误(“segmentation fault”)

#指针也有类型,所以a++这种操作系统才知道要移动几个内存单元。
#指针的类型是在指针定义时确定的。比如:“int *p;”中“int *”就指明了这是一个什么类型的指针。如果p++,那系统根据指针类型就知道要移动几个内存单元格。
如果“p + 1”是否也是根据指针类型来确认移动几个内存单元?


#Makefile有三个非常有用的变量。分别是$@,$^,$<代表的意义分别是:[1]
$@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件。

#Makefile中输出一个变量的内容
echo $(OUTPUT_DIR)

#Makefile中要用tab缩进,否则会引起语法错误

#Makefile字符串拼接 [1] [2]
var3=${var1}${var2}






posted on 2015-08-14 15:10  dream_bccb  阅读(199)  评论(0)    收藏  举报