|
-
Using multiple source files in C
I've been programming C for a while now, but never got round to actually using multiple source files. Quite embarassing as I expect it's really simple. Can anyone enlighten me?
-
Each source file compiles to a separate object file. The linker links multiple object files together. However, the linker needs to know what files to link. This is usually done with something like a make file or project files. Assuming you are using MS VC++, it uses project files. You add multiple source files to a project, and the linker then knows to combine them together.
The main thing to remember is that each source file has nothing to do with one another. A global variable in one file is not visible in another by default. This confused me for some time. I would have a.c and b.c with headers for each file. I would then make c.h that would have a global variable, 'x'. I would include c.h in both a and b, and try to use x in each source file. This causes linker errors. What you need to do in this case is pick a file to make x global in. Maybe in a.c or b.c, or possibly in c.c. Then the other files would use the "extern" keyword, and the linker would import the correct global variable. The moral of the story is that everything outside of a single .c file is just view as a placeholder until you link. Say a.c contains f(x) and a.h contains the prototype for f(x), and b.h includes a.h. Assume b.c uses f(x). When a.c compiles, f(x) will be changed to object code. When b.c compiles, the compiler will look at the prototype to make sure the correct types are being sent as parameters and assigned as return, but it doesn't care about the code for f(x). When you link, the linker will combine the two object files so that the actual code for f(x) will be used by b. I hope I got this right
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
-
OK, here goes some test code to see if I've got this right. The function f in f.c simply prints an integer using printf, described (hopefully) by f.h. The main file, test.c, #includes f.h and tries to print "8" using f.
The error given (Visual Studio 6) is "error C2143: syntax error : missing '{' before 'PCH creation point'" on line 3 of test.c. Anyone know what the problem is?
In the file f.c:
#include<stdio.h>
int f(int x)
{
printf("%d\n", x);
return 0;
}
The header for f.c, f.h
int f(int)
In the file test.c:
#include "f.h"
int main()
{
f(8);
return -1;
}
-
The prototype of f() in f.h needs a semicolon after it.
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
-
Most embarassing. First .h file I've ever written. 
Thanks Zoma!
-
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|