How to link two files using header file in C - c

I am trying to link two files. Means, there are files "file1.c" and "file2.c".
file1.c
#include"stdlib.h"
#include"stdio.h"
void function1(int a)
{
printf("hello I am file%d.c\n ", a);
}
void main()
{
function1(1);
}
file2.c
#include"stdlib.h"
#include"stdio.h"
#include"file.h"
void function2(int b)
{
printf("hello I am file%d.c\n", b);
}
int main()
{
function2(2);
function1(1);
}
Then I make a header file file.h as
#ifndef hell
#define hell
void function1(int a);
#endif
When I compile file2.c as "gcc file2.c file1.c -o file2
" it gives following error
/tmp/cc4tno9R.o: In function `main':
file1.c:(.text+0x24): multiple definition of `main'
/tmp/ccL4fEki.o:file2.c:(.text+0x24): first defined here
collect2: ld returned 1 exit status
How to write in header file? Is there any error in header file?
Or error in file2.c?
And what about extern? Is it uses for same purpose?

Say that the directory structure is like:
Project
|
------------------------------
| | |
csource output header
| | |
*.c files executable .h files
files
Now, put these two .c files inside the source folder.
function.c
int sum(int a, int b)
{
return (a + b);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <mymath.h>
int main(void)
{
int result = sum(11, 19);
printf("Result: %d\n", result);
return EXIT_SUCCESS;
}
Put this header file inside header folder.
mymath.h
#ifndef _MyMath_H__
#define _MyMath_H__
int sum(int, int);
#endif
COMPILATION:
Firstly, we will compile function.c file and create one object file with .o extension, as follows:
C:\Mine\C\test\project>gcc -o output\function.o -c source\function.c
On Cygwin:
Gagandeep Bali#LAPTOP ~/c/Mine/C/test/project
$ gcc -o output/function.o -c source/function.c
Since, function.c doesnot contains a main method, hence, we will simply use the -c option, to only create an object file.
Here, the use of -I option, basically tells the compiler, where to look for include files. Since, we are defining our header folder, hence, you can use #include <mymath.h> instead of #include "mymath.h". Now we will compile themain.c` file as:
C:\Mine\C\test\project>gcc -o output\main -I header\ -Wall source\main.c output\function.o
On Cygwin:
Gagandeep Bali#LAPTOP ~/c/Mine/C/test/project
$ gcc -o output/main -I header/ -Wall source/main.c output/function.o
Now one can run it, like:
C:\Mine\C\test\project>.\output\main
Result: 30
On Cygwin:
Gagandeep Bali#LAPTOP ~/c/Mine/C/test/project
$ ./output/main
Result: 30
You can also, create static and dynamic libraries, of custom functions, that you can use. I just know, how to create a static library.
If you wanted to create a static library, of your own, simply first put all object files inside the library. Create another folder, say library for this purpose. Now add all .o files inside the library, like this:
Gagandeep Bali#LAPTOP ~/c/Mine/C/test/project
$ ar cr library/mymathlibrary.a output/function.o
Now simply compile program like:
Gagandeep Bali#LAPTOP ~/c/Mine/C/test/project
$ gcc -Wall source/main.c library/mymathlibrary.a -o output/main -I header
And run as previously described.

You don't need to include all library files in the first file. Just save it as a library file with a ".h" extension as a library file and include it in second file, Like shown below.
file1.h
void function1(int a) {
printf("hello I am file%d.c\n ", a);
}
file2.c
#include <stdlib.h>
#include <stdio.h>
#include "file.h"
void function2(int b) {
printf("hello I am file%d.c\n", b);
}
int main() {
function2(2);
function1(1);
return 0;
}

So all should look like this:
file1.c:
#include <stdlib.h>
#include <stdio.h>
void function1(int a) {
printf("hello I am file%d.c\n ", a);
}
file2.c:
#include <stdlib.h>
#include <stdio.h>
#include "file.h"
void function2(int b) {
printf("hello I am file%d.c\n", b);
}
int main() {
function2(2);
function1(1);
return 0;
}

When you run the program main is beeing called. If you have 2 definitions of main which one should be called?
There should be one file including main and another file including function that you want to use in the first file.

Related

How can i run multiple files in C (visual studio code)?

I want to to run a simple program, using vs code, which includes three files: main.c item.c item.h.
I understand that I there is a way to link things together, but I don't know how. Can you explain me how to do it?
I've tried also to add the extension to make a project, but I didn't understand how to.
Here's the code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "item.h"
int main () {
int a = 2;
int b = 3;
int res;
res = prod(a,b);
printf("%d ", res);
return 0;
}
item.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "item.h"
int prod(int a, int b) {
return a*b;
}
item .h
#ifndef ITEM_H
#define ITEM_H
int prod(int a, int b);
#endif
I don't know if you are using Windows or Linux or Mac, I'm going to explain it for Linux but the method is similar for the others.
First of you go on VS Code, then you click on new file and rename it "makefile", then you write this:
link: item.o main.o
gcc item.o main.o -o programName
main.o:
gcc -c main.c
item.o:
gcc -c item.c
clear:
rm -f item.o main.o programName //this one is to delete files faster
Once you wrote the makefile you write in the terminal the command make and you get the executable file for your program.
However in item.c you aren't using any of the library you included, you only need to include item.h; last thing, I don't know why you are doing the #ifndef thing but it seems a waste.

How can I link multiple c-files together using headers?

I work on a project that has multiple c files. Each c file has its own header. Now I want to put all c files together.
As a preperation I have tried the following thing:
This would be my example c-code (function.c):
#include <stdio.h>
#include "function.h"
void output()
{
printf("Thats a text\n");
}
Thats the associated header file (function.h):
//header function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
#endif // FUNCTION_H_
And thats my main.c:
#include "function.h"
int main()
{
output();
return 0;
}
I would expect the following output:
"Thats a text"
But I only receive following error:
undefined reference to 'output'
What am I doing wrong here?
Thanks a lot!
You need the prototype for output function in your header so that it's visible in other module(s).
//header function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
void output(void);
#endif // FUNCTION_H_
And you need to link the module (source file function.c) in order to actually provide the definition of output that your main module uses.
For example, you can directly compile them together with:
gcc main.c function.c -o my_out
You may also want to look at Makefiles as well.
Your header should be
//header function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
void output();
#endif // FUNCTION_H_
compile like this:
(actual flags may depend on compiler used)
cc -c main.c
(creates main.o)
cc -c function.c
(creates function.o)
cc main.o function.o
(creates a.out or whatever your system default is)
...or as someone else mentioned:
cc main.c function.c
(does it all)

Using char in different files in C

I have 3 .c files main.c, fun1.c, fun2.c
char buff[50];//in fun1.c
char *arg; //in fun2.c
arg = strstr(buff, "001"); //in fun2.c
I want to print buff in fun2.c but it gives an error buff undeclared, even though I declared it in fun1.h as extern char buff[];
There are functions in fun1.c and fun2.c each
It is hard to say what is wrong with your particular program, but here is an example which links 2 .c files with one .h file.
1. A header file functions.h:
#include <stdio.h>
extern void func();
Where I use extern to provide definitions for another file.
2. Now, a functions.c file which uses this header file:
#include "functions.h"
void func() {
printf("hello");
}
This needs to #include the header file, and use the function void() to print a message.
3. Finally, a main.c file which links it all together:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main(void) {
func();
return 0;
}
Which also needs function.h as it uses func(). You then can compile the code as:
gcc -Wall -Wextra -g main.c functions.c -o main
You could also look into makefiles, which would reduce this long compilation line to simply make.

Proper use of header files

Coming up with errors for undefined references in main.c. This is because I have several files in this fashion:
main.c
{
#include "somefile.h"
somfunct() <--- undefined reference error
}
somefile.h
{
somefunct() declaration
...
}
somefile.c
{
#include "somefile.h"
somefunct() definition
...
}
I am trying to use proper organization in that I use only declarations in the header files and define them in a separate file. After splitting up my code I get the undefined reference error because there is no link between somefile.h and somefile.c. Even though main.c includes the somefile.h header file, there is nothing in somefile.h that explicitly mentions somefile.c, so my functions are only partially defined. What is the proper way to take care of this problem? Many thanks. I hope this is clear let me know if not.
Here's a complete and working example of your goal.
main.c
#include "somefile.h"
int main() {
somefunc();
return 0;
}
somefile.h
#ifndef RHURAC_SOMEFILE_H
#define RHURAC_SOMEFILE_H
void somefunc();
#endif
somefile.c
#include <stdio.h>
#include "somefile.h"
void somefunc() {
printf("hello\n");
}
example build ( gcc )
gcc main.c somefile.c -o main
output
$ ./main
hello

#include other C programs

I need to include file_1.c into main.c. In file_1.c, I currently have multiple functions. If I want to call these functions in main.c, what do I need to do? I have #include"file_1.c" in my main program.
Use standard approach by making header file
#include"file_1.h"
you will have to compile this "file_1.c" together with main.c and make one executable
because function calls are need in run time.
Try this :
create a header file file_1.h
#ifndef _FILE_H
#define _FILE_H
void foo(int );
#endif
give all the declaraion of function and struct definitions (if any) or any global variables
then in file_1.c will contain actual defintion of function
//file_1.c
#include "file_1.h"
#include <stdio.h>
void foo(int x)
{
printf("%d\t",x);
}
//main.c
#include "file_1.h"
int main()
{
int x=10;
foo(x);
return 0;
}
include header file file_1.h in both (main.c and file_1.c) the c files
In gcc
gcc -Wall main.c file_1.c -o myexe.out
Why do you think you need to do this?
Normally you would add the declaration of functions in file_1.c into file_1.h and include that in main.c.
When you link the program, you just need to include both main.c and file_1.c (which then includes the definitions of the functions) on the command line.

Resources