gcc/linking to: myfunc.cpp vs. myfunc.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* hello.cpp */
#include <stdio.h>
extern void myfunc();
int main()
{
  myfunc();
  return 0;
}
/* myfunc.c, or myfunc.cpp */
#include <stdio.h>
void myfunc()
{
  printf("we are in myfunc\n");
}


(Using MinGW 5.1.4)
this works...
gcc -o hello hello.cpp myfunc.cpp


linker error, why?
gcc -o hello hello.cpp myfunc.c

... :hello.cpp:(.text+0x2b): undefined reference to `myfunc()'
collect2: ld returned 1 exit status
Last edited on
C++ name mangling
Read up on extern "C".
C and C++ functions are not the same thing. You need to be clear on what language you are using. There are rules on how to mix them.
I always thought once you get to the link stage, C and C++ are both assembly and indistinguishable. But having a C background I totally forgot about the name mangling. extern "C" did the trick.

Is there anything else I should watch out for when mixing C and C++??

Anyways, thanks: guestgulkan, seymore15074 for the help!
Last edited on
Topic archived. No new replies allowed.