Linking C and C++ Modules

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 It calls a function called disclaimer() (which prints a
copyright disclaimer) found in a C file called (yep you guessed it)

Here's the relevant code:

/* */

/* Includes ... */
#include "disclaimer.h"

int main()

/* ... */

return 0;

/* end of */

/* 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: undefined reference to `disclaimer()'

I found that if I rename disclaimer.c to 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++?

- --
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
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird -


Relevant Pages

  • Re: menu-based console application
    ... typedef void; ... struct MenuEntry ... Compile everything, fix typing errors and run the program. ...
  • Re: CFile and FILE*
    ... ever call exit() in any C++ ... LPVOID, e.g., ... void DoSomething ... Note that you can still compile this as C ...
  • 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. ...
  • 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 ...
  • 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 ... ...