+ -
当前位置:首页 → 问答吧 → 菜鸟问个关于全局变量连接的问题

菜鸟问个关于全局变量连接的问题

时间:2010-09-12

来源:互联网

there are some global vars defined in a.cpp,b.cpp use one global var which is defined in a.cpp.the question is:how to compile and link b.cpp?

比如下面的测试例子:
vi a.c

#include <stdio.h>

int a,b;

int main()
{
  int c;
  c=a+1;
  printf("c=%d\n",c);
  return 0;
}

vi b.c

#include <stdio.h>

extern b;

int main()
{
  int d;
  d=b-1;
  printf("d=%d\n",d);
  return 0;
}

我想的是把a.c弄成一个库给b.c调用,结果如下:
test]$ gcc a.c b.c -c
test]$ ls
a.c  a.o  a.out  b.c  b.o  doubletest.c
test]$ ar r liba.a a.o
ar: creating liba.a
test]$ file liba.a
liba.a: current ar archive
test]$ gcc b.o -o b -L. -la
./liba.a(a.o)(.text+0x0): In function `main':
: multiple definition of `main'
b.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
为什么不行呢?

大哥们帮帮忙,拜谢了!

作者: lingxuwaltz   发布时间: 2010-09-12

a、b都有main

作者: hellioncu   发布时间: 2010-09-12

相关阅读 更多