Proper use of header files - c

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

Related

Why isn't gcc recognizing my .c file even when I provide it as an argument?

I have file1.c:
#include <stdio.h>
int main() {
file2();
}
and file2.c:
#include <stdio.h>
int file2() {
}
I'm trying to compile file1 with gcc -o file1 file1.c file2.c, but I'm getting the error implicit declaration of function 'file2' is invalid.
Does anyone know what I could be missing here? file1.c and file2.c are both in the same folder.
Does anyone know what I could be missing here?
Each source file is treated separately when being compiled. Thus the compiler when taking care of file.1.c is unaware of what file2() might be and complains.
You will need the function prototype int file2(void); in your file1.c or any included files, because each file is compiled separately and you need to till the compiler where he can find the function before you call it.
You need to have the function declaration in you file1.c file also. Like so
#include <stdio.h>
int file2();
int main() {
file2();
}

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.

including c file includes by header file

I have those 3 files in my workspace:
1.h
#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
//code
#endif
1.c
#include <stdio.h>
#include "1.h"
//code
main.c
#include "1.h"
int main(){
printf("hello");
}
but in main.c printf is unidentified, is there a way to make it work while the relevant header is called in 1.c?
how can I link the 1.c and 1.h files?
edit: it's an academic assignment and I'm not allowed to make changes in the main and header.
You have included #include <stdio.h> only in 1.c, not in 1.h or main.c.
Obvious solution is to include it in main.c.
Because of the way the #include macro works (it expands the whole header file that you include at the line where you call the macro), you actually don't need to include stdio.h within main.c as long as stdio.h is included in a header file that main.c includes.
Hopefully this makes it clear:
main.c
#include "test.h"
int main()
{
printf("I can call this function thanks to test.h!\n");
return 0;
}
test.h
#include <stdio.h>
This will work just fine.
But this is not the same as being able to use a function that a .c file has access to based on its own #include definition just because you cross-compiled them. In that case the other.c file that calls #include <stdio.h> will get printf(), but main.c does not.
In this setup,
main.c
int main()
{
printf("I don't have any way to call this...\n");
return 0;
}
test.c
#include <stdio.h>
You will not have any way for main to know what printf() is, even if you cross-compile the two. test.c knows what printf() is but not main.
What you want is to have #include <stdio.h> in other.h, and then #include "other.h" in main.c.
But for future reference, this is probably poor practise as it should be immediately apparent what each file's requirements are so that you get a good sense of what its job is.
So here's what I would probably suggest as the best solution:
main.c
#include <stdio.h>
int main()
{
printf("I can call this all on my own.\n");
return 0;
}

#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