tags:

views:

52

answers:

5

Trying to call a function from another file in C with the following code:

main.c

#include "display.h"
int main()
{
display_options();
display_price();

return 0;
}

display.h

int display_options(void);
int display_price(void);

display.c

#include <stdio.h>

int display_options()
{
printf("Welcome to the pizza parlor\n");
printf("What size pizza would you like? (in inches)");

return 0;
}

int display_price()
{
printf("Your pizza will cost 0.00\n");

return 0;
}

I created this following an example here http://www.faqs.org/docs/learnc/x307.html but it doesn't seem to be working i get an error message in codeblocks 10.05 on the function called in main.c saying "undefined reference to 'display_options'"

+3  A: 

Looks like you are compiling just the main.c file.

Make sure you compile as:

gcc -Wall main.c display.c

and run it as:

./a.out
codaddict
Isn't it better to use gcc -Wall main.c display.c -o display then just ./display?
Cristina
ok, i understand how this would work, but im unsure how this would be done in codeblocks in xp
deepheat
A: 

gcc allows you to compile and link a trivial application (has only 1 .c file) in one step.

For applications with more than one .c file, you need to compile all the source (.c) files into object (.o) files. These then need to be linked together.

So you will need to compile each .c file

gcc -c main.c
gcc -c display.c

and then link them using

gcc -o display main.o display.o

This will create the binary display

This can be automated with a Makefile. you then build the entire thing by just calling make.

doron
A: 

display.c

#include <stdio.h>

int display_options(void);
int display_price(void);
int display_options()
{
printf("Welcome to the pizza parlor\n");
printf("What size pizza would you like? (in inches)");

return 0;
}

int display_price()
{
printf("Your pizza will cost 0.00\n");

return 0;
}

Now include direct display.c

in the main file instead of display.h

Ricky Dang
+1  A: 

Make sure that #include "display.h" is also at the top of the display.c file. Since you are using Code::Blocks, it will automatically compile main.c and display.c for you once you do that.

Andrew Dunn
i tried this, i still get the error message
deepheat
Add it then rebuild everything (Ctrl+F11). Also, move `#include <stdio.h>` to `display.h` and ensure all files are in the same project.
Andrew Dunn
A: 

In Code::Blocks, you need to make sure that display.c is in your project (if you haven't created a project, do so now) and that it's also included in one or more build targets. debug and release build targets are created by default, but nothing is added to a target unless you say so. When you create a new file or add an existing file to a project, the IDE should ask you which targets to add the file to. Select all of them and hit OK.

If your file is already in the project but not included in any target, go to Project | Properties | Build targets and make sure that the Build target files panel shows checkmarks beside both main.c and display.c.

Jon Purdy
thanks, this is what the problem was :)
deepheat
No problem. I'm glad to help. Of course, the other answers tell you *why* you were seeing the behaviour that you were. It's important that you learn how build systems work under the hood and not rely solely on an IDE to take care of it for you; you'd be surprised how soon you'll find yourself in front of a terminal with nothing but an editor and a compiler at your disposal.
Jon Purdy
i have compiled programs using the terminal, but i've never had to compile a program with multiple files the terminal. would you say that it is more efficient to build the program in a terminal rather than an IDE or are there other reasons for doing so. apart from the obvious: not having to use the mouse to click buttons
deepheat
There's basically no difference between compiling in a terminal versus compiling with an IDE, since the IDE just outsources to the same compiler you'd be using anyway, and it adds other handy features such as figuring out which files have changed and need to be rebuilt. In general, though, you should understand that source files become object files via compilation, and object files become libraries and executables via linking.
Jon Purdy