如何在代码块中编译多个C文件

时间:2015-10-16 04:15:41

标签: c compilation codeblocks

所以我最初在代码块中编写代码,但是当我尝试编译它时总是会给我错误,说它不理解我对其他文件中的函数所做的引用。我从哪一点开始使用原子,但它已经到了我需要在代码块中使用他调试工具的地步,即使我的代码在我通过gcc运行时编译,我仍然会得到相同的错误。有人可以帮忙吗?这些是我得到的错误。

|| === Build:A2中的Debug(编译器:GNU GCC编译器)=== | obj \ Debug \ main.o ||在函数main':| main.c|18|undefined reference to createMyVector'|中 main.c | 29 |未定义引用PathInit'| main.c|30|undefined reference to AllPathsRec'| main.c | 31 |对“PathPrint”的未定义引用 || ===构建失败:4个错误,0个警告(0分钟,0秒(秒))=== |

的main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vector.h"
#include "path.h"
#define BUFFERSIZE 20

int main()
{
Vector *leVector;

unsigned int size;
char leArray[BUFFERSIZE];

scanf("%u\n",&size);
fgets(leArray,sizeof(leArray),stdin);

leVector = createMyVector(size);

char *element = strtok(leArray, " ");

int i;
for(i=0;i<size;i++){
    *(leVector->item + i) = atoi(element);
    element = strtok(NULL," ");
}

Path Solution;
PathInit(&Solution,size);
AllPathsRec(0,leVector,&Solution);
PathPrint(&Solution);
return 0;
}

vector.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vector.h"

void vectorRead(Vector * V){
printf("Size of the array is: %d\n",V->size);

int i;
for(i = 0; i < V->size; i++){
    if(i == V->size)
        printf("%d\n ",*(V->item+i));
    else
        printf("%d ",*(V->item+i));
}
}

Vector * createMyVector(int size){
Vector * vect = (Vector *)malloc(sizeof(Vector));
vect->size = size;
vect->item = (int *)malloc(sizeof(int)*size);
return vect;
}

path.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vector.h"
#include "path.h"

void PathInit(Path *P, int vsize){
P->size = vsize;
P->item = (int *)malloc(sizeof(int)*vsize);
P->top = -1;
}

int AllPathsRec(int position, Vector *V, Path *Solution){
PathAddEntry(Solution,position);
position += *(V->item + position);

while(Solution->top != V->size -1){
  AllPathsRec(position, V, Solution);
}

return 0;
}

int PathAddEntry(Path *P, int entry){
if(P->top >= P->size - 1){
printf("ERROR: STACK OVERFLOW\n");
return 1;
}
P->top++;
*(P->item + P->top) = entry;
return 0;
}

int PathRemoveEntry(Path *P){
if(P->top <= -1){
  printf("\nERROR: NO ELEMENT TO REMOVE\n");
  return 1;
}
P->top--;
return 0;
}

void PathPrint(Path *P){
printf("Size of the Solution array is: %d\n",P->size);

int i;
for(i = 0;i <= P->top; i++){
    if(i == P->top)
        printf("%d\n ", *(P->item+i));
    else
        printf("%d ", *(P->item+i));
}

}

vector.h

#ifndef VECTOR_H
#define VECTOR_H

typedef struct {
int size;
int *item;
}Vector;

Vector * createMyVector(int size);
void vectorRead(Vector * V);
#endif

path.h

#ifndef PATH_H
#define PATH_H

typedef struct{
int size;
int top;
int *item;
}Path;

void PathInit(Path *P, int size);
int AllPathsRec(int position, Vector *V, Path *Solution);
int PathAddEntry(Path *P, int entry);
int PathRemoveEntry(Path *P);
void PathPrint(Path *P);

#endif

0 个答案:

没有答案