gpt4 book ai didi

c - 使用 gcc -c 生成 .o 文件

转载 作者:太空狗 更新时间:2023-10-29 16:02:19 24 4
gpt4 key购买 nike

所以我不得不创建一些代码来制作一个列表并对其执行各种操作。专门打印它,对其进行排序,然后查看其中是否有值。这样做,运行良好。现在我必须使用这些函数并将它们拆分成单独的文件,然后使用 gcc -c(我不太确定我使用的是否正确)来获取 .o 文件,以及用于测试程序的 .o .然后我必须使用 gcc 将我的 .o 文件链接到一个可执行文件中。提示说它将识别 .o 并了解如何链接它们。

所以这是我的问题:为什么下面的代码返回错误(以下面定义的方式)?和我到底应该在命令行中写入什么来链接这些人?

所以代码如下:(首先是 .h 文件,然后是主 .c 文件)

节点.h

typedef struct Node{
int data;
struct Node *next;
struct Node *prev;
}node;

打印.h

#include<stdio.h>
#include"node.h"
void print(node *pointer){
if (pointer == NULL){
return;
}

printf("%d ",pointer->data);
print(pointer->next);
}

初始化.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;
}

排序.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);
}
}

列表.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 文件中吗?

我还遇到错误,我试图将不适当的参数传递给 init 函数,但事实并非如此。但我可能只是看多了。

提前感谢您的帮助。

编辑:将 main 中的变量从 new 更改为 start输入“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 *'

(然后 main 中的每个单独的 init 调用都有错误)

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 *'

(然后还有一个用于第二个打印功能)

最佳答案

在 .h 文件中放置包含障碍。例如,对于 sort.h

#ifndef INCLUDE_SORT_H
#define INCLUDE_SORT_H

// contents of file

#endif


编辑

实际上,我只是更仔细地查看了您的代码。您已经在 .h 文件中定义了函数,这不是应该做的事情。真的,我认为根本没有理由将它分成单独的文件。

所以将它们全部合并到一个文件中并编译:

gcc -o list -Wall list.c

如果您确实需要单独的文件,则将函数放入 C 文件,将结构和原型(prototype)放入 .h 文件(您将其包含在每个 C 文件中)。然后使用类似的东西编译和链接:

gcc -o list -Wall list.c node.c main.c

关于c - 使用 gcc -c 生成 .o 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com