首页 > 编程知识 正文

THE compile of serveral files

时间:2023-05-03 11:09:51 阅读:267544 作者:4489

cc多文件编译 三个文件:tree.c tree_fun.c tree.h 我怎么把他们编译成一个可执行文件?? 其中tree.c和tree_fun中已include了文件tree.h,???? 点击打开链接

最佳答案

写一个Makefile文件,如下:SOURCE = tree.c tree_fun.c DEST = mainGCC = gccFLAGS = all: $(GCC) $(SOURCE) $(FLAGS) -o $(DEST)这样就可以直接make,可以编译这三个文件了,编译成的可执行文件是main






https://zhidao.baidu.com/question/149436491.html?qbl=relate_question_4

gcc 编译c的多文件程序问题 菜鸟学习linux下C编程不太理解多文件编译问题,写了个实例,请高手指点.
写了一个链表的操作程序,包括主函数文件main.c,链表创建函数文件creat.c
打印函数文件print.c删除函数文件del.c,添加函数文件insert.c.
代码如下:
主函数文件main.c
#include<stdio.h>
main()
{
struct student *head,stu;
long del_num;
printf("input records:n");
head=creat();
print(head);
printf("n input the deleted number:n");
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
printf("n input the inderted record:");
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
}

链表建立文件creat.c
#include<stdio.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num ;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("输入学号,成绩n");
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}

删除函数文件

struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{printf("nlist null! n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete:%ldn",num);
n=n-1;
}
else
printf("%ld not been found! n",num);
return(head);
}

插入文件insert.c
#include<stdio.h>
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
p2=p1;
p1=p1->next;
if(p0->num<=p1->num)
{if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;

}
else
{
p1->next=p0;p0->next=NULL;

}

}
n=n+1;
return(head);
}

打印函数文件
print.c
#include<stdio.h>
void print(struct student *head)
{
struct student *p;
printf("nNow,These %d records are :n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1fn",p->num,p->score);
p=p->next;
}while(p!=NULL)
}
问题:
1各函数文件是相互独立的,怎样添加代码使之能够关联编译成功?
2使用GCC编译的话具体的编译命令?


最佳答案

不怕麻烦的话呢,可以调整#include结构来让各c文件相连。。建议呢,把类型与函数的声明整理到特定的.h文件中,然后用#ifndef来使其只被包含一次,然后,在用到其它文件中实现的函数的时候将相应的.h文件include进来就好。这样呢,编译的时候可以单个文件逐一编译: gcc -c main.c -o main.o gcc -c del.c -o del.o gcc -c insert.c -o insert.o参数-c意思是只编译不连接,-o是名命输出文件。全部编译成.o文件无误后,再将所有的.o文件相连: gcc main.o del.o insert.o -o prog就可以生成prog了。然后举个例子说明处理.h文件:比如这个create.c。里面定义了一个struct和一个函数。那么就可以写create.h如下:#ifndef __CREATE_H__#define __CREATE_H__struct student { long num ; float score; struct student *next;};struct student *creat(void);#endif然后呢,因为student这个struct在这个.h文件里已经声明了,所以create.c里只要写#include "create.h",而在用到这个struct或者这个函数的场合,也只要这样简单一句就可以了。而.h文件开头的#ifndef可以保证同一个h文件不会被多次包含。当然,如果编译器支持的话,用#pragma once也许会比#ifndef更省事一些。





----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Linux编译多个文件


-c 只编译不连接

  g++ file1 -c -o file1.o

  g++ file2 -c -o file2.o

  g++ file1.o file.o -o exec

  g++   -c   a.cpp   编译

  g++   -o   a   a.o   生成可执行文件

  也可以   g++   -o   a   a.cpp直接生成可执行文件。

  1. 编译单个源文件

  为了进行测试,你可以创建“Hello World”程序:

  #include <stdio.h>

  #include <stdlib.h>

  int main(int argc, char **argv)

  {

  printf(“Hello world!n”);

  exit(0);

  }

  使用如下命令编译并测试这个代码:

  # gcc -o hello hello.c

  # ./hello

  Hello wordl!

  在默认情况下产生的可执行程序名为a.out,但你通常可以通过 gcc 的“-o”选项来指定自己的可执行程序名称。

  2. 编译多个源文件

  源文件message.c包含一个简单的消息打印函数:

  #include <stdio.h>

  void goodbye_world(void)

  {

  printf(“Goodbye, world!n”);

  }

  使用gcc的“-c”标记来编译支持库代码:

  # gcc -c message.c

  这一过程的输出结果是一个名为message.o的文件,它包含适合连接到一个较大程序的已编译目标代码。

  创建一个简单的示例程序,它包含一个调用goodbye_world的main函数

  #include <stdlib.h>

  void goodbye_world(void):

  int main(int argc, char **argv)

  {

  goodbye_world();

  exit(0);

  }

  使用GCC编译这个程序:

  # gcc -c main.c

  现在有了两个目标文件: message.o 和 main.o 。它们包含能够被 Linux 执行的目标代码。要从这个目标代码创建Linux可执行程序,需要再一次调用 GCC 来执行连接阶段的工作:

  # gcc -o goodbye message.o main.o

  运行编译结果:

  # ./goodbye

  Goodbye, world!

  前面这些单独的步骤也可以简化为一个命令,这是因为 GCC 对如何将多个源文件编译为一个可执行程序有内置的规则。

  # gcc -o goodbye message.c main.c

  # ./goodbye

  Goodbye, world!






版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。