观海听涛BBS's Archiver

zhponly 发表于 2008-4-21 21:40

呃,求助一个多文件编译的问题(已解决)

[code]如下:
----------------------------------main.c
#include"fish.h"
int main(void)
{
    i=0;
    printf("%d",linker.key);
    getch();
    return 0;
}
----------------------------------main.c
----------------------------------fish.h
#include"AQS.c"
#include"AB.c"
#include"LB.c"
#include"val.c"
#include"ID.c"
extern int array[];
extern struct LINK linker;
extern int i;
extern int *p;
----------------------------------fish.h
----------------------------------ID.c
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
----------------------------------ID.c
----------------------------------LB.c
struct LINK
           {
            int key;
            struct LINK *next;
           };
struct LINK linker;
linker.key=5;
----------------------------------LB.c
此时无论是win-tc或vc++都会报错...
错误定位在LB.c的最后一行上,即linker.key=5;
不知道为什么.....如果去了那一行,即:
----------------------------------LB.c
struct LINK
           {
            int key;
            struct LINK *next;
           };
struct LINK linker;
----------------------------------LB.c
在main中处理这个,即:
----------------------------------main.c
#include"fish.h"
int main(void)
{
    i=0;
    linker.key=5;
    printf("%d",linker.key);
    getch();
    return 0;
}
----------------------------------main.c
就正确了.....呃...不知道为什么....
如果各位看官知道原因,烦告知................[/code]

[[i] 本帖最后由 zhponly 于 2008-4-28 22:20 编辑 [/i]]

zhponly 发表于 2008-4-22 13:40

噢,事实证明是win-tc对这个支持不太好...于是换了dev........结果如下....:[code]  呃,希望可以学一些东西,为后来准备点.

   一般是建立一个工程:

   之后有:

----------------------------------------------------------------main.c

#include <stdio.h>
#include <stdlib.h>
#include"test.h"
int a;
int b;
extern void test(void);
int main(int argc, char *argv[])
{
  test();
  printf("%d\n",a);
  getch();
  return 0;
}

----------------------------------------------------------------main.c

----------------------------------------------------------------test.c

#include"test.h"
void test(void)
{
printf("hello ,wrold\n");      
}

----------------------------------------------------------------test.c

----------------------------------------------------------------test.h

extern void test(void);

----------------------------------------------------------------test.h

这个比较简单的是.只是容易发生一些比较诡异的错误:

在test.h加一句:extern int a;

在test.c中加一句int a=5;

那么main.c中就会输出5了.

比较奇怪的是dev中,重包含头文件没有关系......

win-tc对多模块的支持很差.....放弃了.....

如果是结构体或是类...就有些不同了....

噢....以前学的全忘了.....

----------------------------------------------------------------main.c

#include <stdio.h>
#include <stdlib.h>
#include"test.h"
extern void test(void);
extern struct LINK linker[10];
int main(int argc, char *argv[])
{
  test();
  printf("%f\n%d",a,linker[3].key);
  getch();
  return 0;
}
----------------------------------------------------------------main.c

----------------------------------------------------------------test.c

#include"test.h"
#include<stdio.h>
double a=5.5;
struct LINK linker[10];
void test(void)
{
linker[3].key=5;   
printf("hello ,wrold\n");      
}
----------------------------------------------------------------test.c

----------------------------------------------------------------test.h

#include<stdio.h>
extern void test(void);
extern double a;
struct LINK
{
  int key;
  struct LINK *next;     
};
----------------------------------------------------------------test.h[/code]

Iecho 发表于 2008-4-23 17:29

也就说你原来的文件在dev下运行成功了?

zhponly 发表于 2008-4-23 18:45

回复 3# Iecho 的帖子

噢..不是.....事实上第一个是种极不专业的写法...以前学过的...最近学算法了..忘了....
参照了下书.......2楼的写法比较好.....

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.