Linking C and C++ Modules
- From: Jonathan Lamothe <jonathan_lamothe@xxxxxxxxxxx>
- Date: Sun, 02 Apr 2006 21:54:25 -0400
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hey guys, this is probably a dumb question, but I can't figure out the
answer for the life of me.
I'm writing myself a custom program to track finances and such (yes, I
know such things already exist, but I haven't found one I like), but
I've run into a problem. Some modules are better off written in C++ and
some are better suited to C. They'll all compile into object code quite
nicely, but they won't link.
Here's an example:
The main() function in my program is contained in a C++ file called
main.cc. It calls a function called disclaimer() (which prints a
copyright disclaimer) found in a C file called (yep you guessed it)
disclaimer.c.
Here's the relevant code:
/* main.cc */
/* Includes ... */
#include "disclaimer.h"
int main()
{
disclaimer();
/* ... */
return 0;
}
/* end of main.cc */
/* disclaimer.h */
#ifndef DISCLAIMER_H
#define DISCLAIMER_H
void disclaimer();
#endif /* DISCLAIMER_H */
/* end of disclaimer.h */
/* disclaimer.c */
#include <stdio.h>
void disclaimer()
{
/* Use fprintf to print a disclaimer on stderr. */
}
/* end of disclaimer.c */
As I mentioned the object files compiles quite nicely, but when I try to
link them I get the following error:
main.cc:41: undefined reference to `disclaimer()'
I found that if I rename disclaimer.c to disclaimer.cc and compile it as
a C++ file everything works fine, but that defeats the whole point. Can
anyone tell me what I'm doing wrong? Can what I'm attempting actually
be done, or should I just re-write everything in C++?
- --
Regards,
Jonathan Lamothe
/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/
die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEMIBQNrv4JaRC3JsRArBZAJ414MovWFxJ/XlqvOGXC9naMPRnZgCfc/bw
4qVRNQ/VWalyJcsPUiEEWWs=
=aPBp
-----END PGP SIGNATURE-----
.
- Follow-Ups:
- Re: Linking C and C++ Modules
- From: Willem
- Re: Linking C and C++ Modules
- From: uraniumore235
- Re: Linking C and C++ Modules
- Prev by Date: Re: introselect
- Next by Date: exponentiation algorithm
- Previous by thread: introselect
- Next by thread: Re: Linking C and C++ Modules
- Index(es):
Relevant Pages
|