开发者

making small c project

开发者 https://www.devze.com 2023-02-28 23:39 出处:网络
ev开发者_如何学Cerybody out there i write a very simple c code which is following: #include<stdio.h>

ev开发者_如何学Cerybody out there i write a very simple c code which is following:

#include<stdio.h>
int main()
{
    int a,b,s,m,d;
    system("clear");
    int a =20;
    int b =40;
    s=sum(a,b);
    m=mul(a,b);
    d=div(a,b);
    printf("\n the sum of given no. = %d\nThe product of given no. = %d\nThe division of given no = %d",s,m,d);
    return 0;
}

the name of the file is exp.c than i write the following code:

#include<stdio.h>
int sum( int x ,int y)
{
   int z;
   z=x+y;
   return z;
}

i saved it as sum.c than i write the following code :

#include<stdio.h>
int mul( int z ,int u)
{
   int v ;
   v=z+u;
   return v;
}

save it as mul.c than i write the following code

#include<stdio.h>
int div (int a, int b)
{ 
   int f;
   f=a/b;
   return f;
}

save it as div .c

now my problem is that i want to use all file as a single project. i want exp.c use the function defined in mul.c,div.c,sum.c i want to know how to do this? how to make library form mul.c,div.c,sum.c? how to associate these library with exp.c ? can any body explain me the detail process of making project ? i 'm using ubuntu as my operating system. please help me


The easiest way is to not make a library, but just compile them all together into a single executable:

$ gcc -o myprogram sum.c mul.c div.c

This has the drawback that you will re-compile all the code all the time, so as the files grow large, the penalty (build time) goes up since even changing just div.c (for example) will force you to re-compile sum.c and mul.c too.

The next step is to compile them separately, and leave the object files around. For this, we can use a Makefile like so:

myprogram: sum.o mul.o div.o

sum.o:     sum.c

mul.o:     mul.c

div.o:     div.c

This will leave the object files around, and when you type make the make tool will compare the timestamps of the object files to those of the C files, and only re-compile that which changed. Note that for the above to work, there must be a physical TAB after each colon.


There are a few steps you need to do for this:

  1. Declare the functions in your main file When you compile your main file (exp.c) the compiler will output an error because he does not know what kind of functions sum, mul etc. are. So you have to declare them via int sum( int x ,int y); in this file. A more general approach (which is clearer) is to write all the functions you have in a file (not all, but those that will be accessed from other files) into a header file and then include the header file.

  2. Compile each file You need to compile each file. This can be done via a simple gcc -c mul.c etc. This will create a mul.o - a machine language file.

  3. Link them Once every file is compiled you need to put them together in one executable. This is done via gcc -o outputname mul.o sum.o ...

Note that steps 2 and 3 can also be combined, I just wanted to explain the steps clearly. This is usually done via a Makefile to speed things up a bit


Firstly, you will need to declare each of your functions in a corresponding header file (you don't have to use header files, but it's the most common way of doing this). For instance, div.h might look like:

#ifndef DIV_H_
#define DIV_H_

int div(int a, int b);

#endif

You will then to #include the header files in source files where the corresponding functions are used.

Then, to compile and link:

gcc -o my_prog exp.c sum.c mul.c div.c

As others have suggested, you make want to read up on Make, as it helps simplify the build process once your project gets more complicated.


You need to declare the functions in the file they are used. The common way to do this is to put the declarations in a header file, lets say funcs.h:

#ifndef FUNCS_H
#define FUNCS_H

int sum( int, int );
int mul( int, int );
int div( int, int );

#endif

Now #include this in your main source file. Then to build the executable:

 gcc exp.c sum.c div.c mul.c

To create a library, you need to compile the files separately:

gcc -c sum.c div.c mul.c

and then run ar to build the library:

ar rvs sum.o div.o mul.o mylib.a

And then use it from gcc:

gcc exp.c mylib.a


A good practise to organize the code could be put all the functions prototypes inside a .h file, and the implementations into a related .c file, using include guards to avoid multiple inclusion.

Example module.h file:

#ifndef MODULE_NAME
#define MODULE_NAME
void module_func();
#endif

Example module.c :

#include "module.h"
void module_func(){
  //implementation
}


read up on make - this will answer your questions about building/compilation/etc

You should have a .h file that will include your function prototypes. It's not strictly needed (as your functions return int) but you must get in the habit now, because it won't come easy later

0

精彩评论

暂无评论...
验证码 换一张
取 消