Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to link C source files


  • Please log in to reply
7 replies to this topic

#1 DarkLordoftheMonkeys

DarkLordoftheMonkeys

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

Posted 23 January 2010 - 01:54 PM

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

#2 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

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?
  • 0

#3 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

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:
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.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#4 DarkLordoftheMonkeys

DarkLordoftheMonkeys

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

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

#5 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 23 January 2010 - 07:53 PM

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.
  • 0

#6 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

Posted 23 January 2010 - 08:03 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?


Yes.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#7 DarkLordoftheMonkeys

DarkLordoftheMonkeys

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 232 posts

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

#8 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 23 January 2010 - 08:39 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?

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




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download