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

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)

Related

How to create a C library with CodeBlocks?

How can I create a C library in CodeBlocks that can be define and used like a standard library with the #include command?
In fact I want to create a simple library that is Composed of several functions.
Basically, you need a .h file for the header definitions and a .c containing the source code.
An example:
/* command.h */
#ifndef COMMAND_H
#define COMMAND_H
int func(void);
#endif /* COMMAND_H */
/* command.c */
#include "command.h"
int func(void)
{
return 0;
}
/* main.c */
#include <stdio.h>
#include "command.h"
int main(void)
{
printf("%d\n", func());
return 0;
}
ifndef is used to prevent the file from being included more than once.
Compile it including both .c files in the command line:
gcc -o demo main.c command.c
Or in your case, follow this guide to compile multiple files in codeblocks.

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

How to link two files using header file in 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.

C preprocessor directive error

I have a problem when i want use his scripts:
lib1.h
...
#ifdef LIB1_01
int lib1func(void);
#endif
...
lib1.c
...
#ifdef LIB1_01
int lib1func(void){
...
}
#endif
...
main.c
#define LIB1_01
#include <lib1.h>
int main(){
...
int x = lib1func(void);
...
...
I want use lib1func() when #define LIB1_01 is declared but I have an 'warning : implicit declaration of function' error when i use it...why ? Can you help me ?
Best regards.
Recommended alternative:
lib1.h
#ifndef LIB1_H
#define LIB1_H
int lib1func(void);
#endif
...
lib1.c
#include "lib1.h"
int lib1func(void){
...
}
main.c
#include "lib1.h"
int main(){
...
int x = lib1func(void);
...
...
NOTE:
1) You should declare "int lib1func(void)" in the header, but you may define it anywhere. In lib1.c (if you prefer), or even main.c. Just make sure you only define it once.
2) Note the use of the guard around the entire header body.
3) Also note the use of include "myheader.h" (for your own header files), vs. #include <systemheader.h>. The "<>" syntax should be used only for system headers.
To use that kind of includes, compile with option I.
gcc myfile.c -o myfile -I .
The . symbol means look in the current directory.

Duplicate header files throughout source files?

// File foo1.c :
#include <stdio.h> // once
void foo1(void);
void foo1(void){
puts("foo1");
}
// File foo2.c :
#include <stdio.h> // again
void foo2(void);
void foo2(void){
puts("foo2");
}
// File foomain.c :
#include <stdio.h> // yet again
void foo1(void); // again
void foo2(void); // again
int main(void){
foo1();
foo2();
puts("foomain");
return 0;
}
// create object files
gcc -fPIC foo1.c -o foo1.o // 1 stdio.h
gcc -fPIC foo2.c -o foo2.o // 1 stdio.h
// create shared library
gcc -fPIC -shared foo1.o foo2.o -o foo.so // foo.so contains stdio.h 2 times ?
// build entire program
gcc foo.so foomain.c -o foomain // foomain contains 1 stdio.h plus the 2 from foo.so ?
Why does the entire program contain 3 stdio.h ? Seems redundant, why not just 1 ? Shouldn't the compiler need only 1 ?
It makes sense for the object files to contain a prototype but why do they have to be specified again in foomain.c ? Shouldn't the compiler know they are already specified in foo.so ?
That's because each file is compiled separately, so each time the compiler should know the signatures of all functions used to perform compile-time checks. So, each file has to contain all declarations used, which are included by the preprocessor before the file is compiled.
If you look at the top of most header files they have an include guard to stop double inclusion.
#ifndef FOO
#define FOO
#endif
See Include Guard for more information.
The #include lines are not actually a part of the compiler, but the C preprocessor.
What the preprocessor does with #include lines is to actually include the file into the source, and creates a new temporary file containing the contents of your file with the #include line replaced by the contents of the file being included.
You don't actually need the include file at all, if all you are doing is calling functions. You might get warnings about the functions not being declared, but those can be adding the prototypes for those functions yourself. For example, in your main source file you only use puts, instead of including <stdio.h> you can add a prototype like this:
int puts(const char *s);
However, <stdio.h> also defines some structures (like the FILE structure) and declares some variables (like stdout) and if you use any of those you need the header file as well.
You can use include guards as #Jeff suggested or just put #pragma once at the top of each header.

Resources