Chinaunix首页 | 论坛 | 博客
  • 博客访问: 517687
  • 博文数量: 137
  • 博客积分: 3170
  • 博客等级: 中校
  • 技术积分: 1455
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 11:47
文章分类

全部博文(137)

文章存档

2015年(2)

2013年(1)

2012年(6)

2011年(5)

2010年(62)

2009年(61)

我的朋友

分类: C/C++

2009-12-01 18:56:34


1.缘起:为学习生成静态库.a和.so,
例子代码总是编译不过,主要的问题是重复定义全局变量,所以上网查了一下资料,经过自己的修改记录如下:
ctest1.c

#include "my.h"
int h = 999;
int tst2 = 888;

void ctest1(int *i)
{
   *i= h;
}


ctest2.c

#include "my.h"
void ctest2(int *i)
{
   *i=100;
   *i = tst2;
}


my.h

#ifndef _MY_H
#define _MY_H
extern int h ;
extern int tst2 ;
void ctest2(int *i);
void ctest1(int *i);
#endif

prog.c

#include <stdio.h>
#include "my.h"
extern void ctest2(int *i);
extern void ctest1(int *i);
int

main()
{
    int x, y;
   ctest1(&x);
   ctest2(&y);
   printf("Valx=%d\n",x);
   printf("Valy=%d\n",y);
   return 0;
}


2.使用命令行直接编译生成可执行程序prog:

gcc ctest1.c ctest2.c my.h prog.c -o prog


3.makefile的书写
3.1隐晦写法:

prog: prog.o ctest1.o ctest2.o
ctest1.o: ctest1.c
ctest2.o: ctest2.c
clean:
    rm prog prog.o ctest1.o ctest2.o

3.2 完整的书写-target,prequsite,cmd都全的
目前写不出来 ,呵呵。编译不过。功力不够。
阅读(1367) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~