/* main.c */
extern void foo ( void );
int main ( void )
{
foo();
return 0;
}
/* print.c */
#include <stdio.h>
void foo ( void )
{
puts ( "foo!" );
}
That's all it takes. In fact, you don't even need the extern keyword for the prototype (but it aids clarity). If that's what you're doing, can you explain how the compiler "refuses to work"?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
How are you compiling and linking your files?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Well first, your prototype and definition don't match. If you omit the return type, that's call implicit int and an int return type will be assumed, not void. Second, I asked how you're compiling and linking, not to post your code.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Okay, then post your exact code so that I can try it out.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
It works fine for me in Visual Studio 2005 after correcting the syntax errors, setting the correct return type for your external function, and compiling as C++ because that's clearly what you're doing. Post the errors you get when you build.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I can't help if I can't reproduce the problem. Your code works fine once I clear up the compilation errors, which suggests that your files are somehow not set up properly in the project.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Try changing it to:-
/* main.c */
#include "print.c"
int main ( void )
{
foo();
return 0;
}
I used gcc and that worked.
/* print.c */
#include <stdio.h>
void foo ( void )
{
puts ( "foo!" );
}
output
>g++ -Wall main.c
>./a.out
>foo!
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>I used gcc and that worked.
Well yea, of course it would work because #include just pastes the text of the print.c into main.c. It's also dangerous if you decide to do this in multiple files. You'll get more linker errors about multiple definitions. It's better to learn to do it the right way than to hack something that "just works".
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Why is it dangerous if no duplicate definitions of foo exist?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>Why is it dangerous if no duplicate definitions of foo exist?
Include the file one more time and duplicate definitions do exist. The only way to defend against that kind of error is to name the file something obviously different from *.c or *.h:
#include "print.def"
int main ( void )
{
foo();
return 0;
}
That way it catches your eye, but you're still relying on the user of the file to do the right thing. It's much much safer to do the header/implementation setup:
#include "print.h"
int main ( void )
{
foo();
return 0;
}
#ifndef PRINT_H
#define PRINT_H
void foo ( void );
#endif
#include <stdio.h>
#include "print.h"
void foo ( void )
{
puts ( "foo!" );
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I don't get it? Where do ya put this:-
#ifndef PRINT_H
#define PRINT_H
void foo ( void );
#endif
Plus I still don't see the difference between doing a #include "somthing.c" and a #include "something.h"
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439