How to you link C source files together after you compiler them? Is that what make is for? Also, are functions in one C file visible in another C file if they are linked together?
How to link C source files
Started by DarkLordoftheMonkeys, Jan 23 2010 01:54 PM
7 replies to this topic
#1
Posted 23 January 2010 - 01:54 PM
Life's too short to be cool. Be a nerd.
|
|
|
#2
Posted 23 January 2010 - 02:13 PM
Many IDEs make this trivial. A makefile makes it easier for command-line stuff. But of course the specifics of linking depend on the toolchain you are using. What might that be?
#3
Posted 23 January 2010 - 06:15 PM
Here's my explanation:
Let's say we have two files, hello.c and hello2.c
hello.c:
The first way to do this is:
The second way to do this is to add this to the beginning of hello.c and compile:
Here's the third way to do this:
Let's say we have two files, hello.c and hello2.c
hello.c:
int main(void) {
hello();
return 0;
}
hello2.c:#include <stdio.h>
void hello(void) {
printf("Hello, World!\n");
}
As you can see, hello.c needs to call a function from hello2.cThe first way to do this is:
gcc hello.c hello2.cThis will compile the two files into one executable.
The second way to do this is to add this to the beginning of hello.c and compile:
#include "hello2.c"This tells the compiler to include hello2.c in the compilation automatically.
Here's the third way to do this:
gcc -c hello.c gcc -c hello2.c gcc hello.o hello2.oFirst, you tell gcc to compile hello.c and hello2.c, but you do not link them. This produces the object files hello.o and hello2.o. You can easily link the object files together with the last command. There is also a very nice tutorial that explains make and the compilation process.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Download the new operating system programming kit! (some assembly required)
#4
Posted 23 January 2010 - 07:53 PM
Okay, I think I understand this: you put function definitions in C files, and you put macro and data type definitions in header files. Am I right in thinking this?
Life's too short to be cool. Be a nerd.
#5
Posted 23 January 2010 - 07:53 PM
Guest said:
The second way to do this is to add this to the beginning of hello.c and compile:
#include "hello2.c"This tells the compiler to include hello2.c in the compilation automatically.
The only exception to that generality is in C++ when including template code.
#6
Posted 23 January 2010 - 08:03 PM
DarkLordoftheMonkeys said:
Okay, I think I understand this: you put function definitions in C files, and you put macro and data type definitions in header files. Am I right in thinking this?
Yes.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Download the new operating system programming kit! (some assembly required)
#7
Posted 23 January 2010 - 08:10 PM
There's still one thing I don't understand. Where are functions like printf() and malloc() defined if not in header files? Why do you need to include stdlib.h to use malloc()? Is it because the function prototype for malloc() is in stdlib.h?
Life's too short to be cool. Be a nerd.
#8
Posted 23 January 2010 - 08:39 PM
DarkLordoftheMonkeys said:
There's still one thing I don't understand. Where are functions like printf() and malloc() defined if not in header files? Why do you need to include stdlib.h to use malloc()? Is it because the function prototype for malloc() is in stdlib.h?
Most of the time, these libraries are not source code. A library file will be code that has already been compiled and is ready for the linker.
You need to include the headers so that the compiler can tell you if you are calling the functions incorrectly. ;)


Sign In
Create Account


Back to top









