Linking C and C++ Modules



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



Relevant Pages

  • Re: menu-based console application
    ... typedef void; ... struct MenuEntry ... Compile everything, fix typing errors and run the program. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: CFile and FILE*
    ... ever call exit() in any C++ ... LPVOID, e.g., ... void DoSomething ... Note that you can still compile this as C ...
    (microsoft.public.vc.mfc)
  • Re: Library bug or my fault?
    ... void memcpy ... struct Foo { ... 29 void cp(const Foo *foo) ... You have no definition for cp2so the code above won't compile. ...
    (comp.lang.c)
  • Re: A non-const reference may only be bound to an lvalue?
    ... To my surprise, I have tested that the following code can compile, ... struct X { ... void f; ... There are legitimate reasons to want to do that, but I've come to accept it ...
    (microsoft.public.vc.language)
  • SUMMARY: void main(int argc, char *argv[]) question
    ... The consensus is that the use of void on main is wrong (The book ... return and int. ... >In trying to compile some old test Unix C programs... ... Why do those old UNIX programs used void ... ...
    (SunManagers)