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.
Related
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
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.
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;
}
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.
I'm trying to understand how global variables and functions work in C. My program compiles and works fine with gcc, but does not compile with g++. I have the following files:
globals.h:
int i;
void fun();
globals.c:
#include "stdlib.h"
#include "stdio.h"
void fun()
{
printf("global function\n");
}
main.c:
#include "stdlib.h"
#include "stdio.h"
#include "globals.h"
void myfun();
int main()
{
i=1;
myfun();
return 0;
}
And finally, myfun.c:
#include "stdlib.h"
#include "stdio.h"
#include "globals.h"
void myfun()
{
fun();
}
I get the following error when compiling with g++:
/tmp/ccoZxBg9.o:(.bss+0x0): multiple definition of `i'
/tmp/ccz8cPTA.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Any ideas why? I would prefer to compile with g++.
Every file you include globals.h from will define "int i".
Instead, put "extern int i;" into the header file and then put the actual definition of "int i = 1;" in globals.c.
Putting header guards around globals.h would be sensible too.
Edit: In answer to your question its because a #include works kind of like a cut and paste. It pastes the contents of the included file into the c file that you are calling include from. As you include "globals.h" from main.c and myfun.c you define int i = 1 in both files. This value, being global, gets put into the table of linkable values. If you have the same variable name twice then the linker won't be able to tell which one it needs and you get the error you are seeing. Instead by adding extern on the front in the header file you are telling each file that "int i" is defined somewhere else. Obviously, you need to define it somewhere else (and ONLY in one place) so defining it in globals.c makes perfect sense.
Hope that helps :)
I would add an include guard in your globals file
#ifndef GLOBALS_H
#define GLOBALS_H
int i;
void fun();
#endif
Edit: Change your globals to be like this (using extern as the other answer describes)
globals.h
extern int i;
extern void fun();
globals.c
#include "stdlib.h"
#include "stdio.h"
int i;
void fun()
{
printf("global function\n");
}
I compiled it with
g++ globals.c main.c myfun.c
and it ran ok
Several things wrong here; several other things highly recommended:
globals.h:
#ifndef GLOBALS_H
#define GLOBALS_H
extern int my_global;
#ifdef __cplusplus
extern "C" {
#endif
void fun();
#ifdef __cplusplus
}
#endif
#endif
/* GLOBALS_H */
globals.c:
#include <stdlib.h>
#include <stdio.h>
#include "globals.h"
int my_global;
void fun()
{
printf("global function: %d\n", my_global);
}
main.c:
#include <stdlib.h>
#include <stdio.h>
#include "globals.h"
void myfun();
int main()
{
my_global=1;
myfun();
return 0;
}
void myfun()
{
fun();
}
You should declare "extern int myvar" in your header, and actually allocate "int myvar" in one and only one .c file.
You should include "globals.h" in every file that uses "myvar" - including the file where it's allocated.
Especially if you're planning on mixing C and C++ modules, you should use 'extern "C"' to distinguish non-C++ functions.
System headers should be "#include <some_header.h>"; your own headers should use quotes (#include "myheader.h") instead.
Short variable names like "i" might be OK for a strictly local variable (like a loop index), but you should always use longer, descriptive names whenever you can't avoid using a global variable.
I added a "printf" for my_global.
'Hope that helps!
I had this problem when porting some old C code to C++. The problem was it was a project that was connected to a database, and i wanted to port the database to c++ but not the rest. The database pulled in some C dependencies that couldn't be ported, so i needed the C code that overlapped both the database and the other project to compile in g++ as well as gcc...
The solution to this problem is to define all variables as extern in the .h file. then when you compile in either gcc or g++ it will report symbols missing in the .c files. So edit the .c files in the error messages and insert the declaration into all the .c files that need the variables. Note: you may have to declare it in multiple .c files, which is what threw me and why I was stuck on this problem for ages.
Anyway this solved my problem and the code compiles cleanly under both gcc and g++ now.