所以我不得不創建一些代碼來製作一個列表併爲它做各種事情。具體打印它,對其進行分類,並查看是否有值。沒有,運行良好。現在,我必須將這些函數分成不同的文件,然後使用gcc -c(我不確定我是否正確使用)來獲取.o文件,再加上測試程序的.o 。然後我必須使用gcc將我的.o文件鏈接到一個可執行文件中。提示表示它會識別.o的並意識到如何鏈接它們。使用gcc -c生成.o文件
所以,這裏是我的問題: 爲什麼下面的代碼返回錯誤(將在下面定義的方式)? 和 究竟是什麼我應該寫入命令行來鏈接這些傢伙?
因此,代碼如下: (第一h文件,則主.c文件)
node.h
typedef struct Node{
int data;
struct Node *next;
struct Node *prev;
}node;
print.h
#include<stdio.h>
#include"node.h"
void print(node *pointer){
if (pointer == NULL){
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
init.h
#include<stdio.h>
#include"node.h"
int init(node *pointer,int find){
pointer = pointer->next;
while (pointer != NULL){
if (pointer->data == find)//found find
{
printf("The data is in the list.");
return 1;
}
pointer = pointer->next;// Search in the next node.
}
//find is not found
printf("The data is not in the list.");
return 0;
}
sort.h
#include<stdio.h>
#include"node.h"
void swap (node *x, node *y){
int temp = x->data;
x->data = y->data;
y->data = temp;
}
void sort(node*pointer){
int i;
while (pointer->next != NULL){
if (pointer->data>pointer->next->data){
swap(pointer,pointer->next);
}
pointer = pointer->next;
sort(pointer);
}
}
list.c
#include<stdio.h>
#include<stdlib.h>
#include"node.h"
#lnclude"print.h"
#include"sort.h"
#include"init.h"
int i;
node *p;
node *n;
void insert(node *pointer, int data){
//go through list till ya find the last node
while (pointer->next != NULL){
pointer = pointer->next;
}
//allocate memory for new node and put data in it
pointer->next = (node *)malloc(sizeof(node));
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int main(){
//start is used to point to the first node
//temp is for the last node
node *start, *temp;
int z;
start = (node *)malloc(sizeof(node));
temp = new;
temp->next = NULL;
temp->prev = NULL;
for (z = 0; z < 10; z++){
insert(start,(3*10) - z);
}
init(start,12);
init(start,3);
init(start,27);
init(start,7);
print(start);
sort(start);
print(start);
}
現在的代碼都跑了完美的罰款當一切都在一起,node.h的異常(即始終是一個單獨的文件)。 .h文件將完美編譯,但是當我嘗試編譯.c文件時,它會返回錯誤,聲明我正試圖在每個.h文件中重新定義節點。這可能是因爲我將它包含在每個.h文件中?
我也遇到錯誤,我試圖將不適當的參數傳遞給初始化函數,這似乎並不是這種情況。但我可能只是在看東西。
非常感謝您的幫助。
編輯:在從主新改變可變鍵入「GCC list.c」
In file included from init.h:2:0,
from list.c:4:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2 error: conflicting types for 'node'
node.h:5:2 note: previous declaration of 'node' was here
In file included from sort.h:2:0;
from list.c:5:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2 error: conflicting types for 'node'
node.h:5:2 note: previous declaration of 'node' was here
list.c: In function 'main':
list.c:41:1: warning: passing argument 1 of 'init' from incompatible pointer type[enabled by default]
init.h:4:5: note: expected 'struct node *' but argument is of type 'struct node *'
(再有每個在主單獨初始化呼叫的錯誤)
何時開始 錯誤list.c:45:1: warning:passing argument 1 of 'print' from incompatible pointer type [enabled by default]
print.h:3:6: note expected 'struct node *' but argument is of type 'struct node *'
(再有另一個用於第二打印功能)
問:我可以使用「gcc -c」創建.o文件(並在稍後鏈接它們)嗎?答:是的。問:*你得到什麼特定的錯誤*? PS:不要使用變量名稱「new」 - 它很容易與C++關鍵字混淆。 – paulsm4 2013-02-10 23:12:43
所以我在威廉莫里斯的幫助下解決了我的錯誤。但我不完全確定要寫什麼來創建.o文件或將它們鏈接到可執行文件中。你能詳細說明一下嗎? – drpogue 2013-02-10 23:33:46
威廉莫里斯以「[衛兵](http://en.wikipedia.org/wiki/Include_guard)」爲你提供幫助。這個約定允許你多次使用頭文件,而不會出現「重複定義」的錯誤。你的下一個挑戰是熟悉「[make](http://en.wikipedia.org/wiki/Makefile)」。這是一個很好的教程(你可以在網上找到很多教程):http://www.codeproject.com/Articles/31488/Makefiles-in-Linux-An-Overview – paulsm4 2013-02-11 19:07:27