Jump to content

How to link C source files

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
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?
Life's too short to be cool. Be a nerd.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
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
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Here's my explanation:
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.c
The first way to do this is:
gcc hello.c hello2.c
This 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.o
First, 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)

#4
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
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
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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.
That's the way not to do it. If "hello2.c" were included by more than one file, you'd end up with linker errors.

The only exception to that generality is in C++ when including template code.

#6
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts

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)

#7
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
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
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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?
They are in library modules that are included by the linker.

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. ;)